github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/publicgateway.go (about)

     1  package ibmcloud
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/IBM/vpc-go-sdk/vpcv1"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  const publicGatewayTypeName = "public gateway"
    12  
    13  // listPublicGateways lists public gateways in the vpc
    14  func (o *ClusterUninstaller) listPublicGateways() (cloudResources, error) {
    15  	o.Logger.Debugf("Listing public gateways")
    16  	ctx, cancel := o.contextWithTimeout()
    17  	defer cancel()
    18  
    19  	options := o.vpcSvc.NewListPublicGatewaysOptions()
    20  	resources, _, err := o.vpcSvc.ListPublicGatewaysWithContext(ctx, options)
    21  	if err != nil {
    22  		return nil, errors.Wrapf(err, "failed to list public gateways")
    23  	}
    24  
    25  	result := []cloudResource{}
    26  	for _, publicGateway := range resources.PublicGateways {
    27  		if strings.Contains(*publicGateway.Name, o.InfraID) {
    28  			result = append(result, cloudResource{
    29  				key:      *publicGateway.ID,
    30  				name:     *publicGateway.Name,
    31  				status:   *publicGateway.Status,
    32  				typeName: publicGatewayTypeName,
    33  				id:       *publicGateway.ID,
    34  			})
    35  		}
    36  	}
    37  
    38  	return cloudResources{}.insert(result...), nil
    39  }
    40  
    41  func (o *ClusterUninstaller) deletePublicGateway(item cloudResource) error {
    42  	if item.status == vpcv1.PublicGatewayStatusDeletingConst {
    43  		o.Logger.Debugf("Waiting for public gateway %q to delete", item.name)
    44  		return nil
    45  	}
    46  
    47  	o.Logger.Debugf("Deleting public gateway %q", item.name)
    48  	ctx, cancel := o.contextWithTimeout()
    49  	defer cancel()
    50  
    51  	options := o.vpcSvc.NewDeletePublicGatewayOptions(item.id)
    52  	details, err := o.vpcSvc.DeletePublicGatewayWithContext(ctx, options)
    53  
    54  	if err != nil && details != nil && details.StatusCode == http.StatusNotFound {
    55  		// The resource is gone
    56  		o.deletePendingItems(item.typeName, []cloudResource{item})
    57  		o.Logger.Infof("Deleted public gateway %q", item.name)
    58  		return nil
    59  	}
    60  
    61  	if err != nil && details != nil && details.StatusCode != http.StatusNotFound {
    62  		return errors.Wrapf(err, "Failed to delete public gateway %s", item.name)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // destroyPublicGateways removes all public gateway resources that have a name prefixed
    69  // with the cluster's infra ID.
    70  func (o *ClusterUninstaller) destroyPublicGateways() error {
    71  	if len(o.UserProvidedSubnets) > 0 {
    72  		o.Logger.Info("Skipping deletion of public gateways in case of user-provided subnets")
    73  		return nil
    74  	}
    75  
    76  	found, err := o.listPublicGateways()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	items := o.insertPendingItems(publicGatewayTypeName, found.list())
    82  	for _, item := range items {
    83  		if _, ok := found[item.key]; !ok {
    84  			// This item has finished deletion.
    85  			o.deletePendingItems(item.typeName, []cloudResource{item})
    86  			o.Logger.Infof("Deleted public gateway %q", item.name)
    87  			continue
    88  		}
    89  
    90  		err := o.deletePublicGateway(item)
    91  		if err != nil {
    92  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    93  		}
    94  	}
    95  
    96  	if items = o.getPendingItems(publicGatewayTypeName); len(items) > 0 {
    97  		return errors.Errorf("%d items pending", len(items))
    98  	}
    99  	return nil
   100  }