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

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