github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/edp_deregistration.go (about)

     1  package deprovisioning
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/edp"
    10  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  //go:generate mockery --name=EDPClient --output=automock --outpkg=automock --case=underscore
    18  type EDPClient interface {
    19  	DeleteDataTenant(name, env string) error
    20  	DeleteMetadataTenant(name, env, key string) error
    21  }
    22  
    23  type EDPDeregistrationStep struct {
    24  	operationManager *process.OperationManager
    25  	client           EDPClient
    26  	config           edp.Config
    27  }
    28  
    29  func NewEDPDeregistrationStep(os storage.Operations, client EDPClient, config edp.Config) *EDPDeregistrationStep {
    30  	return &EDPDeregistrationStep{
    31  		operationManager: process.NewOperationManager(os),
    32  		client:           client,
    33  		config:           config,
    34  	}
    35  }
    36  
    37  func (s *EDPDeregistrationStep) Name() string {
    38  	return "EDP_Deregistration"
    39  }
    40  
    41  func (s *EDPDeregistrationStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    42  	log.Info("Delete DataTenant metadata")
    43  
    44  	subAccountID := strings.ToLower(operation.SubAccountID)
    45  	for _, key := range []string{
    46  		edp.MaasConsumerEnvironmentKey,
    47  		edp.MaasConsumerRegionKey,
    48  		edp.MaasConsumerSubAccountKey,
    49  		edp.MaasConsumerServicePlan,
    50  	} {
    51  		err := s.client.DeleteMetadataTenant(subAccountID, s.config.Environment, key)
    52  		if err != nil {
    53  			return s.handleError(operation, err, log, fmt.Sprintf("cannot remove DataTenant metadata with key: %s", key))
    54  		}
    55  	}
    56  
    57  	log.Info("Delete DataTenant")
    58  	err := s.client.DeleteDataTenant(subAccountID, s.config.Environment)
    59  	if err != nil {
    60  		return s.handleError(operation, err, log, "cannot remove DataTenant")
    61  	}
    62  
    63  	return operation, 0, nil
    64  }
    65  
    66  func (s *EDPDeregistrationStep) handleError(operation internal.Operation, err error, log logrus.FieldLogger, msg string) (internal.Operation, time.Duration, error) {
    67  	log.Errorf("%s: %s", msg, err)
    68  
    69  	if kebError.IsTemporaryError(err) {
    70  		since := time.Since(operation.UpdatedAt)
    71  		if since < time.Minute*30 {
    72  			log.Errorf("request to EDP failed: %s. Retry...", err)
    73  			return operation, 10 * time.Second, nil
    74  		}
    75  	}
    76  
    77  	errMsg := fmt.Sprintf("Step %s failed. EDP data have not been deleted.", s.Name())
    78  	operation, repeat, err := s.operationManager.MarkStepAsExcutedButNotCompleted(operation, s.Name(), errMsg, log)
    79  	if repeat != 0 {
    80  		return operation, repeat, err
    81  	}
    82  	return operation, 0, nil
    83  }