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

     1  package provisioning
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
     7  	"github.com/kyma-project/kyma-environment-broker/internal/process"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
    10  	"github.com/pivotal-cf/brokerapi/v8/domain"
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/kyma-project/kyma-environment-broker/internal"
    14  )
    15  
    16  // StartStep changes the state from pending to in progress if necessary
    17  type StartStep struct {
    18  	operationStorage storage.Operations
    19  	instanceStorage  storage.Instances
    20  	operationManager *process.OperationManager
    21  }
    22  
    23  func NewStartStep(os storage.Operations, is storage.Instances) *StartStep {
    24  	return &StartStep{
    25  		operationStorage: os,
    26  		instanceStorage:  is,
    27  		operationManager: process.NewOperationManager(os),
    28  	}
    29  }
    30  
    31  func (s *StartStep) Name() string {
    32  	return "Starting"
    33  }
    34  
    35  func (s *StartStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    36  	if operation.State != orchestration.Pending {
    37  		return operation, 0, nil
    38  	}
    39  
    40  	deprovisionOp, err := s.operationStorage.GetDeprovisioningOperationByInstanceID(operation.InstanceID)
    41  	if err != nil && !dberr.IsNotFound(err) {
    42  		log.Errorf("Unable to get deprovisioning operation: %s", err.Error())
    43  		return operation, time.Second, nil
    44  	}
    45  	if deprovisionOp != nil && deprovisionOp.State == domain.InProgress {
    46  		return operation, time.Minute, nil
    47  	}
    48  
    49  	// if there was a deprovisioning process before, take new InstanceDetails
    50  	if deprovisionOp != nil {
    51  		inst, err := s.instanceStorage.GetByID(operation.InstanceID)
    52  		if err != nil {
    53  			if dberr.IsNotFound(err) {
    54  				log.Errorf("Instance does not exists.")
    55  				return s.operationManager.OperationFailed(operation, "The instance does not exists", err, log)
    56  			}
    57  			log.Errorf("Unable to get the instance: %s", err.Error())
    58  			return operation, time.Second, nil
    59  		}
    60  		log.Infof("Setting the newest InstanceDetails")
    61  		operation.InstanceDetails, err = inst.GetInstanceDetails()
    62  		if err != nil {
    63  			log.Errorf("Unable to provide Instance details: %s", err.Error())
    64  			return s.operationManager.OperationFailed(operation, "Unable to provide Instance details", err, log)
    65  		}
    66  	}
    67  	lastOp, err := s.operationStorage.GetLastOperation(operation.InstanceID)
    68  	if err != nil && !dberr.IsNotFound(err) {
    69  		log.Warn("Failed to get last operation for ERSContext:", err)
    70  		return operation, time.Minute, nil
    71  	}
    72  	log.Infof("Setting the operation to 'InProgress'")
    73  	newOp, retry, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    74  		if lastOp != nil {
    75  			op.ProvisioningParameters.ErsContext = internal.InheritMissingERSContext(op.ProvisioningParameters.ErsContext, lastOp.ProvisioningParameters.ErsContext)
    76  		}
    77  		op.State = domain.InProgress
    78  	}, log)
    79  	operation = newOp
    80  	if retry > 0 {
    81  		return operation, time.Second, nil
    82  	}
    83  
    84  	return operation, 0, nil
    85  }