github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/router.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) listRouters(ctx context.Context) ([]cloudResource, error) {
    14  	return o.listRoutersWithFilter(ctx, "items(name),nextPageToken", o.clusterIDFilter(), nil)
    15  }
    16  
    17  // listRoutersWithFilter lists routers 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) listRoutersWithFilter(ctx context.Context, fields string, filter string, filterFunc func(*compute.Router) bool) ([]cloudResource, error) {
    22  	o.Logger.Debug("Listing routers")
    23  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    24  	defer cancel()
    25  	result := []cloudResource{}
    26  	req := o.computeSvc.Routers.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.RouterList) error {
    31  		for _, item := range list.Items {
    32  			if filterFunc == nil || filterFunc != nil && filterFunc(item) {
    33  				o.Logger.Debugf("Found router: %s", item.Name)
    34  				result = append(result, cloudResource{
    35  					key:      item.Name,
    36  					name:     item.Name,
    37  					typeName: "router",
    38  					quota: []gcp.QuotaUsage{{
    39  						Metric: &gcp.Metric{
    40  							Service: gcp.ServiceComputeEngineAPI,
    41  							Limit:   "routers",
    42  						},
    43  						Amount: 1,
    44  					}},
    45  				})
    46  			}
    47  		}
    48  		return nil
    49  	})
    50  	if err != nil {
    51  		return nil, errors.Wrap(err, "failed to list routers")
    52  	}
    53  	return result, nil
    54  }
    55  
    56  func (o *ClusterUninstaller) deleteRouter(ctx context.Context, item cloudResource) error {
    57  	o.Logger.Debugf("Deleting router %s", item.name)
    58  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    59  	defer cancel()
    60  	op, err := o.computeSvc.Routers.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 router %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 router %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 router %s", item.name)
    73  	}
    74  	return nil
    75  }
    76  
    77  // destroyRouters removes all router resources that have a name prefixed
    78  // with the cluster's infra ID.
    79  func (o *ClusterUninstaller) destroyRouters(ctx context.Context) error {
    80  	found, err := o.listRouters(ctx)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	items := o.insertPendingItems("router", found)
    85  	for _, item := range items {
    86  		err := o.deleteRouter(ctx, item)
    87  		if err != nil {
    88  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    89  		}
    90  	}
    91  	if items = o.getPendingItems("router"); len(items) > 0 {
    92  		return errors.Errorf("%d items pending", len(items))
    93  	}
    94  	return nil
    95  }