diff --git a/examples/teardown_test.go b/examples/teardown_test.go index 1d347fa0..c59496e9 100644 --- a/examples/teardown_test.go +++ b/examples/teardown_test.go @@ -42,7 +42,7 @@ import ( ) /* - Teardown example + Teardown example - Delete VMs - Delete Load Balancers @@ -92,7 +92,7 @@ func ExampleTeardown() { Teardown(netID, client, ctx) - filterNets := osc.FiltersNet{ NetIds: &[]string{netID} } + filterNets := osc.FiltersNet{NetIds: &[]string{netID}} netsCB, httpRes, err := client.NetApi.ReadNets(ctx).ReadNetsRequest(osc.ReadNetsRequest{Filters: &filterNets}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Net state") @@ -162,11 +162,13 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { fmt.Fprintf(os.Stderr, "> Load Balancer \"%s\" deleted\n", *lb.LoadBalancerName) } } - if !cleared { fmt.Fprintln(os.Stderr, "> No Load Balancers found") } + if !cleared { + fmt.Fprintln(os.Stderr, "> No Load Balancers found") + } // Unlink and delete non main Route Tables isMainRT := false - filterRTs := osc.FiltersRouteTable{ NetIds: &[]string{netID}, LinkRouteTableMain: &isMainRT } + filterRTs := osc.FiltersRouteTable{NetIds: &[]string{netID}, LinkRouteTableMain: &isMainRT} routeTables, _, err := client.RouteTableApi.ReadRouteTables(ctx).ReadRouteTablesRequest(osc.ReadRouteTablesRequest{Filters: &filterRTs}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Route Tables") @@ -188,10 +190,12 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } fmt.Fprintf(os.Stderr, "> Route Table \"%s\" deleted\n", *routeTable.RouteTableId) } - if len(*routeTables.RouteTables) == 0 { fmt.Fprintln(os.Stderr, "> No Route Tables found") } + if len(*routeTables.RouteTables) == 0 { + fmt.Fprintln(os.Stderr, "> No Route Tables found") + } // Unlink Virtual Gateways - filterVGWs := osc.FiltersVirtualGateway{ LinkNetIds: &[]string{netID} } + filterVGWs := osc.FiltersVirtualGateway{LinkNetIds: &[]string{netID}} vgws, _, err := client.VirtualGatewayApi.ReadVirtualGateways(ctx).ReadVirtualGatewaysRequest(osc.ReadVirtualGatewaysRequest{Filters: &filterVGWs}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Virtual Gateways") @@ -205,10 +209,12 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } fmt.Fprintf(os.Stderr, "> Virtual Gateway \"%s\" deleted\n", *vgw.VirtualGatewayId) } - if len(*vgws.VirtualGateways) == 0 { fmt.Fprintln(os.Stderr, "> No Virtual Gateways found") } + if len(*vgws.VirtualGateways) == 0 { + fmt.Fprintln(os.Stderr, "> No Virtual Gateways found") + } // Delete NATs - filterNats := osc.FiltersNatService{ NetIds: &[]string{netID} } + filterNats := osc.FiltersNatService{NetIds: &[]string{netID}} nats, _, err := client.NatServiceApi.ReadNatServices(ctx).ReadNatServicesRequest(osc.ReadNatServicesRequest{Filters: &filterNats}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the NATs") @@ -232,7 +238,7 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } // Delete Net Access Points - filterNetAPs := osc.FiltersNetAccessPoint { NetIds: &[]string{netID} } + filterNetAPs := osc.FiltersNetAccessPoint{NetIds: &[]string{netID}} netAPs, _, err := client.NetAccessPointApi.ReadNetAccessPoints(ctx).ReadNetAccessPointsRequest(osc.ReadNetAccessPointsRequest{Filters: &filterNetAPs}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Net Access Points") @@ -246,11 +252,13 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } fmt.Fprintf(os.Stderr, "> Net Access Point \"%s\" deleted\n", *netAP.NetAccessPointId) } - if len(*netAPs.NetAccessPoints) == 0 { fmt.Fprintln(os.Stderr, "> No Net Access Points found") } + if len(*netAPs.NetAccessPoints) == 0 { + fmt.Fprintln(os.Stderr, "> No Net Access Points found") + } // Delete Net Peerings - filterSourcePeers := osc.FiltersNetPeering { SourceNetNetIds: &[]string{netID} } - filterAccepterPeers := osc.FiltersNetPeering { AccepterNetNetIds: &[]string{netID} } + filterSourcePeers := osc.FiltersNetPeering{SourceNetNetIds: &[]string{netID}} + filterAccepterPeers := osc.FiltersNetPeering{AccepterNetNetIds: &[]string{netID}} sourceNetPeers, _, err := client.NetPeeringApi.ReadNetPeerings(ctx).ReadNetPeeringsRequest(osc.ReadNetPeeringsRequest{Filters: &filterSourcePeers}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the source Net Peerings") @@ -269,19 +277,23 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { if err != nil { return err } - if !(clearedSource || clearedAccepters) { fmt.Fprintln(os.Stderr, "> No Net Peerings found") } + if !(clearedSource || clearedAccepters) { + fmt.Fprintln(os.Stderr, "> No Net Peerings found") + } // Delete NICs - filterNics := osc.FiltersNic{ NetIds: &[]string{netID} } - nics, _, err := client.NicApi.ReadNics(ctx).ReadNicsRequest(osc.ReadNicsRequest{ Filters: &filterNics }).Execute() + filterNics := osc.FiltersNic{NetIds: &[]string{netID}} + nics, _, err := client.NicApi.ReadNics(ctx).ReadNicsRequest(osc.ReadNicsRequest{Filters: &filterNics}).Execute() err = teardownNICs(vmIds, &publicIps, nics, client, ctx) if err != nil { return err } - if len(*nics.Nics) == 0 { fmt.Fprintln(os.Stderr, "> No NICs found") } + if len(*nics.Nics) == 0 { + fmt.Fprintln(os.Stderr, "> No NICs found") + } // Unlink and delete Internet Services - filterISs := osc.FiltersInternetService{ LinkNetIds: &[]string{netID} } + filterISs := osc.FiltersInternetService{LinkNetIds: &[]string{netID}} interServices, _, err := client.InternetServiceApi.ReadInternetServices(ctx).ReadInternetServicesRequest(osc.ReadInternetServicesRequest{Filters: &filterISs}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Internet Services") @@ -302,11 +314,13 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } fmt.Fprintf(os.Stderr, "> Internet Service \"%s\" deleted\n", *interService.InternetServiceId) } - if len(*interServices.InternetServices) == 0 { fmt.Fprintln(os.Stderr, "> No Internet Services found") } + if len(*interServices.InternetServices) == 0 { + fmt.Fprintln(os.Stderr, "> No Internet Services found") + } // Delete non default Security Groups - filterSGs := osc.FiltersSecurityGroup{ NetIds: &[]string{netID} } - secGroups, _, err := client.SecurityGroupApi.ReadSecurityGroups(ctx).ReadSecurityGroupsRequest(osc.ReadSecurityGroupsRequest{ Filters: &filterSGs }).Execute() + filterSGs := osc.FiltersSecurityGroup{NetIds: &[]string{netID}} + secGroups, _, err := client.SecurityGroupApi.ReadSecurityGroups(ctx).ReadSecurityGroupsRequest(osc.ReadSecurityGroupsRequest{Filters: &filterSGs}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Security Groups") return err @@ -314,7 +328,7 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { cleared = false for _, secGroup := range *secGroups.SecurityGroups { if *secGroup.SecurityGroupName != "default" { - _, _, err := client.SecurityGroupApi.DeleteSecurityGroup(ctx).DeleteSecurityGroupRequest(osc.DeleteSecurityGroupRequest{ SecurityGroupId: secGroup.SecurityGroupId }).Execute() + _, _, err := client.SecurityGroupApi.DeleteSecurityGroup(ctx).DeleteSecurityGroupRequest(osc.DeleteSecurityGroupRequest{SecurityGroupId: secGroup.SecurityGroupId}).Execute() if err != nil { fmt.Fprintf(os.Stderr, "Error deleting the Security Group \"%s\"\n", *secGroup.SecurityGroupId) return err @@ -323,19 +337,21 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { cleared = true } } - if !cleared { fmt.Fprintln(os.Stderr, "> No Security Groups found") } + if !cleared { + fmt.Fprintln(os.Stderr, "> No Security Groups found") + } // Delete Public IPs if len(publicIps) != 0 { - filterPips := osc.FiltersPublicIp{ PublicIps: &publicIps } - publicIPsCb, _, err := client.PublicIpApi.ReadPublicIps(ctx).ReadPublicIpsRequest(osc.ReadPublicIpsRequest{ Filters: &filterPips }).Execute() + filterPips := osc.FiltersPublicIp{PublicIps: &publicIps} + publicIPsCb, _, err := client.PublicIpApi.ReadPublicIps(ctx).ReadPublicIpsRequest(osc.ReadPublicIpsRequest{Filters: &filterPips}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the Public IPs") return err } for _, publicIP := range *publicIPsCb.PublicIps { if publicIP.LinkPublicIpId == nil { - _, _, err := client.PublicIpApi.DeletePublicIp(ctx).DeletePublicIpRequest(osc.DeletePublicIpRequest{ PublicIpId: publicIP.PublicIpId }).Execute() + _, _, err := client.PublicIpApi.DeletePublicIp(ctx).DeletePublicIpRequest(osc.DeletePublicIpRequest{PublicIpId: publicIP.PublicIpId}).Execute() if err != nil { fmt.Fprintf(os.Stderr, "Error deleting the Public IP \"%s\"\n", *publicIP.PublicIp) return err @@ -347,7 +363,6 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { fmt.Fprintln(os.Stderr, "> No Public IPs found") } - // Delete Subnets for _, subnetID := range subnetsIDs { _, _, err := client.SubnetApi.DeleteSubnet(ctx).DeleteSubnetRequest(*osc.NewDeleteSubnetRequest(subnetID)).Execute() @@ -357,7 +372,9 @@ func Teardown(netID string, client *osc.APIClient, ctx context.Context) error { } fmt.Fprintf(os.Stderr, "> Subnet \"%s\" deleted\n", subnetID) } - if len(subnetsIDs) == 0 { fmt.Fprintln(os.Stderr, "> No Subnets found") } + if len(subnetsIDs) == 0 { + fmt.Fprintln(os.Stderr, "> No Subnets found") + } // Delete Net _, _, err = client.NetApi.DeleteNet(ctx).DeleteNetRequest(*osc.NewDeleteNetRequest(netID)).Execute() @@ -388,8 +405,8 @@ func getSubnetsIDs(netID string, client *osc.APIClient, ctx context.Context) ([] } func getVMsFromIDs(vmIDs []string, client *osc.APIClient, ctx context.Context) (osc.ReadVmsResponse, error) { - filterVms := osc.FiltersVm{ VmIds: &vmIDs } - vms, _, err := client.VmApi.ReadVms(ctx).ReadVmsRequest(osc.ReadVmsRequest{ Filters: &filterVms }).Execute() + filterVms := osc.FiltersVm{VmIds: &vmIDs} + vms, _, err := client.VmApi.ReadVms(ctx).ReadVmsRequest(osc.ReadVmsRequest{Filters: &filterVms}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the VMs") return vms, err @@ -399,8 +416,8 @@ func getVMsFromIDs(vmIDs []string, client *osc.APIClient, ctx context.Context) ( } func getVMs(netID string, client *osc.APIClient, ctx context.Context) (osc.ReadVmsResponse, error) { - filterVms := osc.FiltersVm{ NetIds: &[]string{netID} } - vms, _, err := client.VmApi.ReadVms(ctx).ReadVmsRequest(osc.ReadVmsRequest{ Filters: &filterVms }).Execute() + filterVms := osc.FiltersVm{NetIds: &[]string{netID}} + vms, _, err := client.VmApi.ReadVms(ctx).ReadVmsRequest(osc.ReadVmsRequest{Filters: &filterVms}).Execute() if err != nil { fmt.Fprintln(os.Stderr, "Error getting the VMs") return vms, err @@ -459,7 +476,9 @@ func teardownNICs(vmIDs []string, publicIPs *[]string, nics osc.ReadNicsResponse } } - if len(nicsRest) == 0 { return nil } + if len(nicsRest) == 0 { + return nil + } fmt.Fprintln(os.Stderr, "> Waiting for VMs to be terminated to delete NICs...") for true { @@ -473,7 +492,9 @@ func teardownNICs(vmIDs []string, publicIPs *[]string, nics osc.ReadNicsResponse allTerminated = false } } - if allTerminated { break } + if allTerminated { + break + } sleepTime := rand.Intn(10) + 1 time.Sleep(time.Second * time.Duration(sleepTime)) }