github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/targetpool.go (about) 1 package gcp 2 3 import ( 4 "context" 5 6 "github.com/pkg/errors" 7 "google.golang.org/api/compute/v1" 8 "google.golang.org/api/googleapi" 9 10 "github.com/openshift/installer/pkg/types/gcp" 11 ) 12 13 func (o *ClusterUninstaller) listTargetPools(ctx context.Context) ([]cloudResource, error) { 14 return o.listTargetPoolsWithFilter(ctx, "items(name),nextPageToken", o.clusterIDFilter(), nil) 15 } 16 17 // listTargetPoolsWithFilter lists target pools in the project that satisfy the filter criteria. 18 // The fields parameter specifies which fields should be returned in the result, the filter string contains 19 // a filter string passed to the API to filter results. The filterFunc is a client-side filtering function 20 // that determines whether a particular result should be returned or not. 21 func (o *ClusterUninstaller) listTargetPoolsWithFilter(ctx context.Context, fields string, filter string, filterFunc func(*compute.TargetPool) bool) ([]cloudResource, error) { 22 o.Logger.Debugf("Listing target pools") 23 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 24 defer cancel() 25 result := []cloudResource{} 26 req := o.computeSvc.TargetPools.List(o.ProjectID, o.Region).Fields(googleapi.Field(fields)) 27 if len(filter) > 0 { 28 req = req.Filter(filter) 29 } 30 err := req.Pages(ctx, func(list *compute.TargetPoolList) error { 31 for _, item := range list.Items { 32 if filterFunc == nil || filterFunc != nil && filterFunc(item) { 33 o.Logger.Debugf("Found target pool: %s", item.Name) 34 result = append(result, cloudResource{ 35 key: item.Name, 36 name: item.Name, 37 typeName: "targetpool", 38 quota: []gcp.QuotaUsage{{ 39 Metric: &gcp.Metric{ 40 Service: gcp.ServiceComputeEngineAPI, 41 Limit: "target_pools", 42 }, 43 Amount: 1, 44 }}, 45 }) 46 } 47 } 48 return nil 49 }) 50 if err != nil { 51 return nil, errors.Wrapf(err, "failed to list target pools") 52 } 53 return result, nil 54 } 55 56 func (o *ClusterUninstaller) deleteTargetPool(ctx context.Context, item cloudResource) error { 57 o.Logger.Debugf("Deleting target pool %s", item.name) 58 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 59 defer cancel() 60 op, err := o.computeSvc.TargetPools.Delete(o.ProjectID, o.Region, item.name).RequestId(o.requestID(item.typeName, item.name)).Context(ctx).Do() 61 if err != nil && !isNoOp(err) { 62 o.resetRequestID(item.typeName, item.name) 63 return errors.Wrapf(err, "failed to delete target pool %s", item.name) 64 } 65 if op != nil && op.Status == "DONE" && isErrorStatus(op.HttpErrorStatusCode) { 66 o.resetRequestID(item.typeName, item.name) 67 return errors.Errorf("failed to delete target pool %s with error: %s", item.name, operationErrorMessage(op)) 68 } 69 if (err != nil && isNoOp(err)) || (op != nil && op.Status == "DONE") { 70 o.resetRequestID(item.typeName, item.name) 71 o.deletePendingItems(item.typeName, []cloudResource{item}) 72 o.Logger.Infof("Deleted target pool %s", item.name) 73 } 74 return nil 75 } 76 77 // destroyTargetPools removes target pools resources that have a name prefixed 78 // with the cluster's infra ID. 79 func (o *ClusterUninstaller) destroyTargetPools(ctx context.Context) error { 80 found, err := o.listTargetPools(ctx) 81 if err != nil { 82 return err 83 } 84 items := o.insertPendingItems("targetpool", found) 85 for _, item := range items { 86 err := o.deleteTargetPool(ctx, item) 87 if err != nil { 88 o.errorTracker.suppressWarning(item.key, err, o.Logger) 89 } 90 } 91 if items = o.getPendingItems("targetpool"); len(items) > 0 { 92 return errors.Errorf("%d items pending", len(items)) 93 } 94 return nil 95 }