From 6f6da746f9216506fedfb305bc96f12518801a59 Mon Sep 17 00:00:00 2001 From: Danny Bessems Date: Thu, 29 Dec 2022 21:34:41 +0100 Subject: [PATCH] Add --force flag to replace existing network protocol profile --- .drone.yml | 2 +- cmd/npp-prepper/main.go | 3 ++- pkg/hypervisor/datacenter.go | 24 +++++++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.drone.yml b/.drone.yml index 0b1f1cb..f63e7a2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,7 +3,7 @@ type: kubernetes name: 'Golang Build' environment: - BINARY_VERSION: v0.4.4 + BINARY_VERSION: v0.4.5 clone: disable: true diff --git a/cmd/npp-prepper/main.go b/cmd/npp-prepper/main.go index 1740774..eebb9c4 100644 --- a/cmd/npp-prepper/main.go +++ b/cmd/npp-prepper/main.go @@ -25,6 +25,7 @@ var Commands struct { DnsServer []string `long:"dnsserver" required:"true"` DnsDomain string `long:"dnsdomain" 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"` VirtualMachine struct { Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"` @@ -62,7 +63,7 @@ func main() { switch parser.Active.Name { 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) } diff --git a/pkg/hypervisor/datacenter.go b/pkg/hypervisor/datacenter.go index 0598b5c..55ef5c2 100644 --- a/pkg/hypervisor/datacenter.go +++ b/pkg/hypervisor/datacenter.go @@ -17,7 +17,7 @@ import ( "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) dc, err := finder.Datacenter(ctx, datacenter) if err != nil { @@ -33,7 +33,22 @@ func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datace pc := property.DefaultCollector(clt) pc.Retrieve(ctx, []types.ManagedObjectReference{nw.Reference()}, []string{"summary"}, &networksummary) 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)) @@ -66,7 +81,10 @@ func CreateNetworkProtocolProfile(ctx context.Context, clt *vim25.Client, datace } 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