github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/resourcegroup.go (about) 1 package ibmcloud 2 3 import ( 4 "net/http" 5 6 "github.com/pkg/errors" 7 ) 8 9 const resourceGroupTypeName = "resource group" 10 11 // listResourceGroups lists resource groups in the account 12 func (o *ClusterUninstaller) listResourceGroups() (cloudResources, error) { 13 o.Logger.Debugf("Listing resource groups") 14 ctx, cancel := o.contextWithTimeout() 15 defer cancel() 16 17 options := o.managementSvc.NewListResourceGroupsOptions() 18 options.SetAccountID(o.AccountID) 19 resources, _, err := o.managementSvc.ListResourceGroupsWithContext(ctx, options) 20 if err != nil { 21 return nil, errors.Wrapf(err, "Failed to list resource groups") 22 } 23 24 result := []cloudResource{} 25 for _, resourceGroup := range resources.Resources { 26 if *resourceGroup.Name == o.ResourceGroupName { 27 result = append(result, cloudResource{ 28 key: *resourceGroup.ID, 29 name: *resourceGroup.Name, 30 status: "", 31 typeName: resourceGroupTypeName, 32 id: *resourceGroup.ID, 33 }) 34 } 35 } 36 37 return cloudResources{}.insert(result...), nil 38 } 39 40 func (o *ClusterUninstaller) deleteResourceGroup(item cloudResource) error { 41 o.Logger.Debugf("Deleting resource group %q", item.name) 42 ctx, cancel := o.contextWithTimeout() 43 defer cancel() 44 45 options := o.managementSvc.NewDeleteResourceGroupOptions(item.id) 46 details, err := o.managementSvc.DeleteResourceGroupWithContext(ctx, options) 47 48 if err != nil && details != nil && details.StatusCode == http.StatusNotFound { 49 // The resource is gone 50 o.deletePendingItems(item.typeName, []cloudResource{item}) 51 o.Logger.Infof("Deleted resource group %q", item.name) 52 return nil 53 } 54 55 if err != nil && details != nil && details.StatusCode != http.StatusNotFound { 56 return errors.Wrapf(err, "Failed to delete resource group %s", item.name) 57 } 58 59 return nil 60 } 61 62 // destroyResourceGroups removes the installer-generated resource group. If the 63 // resource group is user-provided, it will not be removed. 64 func (o *ClusterUninstaller) destroyResourceGroups() error { 65 if o.ResourceGroupName != o.InfraID { 66 o.Logger.Infof("Skipping deletion of user-provided resource group %v", o.ResourceGroupName) 67 return nil 68 } 69 70 found, err := o.listResourceGroups() 71 if err != nil { 72 return err 73 } 74 75 items := o.insertPendingItems(resourceGroupTypeName, found.list()) 76 77 for _, item := range items { 78 if _, ok := found[item.key]; !ok { 79 // This item has finished deletion. 80 o.deletePendingItems(item.typeName, []cloudResource{item}) 81 o.Logger.Infof("Deleted resource group %q", item.name) 82 continue 83 } 84 err = o.deleteResourceGroup(item) 85 if err != nil { 86 o.errorTracker.suppressWarning(item.key, err, o.Logger) 87 } 88 } 89 90 if items = o.getPendingItems(resourceGroupTypeName); len(items) > 0 { 91 return errors.Errorf("%d items pending", len(items)) 92 } 93 return nil 94 }