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

     1  package deprovisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/common/hyperscaler"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    11  
    12  	"github.com/sirupsen/logrus"
    13  
    14  	"github.com/kyma-project/kyma-environment-broker/internal"
    15  )
    16  
    17  type ReleaseSubscriptionStep struct {
    18  	operationManager *process.OperationManager
    19  	instanceStorage  storage.Instances
    20  	accountProvider  hyperscaler.AccountProvider
    21  }
    22  
    23  var _ process.Step = &ReleaseSubscriptionStep{}
    24  
    25  func NewReleaseSubscriptionStep(os storage.Operations, instanceStorage storage.Instances, accountProvider hyperscaler.AccountProvider) ReleaseSubscriptionStep {
    26  	return ReleaseSubscriptionStep{
    27  		operationManager: process.NewOperationManager(os),
    28  		instanceStorage:  instanceStorage,
    29  		accountProvider:  accountProvider,
    30  	}
    31  }
    32  
    33  func (s ReleaseSubscriptionStep) Name() string {
    34  	return "Release_Subscription"
    35  }
    36  
    37  func (s ReleaseSubscriptionStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    38  
    39  	planID := operation.ProvisioningParameters.PlanID
    40  	if !broker.IsTrialPlan(planID) && !broker.IsOwnClusterPlan(planID) {
    41  		instance, err := s.instanceStorage.GetByID(operation.InstanceID)
    42  		if err != nil {
    43  			msg := fmt.Sprintf("after successful deprovisioning failing to release hyperscaler subscription - get the instance data for instanceID [%s]: %s", operation.InstanceID, err.Error())
    44  			operation, repeat, err := s.operationManager.MarkStepAsExcutedButNotCompleted(operation, s.Name(), msg, log)
    45  			if repeat != 0 {
    46  				return operation, repeat, err
    47  			}
    48  			return operation, 0, nil
    49  		}
    50  
    51  		if string(instance.Provider) == "" {
    52  			log.Info("Instance does not contain cloud provider info due to failed provisioning, skipping")
    53  			return operation, 0, nil
    54  		}
    55  
    56  		hypType, err := hyperscaler.FromCloudProvider(instance.Provider)
    57  		if err != nil {
    58  			msg := fmt.Sprintf("after successful deprovisioning failing to release hyperscaler subscription - determine the type of hyperscaler to use for planID [%s]: %s", planID, err.Error())
    59  			operation, repeat, err := s.operationManager.MarkStepAsExcutedButNotCompleted(operation, s.Name(), msg, log)
    60  			if repeat != 0 {
    61  				return operation, repeat, err
    62  			}
    63  			return operation, 0, nil
    64  		}
    65  
    66  		euAccess := internal.IsEuAccess(operation.ProvisioningParameters.PlatformRegion)
    67  		err = s.accountProvider.MarkUnusedGardenerSecretBindingAsDirty(hypType, instance.GetSubscriptionGlobalAccoundID(), euAccess)
    68  		if err != nil {
    69  			log.Errorf("after successful deprovisioning failed to release hyperscaler subscription: %s", err)
    70  			return operation, 10 * time.Second, nil
    71  		}
    72  	}
    73  	return operation, 0, nil
    74  }