github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/targetTCPProxies.go (about)

     1  package gcp
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     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) listTargetTCPProxies(ctx context.Context) ([]cloudResource, error) {
    14  	return o.listTargetTCPProxiesWithFilter(ctx, "items(name),nextPageToken", o.clusterIDFilter(), nil)
    15  }
    16  
    17  // listTargetTCPProxiesWithFilter lists target TCP Proxies in the project that satisfy the filter criteria.
    18  func (o *ClusterUninstaller) listTargetTCPProxiesWithFilter(ctx context.Context, fields string, filter string, filterFunc func(list *compute.TargetTcpProxy) bool) ([]cloudResource, error) {
    19  	o.Logger.Debugf("Listing target tcp proxies")
    20  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    21  	defer cancel()
    22  	result := []cloudResource{}
    23  	req := o.computeSvc.TargetTcpProxies.List(o.ProjectID).Fields(googleapi.Field(fields))
    24  	if len(filter) > 0 {
    25  		req = req.Filter(filter)
    26  	}
    27  	err := req.Pages(ctx, func(list *compute.TargetTcpProxyList) error {
    28  		for _, item := range list.Items {
    29  			if filterFunc == nil || (filterFunc != nil && filterFunc(item)) {
    30  				o.Logger.Debugf("Found target TCP proxy: %s", item.Name)
    31  				result = append(result, cloudResource{
    32  					key:      item.Name,
    33  					name:     item.Name,
    34  					typeName: "targettcpproxy",
    35  					quota: []gcp.QuotaUsage{{
    36  						Metric: &gcp.Metric{
    37  							Service: gcp.ServiceComputeEngineAPI,
    38  							Limit:   "target_tcp_proxy",
    39  						},
    40  						Amount: 1,
    41  					}},
    42  				})
    43  			}
    44  		}
    45  		return nil
    46  	})
    47  	if err != nil {
    48  		return nil, fmt.Errorf("failed to list target tcp proxies: %w", err)
    49  	}
    50  	return result, nil
    51  }
    52  
    53  func (o *ClusterUninstaller) deleteTargetTCPProxy(ctx context.Context, item cloudResource) error {
    54  	o.Logger.Debugf("Deleting target TCP Proxies %s", item.name)
    55  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    56  	defer cancel()
    57  	op, err := o.computeSvc.TargetTcpProxies.Delete(o.ProjectID, item.name).RequestId(o.requestID(item.typeName, item.name)).Context(ctx).Do()
    58  	if err != nil && !isNoOp(err) {
    59  		o.resetRequestID(item.typeName, item.name)
    60  		return fmt.Errorf("failed to delete target TCP proxy %s: %w", item.name, err)
    61  	}
    62  	if op != nil && op.Status == "DONE" && isErrorStatus(op.HttpErrorStatusCode) {
    63  		o.resetRequestID(item.typeName, item.name)
    64  		return fmt.Errorf("failed to delete target TCP proxy %s with error: %s: %w", item.name, operationErrorMessage(op), err)
    65  	}
    66  	if (err != nil && isNoOp(err)) || (op != nil && op.Status == "DONE") {
    67  		o.resetRequestID(item.typeName, item.name)
    68  		o.deletePendingItems(item.typeName, []cloudResource{item})
    69  		o.Logger.Infof("Deleted target TCP proxy %s", item.name)
    70  	}
    71  	return nil
    72  }
    73  
    74  // destroyTargetTCPProxies removes target tcp proxies with a name prefixed
    75  // with the cluster's infra ID.
    76  func (o *ClusterUninstaller) destroyTargetTCPProxies(ctx context.Context) error {
    77  	found, err := o.listTargetTCPProxies(ctx)
    78  	if err != nil {
    79  		return fmt.Errorf("failed to list target TCP proxies: %w", err)
    80  	}
    81  	items := o.insertPendingItems("targettcpproxy", found)
    82  	for _, item := range items {
    83  		err := o.deleteTargetTCPProxy(ctx, item)
    84  		if err != nil {
    85  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    86  		}
    87  	}
    88  	if items = o.getPendingItems("targettcpproxy"); len(items) > 0 {
    89  		return fmt.Errorf("%d items pending", len(items))
    90  	}
    91  	return nil
    92  }