github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/vpc.go (about) 1 package ibmcloud 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/IBM/vpc-go-sdk/vpcv1" 8 "github.com/pkg/errors" 9 ) 10 11 const vpcTypeName = "vpc" 12 13 // listVPCs lists VPCs 14 func (o *ClusterUninstaller) listVPCs() (cloudResources, error) { 15 o.Logger.Debugf("Listing VPCs") 16 ctx, cancel := o.contextWithTimeout() 17 defer cancel() 18 19 options := o.vpcSvc.NewListVpcsOptions() 20 resources, _, err := o.vpcSvc.ListVpcsWithContext(ctx, options) 21 if err != nil { 22 return nil, errors.Wrapf(err, "failed to list vpcs") 23 } 24 25 result := []cloudResource{} 26 for _, vpc := range resources.Vpcs { 27 if strings.Contains(*vpc.Name, o.InfraID) { 28 result = append(result, cloudResource{ 29 key: *vpc.ID, 30 name: *vpc.Name, 31 status: *vpc.Status, 32 typeName: vpcTypeName, 33 id: *vpc.ID, 34 }) 35 } 36 } 37 38 return cloudResources{}.insert(result...), nil 39 } 40 41 func (o *ClusterUninstaller) deleteVPC(item cloudResource) error { 42 if item.status == vpcv1.VPCStatusDeletingConst { 43 o.Logger.Debugf("Waiting for VPC %q to delete", item.name) 44 return nil 45 } 46 47 o.Logger.Debugf("Deleting VPC %q", item.name) 48 ctx, cancel := o.contextWithTimeout() 49 defer cancel() 50 51 options := o.vpcSvc.NewDeleteVPCOptions(item.id) 52 details, err := o.vpcSvc.DeleteVPCWithContext(ctx, options) 53 54 if err != nil && details != nil && details.StatusCode == http.StatusNotFound { 55 // The resource is gone 56 o.deletePendingItems(item.typeName, []cloudResource{item}) 57 o.Logger.Infof("Deleted VPC %q", item.name) 58 return nil 59 } 60 61 if err != nil && details != nil && details.StatusCode != http.StatusNotFound { 62 return errors.Wrapf(err, "Failed to delete VOC %s", item.name) 63 } 64 65 return nil 66 } 67 68 // listVPCs removes all VPC resources that have a name prefixed 69 // with the cluster's infra ID. 70 func (o *ClusterUninstaller) destroyVPCs() error { 71 if o.UserProvidedVPC != "" { 72 o.Logger.Infof("Skipping deletion of user-provided VPC %q", o.UserProvidedVPC) 73 return nil 74 } 75 76 found, err := o.listVPCs() 77 if err != nil { 78 return err 79 } 80 81 items := o.insertPendingItems(vpcTypeName, found.list()) 82 for _, item := range items { 83 if _, ok := found[item.key]; !ok { 84 // This item has finished deletion. 85 o.deletePendingItems(item.typeName, []cloudResource{item}) 86 o.Logger.Infof("Deleted VPC %q", item.name) 87 continue 88 } 89 err := o.deleteVPC(item) 90 if err != nil { 91 o.errorTracker.suppressWarning(item.key, err, o.Logger) 92 } 93 } 94 95 if items = o.getPendingItems(vpcTypeName); len(items) > 0 { 96 return errors.Errorf("%d items pending", len(items)) 97 } 98 return nil 99 }