github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/subnet.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 subnetTypeName = "subnet" 12 13 // listSubnets lists subnets in the vpc 14 func (o *ClusterUninstaller) listSubnets() (cloudResources, error) { 15 o.Logger.Debugf("Listing subnets") 16 ctx, cancel := o.contextWithTimeout() 17 defer cancel() 18 19 options := o.vpcSvc.NewListSubnetsOptions() 20 resources, _, err := o.vpcSvc.ListSubnetsWithContext(ctx, options) 21 if err != nil { 22 return nil, errors.Wrapf(err, "failed to list subnets") 23 } 24 25 result := []cloudResource{} 26 for _, subnet := range resources.Subnets { 27 if strings.Contains(*subnet.Name, o.InfraID) { 28 result = append(result, cloudResource{ 29 key: *subnet.ID, 30 name: *subnet.Name, 31 status: *subnet.Status, 32 typeName: subnetTypeName, 33 id: *subnet.ID, 34 }) 35 } 36 } 37 38 return cloudResources{}.insert(result...), nil 39 } 40 41 func (o *ClusterUninstaller) deleteSubnet(item cloudResource) error { 42 if item.status == vpcv1.SubnetStatusDeletingConst { 43 o.Logger.Debugf("Waiting for subnet %q to delete", item.name) 44 return nil 45 } 46 47 o.Logger.Debugf("Deleting subnet %q", item.name) 48 ctx, cancel := o.contextWithTimeout() 49 defer cancel() 50 51 options := o.vpcSvc.NewDeleteSubnetOptions(item.id) 52 details, err := o.vpcSvc.DeleteSubnetWithContext(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 subnet %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 subnet %s", item.name) 63 } 64 65 return nil 66 } 67 68 // destroySubnets removes all subnet resources that have a name prefixed 69 // with the cluster's infra ID. 70 func (o *ClusterUninstaller) destroySubnets() error { 71 if len(o.UserProvidedSubnets) > 0 { 72 o.Logger.Infof("Skipping deletion of user-provided subnets %v", o.UserProvidedSubnets) 73 return nil 74 } 75 76 found, err := o.listSubnets() 77 if err != nil { 78 return err 79 } 80 81 items := o.insertPendingItems(subnetTypeName, found.list()) 82 83 for _, item := range items { 84 if _, ok := found[item.key]; !ok { 85 // This item has finished deletion. 86 o.deletePendingItems(item.typeName, []cloudResource{item}) 87 o.Logger.Infof("Deleted subnet %q", item.name) 88 continue 89 } 90 err = o.deleteSubnet(item) 91 if err != nil { 92 o.errorTracker.suppressWarning(item.key, err, o.Logger) 93 } 94 } 95 96 if items = o.getPendingItems(subnetTypeName); len(items) > 0 { 97 return errors.Errorf("%d items pending", len(items)) 98 } 99 return nil 100 }