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

     1  package gcp
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"google.golang.org/api/file/v1"
     8  
     9  	gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
    10  )
    11  
    12  func (o *ClusterUninstaller) filestoreParentPath() string {
    13  	return fmt.Sprintf("projects/%s/locations/-", o.ProjectID)
    14  }
    15  
    16  func (o *ClusterUninstaller) clusterFilestoreLabelFilter() string {
    17  	return fmt.Sprintf("labels.%s = \"owned\"", fmt.Sprintf(gcpconsts.ClusterIDLabelFmt, o.ClusterID))
    18  }
    19  
    20  func (o *ClusterUninstaller) listFilestores(ctx context.Context) ([]cloudResource, error) {
    21  	o.Logger.Debug("Listing filestores")
    22  	result := []cloudResource{}
    23  
    24  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    25  	defer cancel()
    26  	instSvc := file.NewProjectsLocationsInstancesService(o.fileSvc)
    27  	lCall := instSvc.List(o.filestoreParentPath()).Context(ctx).Filter(o.clusterFilestoreLabelFilter())
    28  
    29  	nextPageToken := "pageToken"
    30  	for nextPageToken != "" {
    31  		instances, err := lCall.Do()
    32  		if err != nil {
    33  			return nil, fmt.Errorf("error retrieving filestore instances: %w", err)
    34  		}
    35  
    36  		for _, activeInstance := range instances.Instances {
    37  			o.Logger.Debugf("Found filestore %s", activeInstance.Name)
    38  			result = append(result, cloudResource{
    39  				name:     activeInstance.Name,
    40  				typeName: "filestore",
    41  			})
    42  		}
    43  
    44  		nextPageToken = instances.NextPageToken
    45  		lCall.PageToken(nextPageToken)
    46  	}
    47  	return result, nil
    48  }
    49  
    50  func (o *ClusterUninstaller) deleteFilestore(ctx context.Context, item cloudResource) error {
    51  	o.Logger.Debugf("Deleting filestore %s", item.name)
    52  
    53  	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
    54  	defer cancel()
    55  	instSvc := file.NewProjectsLocationsInstancesService(o.fileSvc)
    56  	_, err := instSvc.Delete(item.name).Context(ctx).Do()
    57  	if err != nil && isForbidden(err) {
    58  		o.deletePendingItems(item.typeName, []cloudResource{item})
    59  		return fmt.Errorf("insufficient permissions to delete filestore %s", item.name)
    60  	}
    61  	if err != nil && !isNoOp(err) {
    62  		o.resetRequestID(item.typeName, item.name)
    63  		return fmt.Errorf("failed to delete filestore %s", item.name)
    64  	}
    65  	if err != nil && isNoOp(err) {
    66  		o.resetRequestID(item.typeName, item.name)
    67  		o.deletePendingItems(item.typeName, []cloudResource{item})
    68  		o.Logger.Infof("Deleted filestore %s", item.name)
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (o *ClusterUninstaller) destroyFilestores(ctx context.Context) error {
    75  	found, err := o.listFilestores(ctx)
    76  	if err != nil {
    77  		if isForbidden(err) {
    78  			o.Logger.Warning("Skipping deletion of filestores: insufficient Filestore API permissions or API disabled")
    79  			return nil
    80  		}
    81  		return err
    82  	}
    83  	items := o.insertPendingItems("filestore", found)
    84  	for _, item := range items {
    85  		err := o.deleteFilestore(ctx, item)
    86  		if err != nil {
    87  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    88  		}
    89  	}
    90  	if items = o.getPendingItems("filestore"); len(items) > 0 {
    91  		return fmt.Errorf("%d items pending", len(items))
    92  	}
    93  
    94  	return nil
    95  }