Skip to content

Commit

Permalink
Format example file
Browse files Browse the repository at this point in the history
  • Loading branch information
ryohkhn committed Jun 17, 2024
1 parent 8b2498b commit a8e24cd
Showing 1 changed file with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions examples/teardown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

/*
Teardown example
Teardown example
- Delete VMs
- Delete Load Balancers
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -302,19 +314,21 @@ 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
}
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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down

0 comments on commit a8e24cd

Please sign in to comment.