Add --force flag to replace existing network protocol profile
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		| @@ -3,7 +3,7 @@ type: kubernetes | |||||||
| name: 'Golang Build' | name: 'Golang Build' | ||||||
|  |  | ||||||
| environment: | environment: | ||||||
|   BINARY_VERSION: v0.4.4 |   BINARY_VERSION: v0.4.5 | ||||||
|  |  | ||||||
| clone: | clone: | ||||||
|   disable: true |   disable: true | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ var Commands struct { | |||||||
| 		DnsServer    []string `long:"dnsserver" required:"true"` | 		DnsServer    []string `long:"dnsserver" required:"true"` | ||||||
| 		DnsDomain    string   `long:"dnsdomain" required:"true"` | 		DnsDomain    string   `long:"dnsdomain" required:"true"` | ||||||
| 		Gateway      string   `long:"gateway" required:"true"` | 		Gateway      string   `long:"gateway" required:"true"` | ||||||
|  | 		Force        bool     `short:"f" long:"force"` | ||||||
| 	} // `command:"datacenter" alias:"dc" description:"Define a Network Protocol Profile within a datacenter"` | 	} // `command:"datacenter" alias:"dc" description:"Define a Network Protocol Profile within a datacenter"` | ||||||
| 	VirtualMachine struct { | 	VirtualMachine struct { | ||||||
| 		Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"` | 		Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"` | ||||||
| @@ -62,7 +63,7 @@ func main() { | |||||||
|  |  | ||||||
| 	switch parser.Active.Name { | 	switch parser.Active.Name { | ||||||
| 	case "datacenter", "dc": | 	case "datacenter", "dc": | ||||||
| 		if err := hypervisor.CreateNetworkProtocolProfile(ctx, clt, Commands.Datacenter.Name, Commands.Datacenter.Network, Commands.Datacenter.StartAddress, Commands.Datacenter.EndAddress, Commands.Datacenter.Netmask, Commands.Datacenter.DnsDomain, Commands.Datacenter.Gateway, Commands.Datacenter.DnsServer); err != nil { | 		if err := hypervisor.CreateNetworkProtocolProfile(ctx, clt, Commands.Datacenter.Name, Commands.Datacenter.Network, Commands.Datacenter.StartAddress, Commands.Datacenter.EndAddress, Commands.Datacenter.Netmask, Commands.Datacenter.DnsDomain, Commands.Datacenter.Gateway, Commands.Datacenter.DnsServer, Commands.Datacenter.Force); err != nil { | ||||||
| 			log.Fatalf("[ERROR] Could not create network protocol profile: %s", err) | 			log.Fatalf("[ERROR] Could not create network protocol profile: %s", err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ import ( | |||||||
| 	"spamasaurus.com/m/pkg/utils" | 	"spamasaurus.com/m/pkg/utils" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network, startaddress, endaddress, netmask, dnsdomain, gateway string, dnsserver []string) error { | func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datacenter, network, startaddress, endaddress, netmask, dnsdomain, gateway string, dnsserver []string, force bool) error { | ||||||
| 	finder := find.NewFinder(clt, true) | 	finder := find.NewFinder(clt, true) | ||||||
| 	dc, err := finder.Datacenter(ctx, datacenter) | 	dc, err := finder.Datacenter(ctx, datacenter) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -33,7 +33,22 @@ func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datace | |||||||
| 	pc := property.DefaultCollector(clt) | 	pc := property.DefaultCollector(clt) | ||||||
| 	pc.Retrieve(ctx, []types.ManagedObjectReference{nw.Reference()}, []string{"summary"}, &networksummary) | 	pc.Retrieve(ctx, []types.ManagedObjectReference{nw.Reference()}, []string{"summary"}, &networksummary) | ||||||
| 	if networksummary.Summary.GetNetworkSummary().IpPoolId != nil { | 	if networksummary.Summary.GetNetworkSummary().IpPoolId != nil { | ||||||
| 		log.Fatalf("[ERROR] Network '%s' already has an existing protocol profile associated", network) | 		if force == true { | ||||||
|  | 			request := &types.DestroyIpPool{ | ||||||
|  | 				This:  *clt.ServiceContent.IpPoolManager, | ||||||
|  | 				Dc:    dc.Reference(), | ||||||
|  | 				Id:    *networksummary.Summary.GetNetworkSummary().IpPoolId, | ||||||
|  | 				Force: true, | ||||||
|  | 			} | ||||||
|  | 			if _, err := methods.DestroyIpPool(ctx, clt.RoundTripper, request); err != nil { | ||||||
|  | 				log.Fatalf("[ERROR] Could not remove existing network protocol profile '%s'", | ||||||
|  | 					networksummary.Summary.GetNetworkSummary().IpPoolName) | ||||||
|  | 			} | ||||||
|  | 		} else { | ||||||
|  | 			log.Fatalf("[ERROR] Network '%s' already has existing network protocol profile '%s' associated; use the --force flag to replace it", | ||||||
|  | 				network, | ||||||
|  | 				networksummary.Summary.GetNetworkSummary().IpPoolName) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	iprange, err := netrange.ParseRange(fmt.Sprintf("%s-%s", startaddress, endaddress)) | 	iprange, err := netrange.ParseRange(fmt.Sprintf("%s-%s", startaddress, endaddress)) | ||||||
| @@ -66,7 +81,10 @@ func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datace | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if _, err := methods.CreateIpPool(ctx, clt.RoundTripper, request); err != nil { | 	if _, err := methods.CreateIpPool(ctx, clt.RoundTripper, request); err != nil { | ||||||
| 		log.Fatalf("[ERROR]: Failed creating new network protocol profile (for network '%s' within datacenter '%s'): %s", network, datacenter, err) | 		log.Fatalf("[ERROR]: Failed creating new network protocol profile (for network '%s' within datacenter '%s'): %s", | ||||||
|  | 			network, | ||||||
|  | 			datacenter, | ||||||
|  | 			err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user