github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/floatingip.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 floatingIPTypeName = "floating ip"
    12  
    13  // listFloatingIPs lists floating IPs in the vpc
    14  func (o *ClusterUninstaller) listFloatingIPs() (cloudResources, error) {
    15  	o.Logger.Debugf("Listing floating IPs")
    16  	ctx, cancel := o.contextWithTimeout()
    17  	defer cancel()
    18  
    19  	options := o.vpcSvc.NewListFloatingIpsOptions()
    20  	resources, _, err := o.vpcSvc.ListFloatingIpsWithContext(ctx, options)
    21  
    22  	if err != nil {
    23  		return nil, errors.Wrapf(err, "failed to list floating IPs")
    24  	}
    25  
    26  	result := []cloudResource{}
    27  	for _, floatingIPs := range resources.FloatingIps {
    28  		if strings.Contains(*floatingIPs.Name, o.InfraID) {
    29  			result = append(result, cloudResource{
    30  				key:      *floatingIPs.ID,
    31  				name:     *floatingIPs.Name,
    32  				status:   *floatingIPs.Status,
    33  				typeName: floatingIPTypeName,
    34  				id:       *floatingIPs.ID,
    35  			})
    36  		}
    37  	}
    38  
    39  	return cloudResources{}.insert(result...), nil
    40  }
    41  
    42  func (o *ClusterUninstaller) deleteFloatingIP(item cloudResource) error {
    43  	if item.status == vpcv1.FloatingIPStatusDeletingConst {
    44  		o.Logger.Debugf("Waiting for floating IP %q to delete", item.name)
    45  		return nil
    46  	}
    47  
    48  	o.Logger.Debugf("Deleting floating IP %q", item.name)
    49  	ctx, cancel := o.contextWithTimeout()
    50  	defer cancel()
    51  
    52  	options := o.vpcSvc.NewDeleteFloatingIPOptions(item.id)
    53  	details, err := o.vpcSvc.DeleteFloatingIPWithContext(ctx, options)
    54  
    55  	if err != nil && details != nil && details.StatusCode == http.StatusNotFound {
    56  		// The resource is gone
    57  		o.deletePendingItems(item.typeName, []cloudResource{item})
    58  		o.Logger.Infof("Deleted floating IP %q", item.name)
    59  		return nil
    60  	}
    61  
    62  	if err != nil && details != nil && details.StatusCode != http.StatusNotFound {
    63  		return errors.Wrapf(err, "Failed to delete floating IP %s", item.name)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  // destroyFloatingIPs removes all floating IP resources that have a name prefixed
    70  // with the cluster's infra ID.
    71  func (o *ClusterUninstaller) destroyFloatingIPs() error {
    72  	found, err := o.listFloatingIPs()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	items := o.insertPendingItems(floatingIPTypeName, found.list())
    78  
    79  	for _, item := range items {
    80  		if _, ok := found[item.key]; !ok {
    81  			// This item has finished deletion.
    82  			o.deletePendingItems(item.typeName, []cloudResource{item})
    83  			o.Logger.Infof("Deleted floating IP %q", item.name)
    84  			continue
    85  		}
    86  		err = o.deleteFloatingIP(item)
    87  		if err != nil {
    88  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    89  		}
    90  	}
    91  
    92  	if items = o.getPendingItems(floatingIPTypeName); len(items) > 0 {
    93  		return errors.Errorf("%d items pending", len(items))
    94  	}
    95  	return nil
    96  }