github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/converter.go (about)

     1  package runtime
     2  
     3  import (
     4  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
     5  	pkg "github.com/kyma-project/kyma-environment-broker/common/runtime"
     6  	"github.com/kyma-project/kyma-environment-broker/internal"
     7  	"github.com/pivotal-cf/brokerapi/v8/domain"
     8  )
     9  
    10  type Converter interface {
    11  	NewDTO(instance internal.Instance) (pkg.RuntimeDTO, error)
    12  	ApplyProvisioningOperation(dto *pkg.RuntimeDTO, pOpr *internal.ProvisioningOperation)
    13  	ApplyDeprovisioningOperation(dto *pkg.RuntimeDTO, dOpr *internal.DeprovisioningOperation)
    14  	ApplyUpgradingKymaOperations(dto *pkg.RuntimeDTO, oprs []internal.UpgradeKymaOperation, totalCount int)
    15  	ApplyUpgradingClusterOperations(dto *pkg.RuntimeDTO, oprs []internal.UpgradeClusterOperation, totalCount int)
    16  	ApplyUpdateOperations(dto *pkg.RuntimeDTO, oprs []internal.UpdatingOperation, totalCount int)
    17  	ApplySuspensionOperations(dto *pkg.RuntimeDTO, oprs []internal.DeprovisioningOperation)
    18  	ApplyUnsuspensionOperations(dto *pkg.RuntimeDTO, oprs []internal.ProvisioningOperation)
    19  }
    20  
    21  type converter struct {
    22  	defaultSubaccountRegion string
    23  }
    24  
    25  func NewConverter(platformRegion string) Converter {
    26  	return &converter{
    27  		defaultSubaccountRegion: platformRegion,
    28  	}
    29  }
    30  
    31  func (c *converter) setRegionOrDefault(instance internal.Instance, runtime *pkg.RuntimeDTO) {
    32  	if instance.Parameters.PlatformRegion == "" {
    33  		runtime.SubAccountRegion = c.defaultSubaccountRegion
    34  	} else {
    35  		runtime.SubAccountRegion = instance.Parameters.PlatformRegion
    36  	}
    37  }
    38  
    39  func (c *converter) ApplyProvisioningOperation(dto *pkg.RuntimeDTO, pOpr *internal.ProvisioningOperation) {
    40  	if pOpr != nil {
    41  		dto.Status.Provisioning = &pkg.Operation{}
    42  		c.applyOperation(&pOpr.Operation, dto.Status.Provisioning)
    43  		c.adjustRuntimeState(dto)
    44  	}
    45  }
    46  
    47  func (c *converter) ApplyDeprovisioningOperation(dto *pkg.RuntimeDTO, dOpr *internal.DeprovisioningOperation) {
    48  	if dOpr != nil {
    49  		if dOpr.Operation.State == orchestration.Pending {
    50  			return
    51  		}
    52  		dto.Status.Deprovisioning = &pkg.Operation{}
    53  		c.applyOperation(&dOpr.Operation, dto.Status.Deprovisioning)
    54  		c.adjustRuntimeState(dto)
    55  	}
    56  }
    57  
    58  func (c *converter) applyOperation(source *internal.Operation, target *pkg.Operation) {
    59  	if source != nil {
    60  		target.OperationID = source.ID
    61  		target.CreatedAt = source.CreatedAt
    62  		target.UpdatedAt = source.UpdatedAt
    63  		target.State = string(source.State)
    64  		target.Description = source.Description
    65  		target.OrchestrationID = source.OrchestrationID
    66  		target.RuntimeVersion = source.RuntimeVersion.Version
    67  		target.FinishedStages = source.FinishedStages
    68  		target.ExecutedButNotCompletedSteps = source.ExcutedButNotCompleted
    69  	}
    70  }
    71  
    72  func (c *converter) NewDTO(instance internal.Instance) (pkg.RuntimeDTO, error) {
    73  	toReturn := pkg.RuntimeDTO{
    74  		InstanceID:                  instance.InstanceID,
    75  		RuntimeID:                   instance.RuntimeID,
    76  		GlobalAccountID:             instance.GlobalAccountID,
    77  		SubscriptionGlobalAccountID: instance.SubscriptionGlobalAccountID,
    78  		SubAccountID:                instance.SubAccountID,
    79  		ServiceClassID:              instance.ServiceID,
    80  		ServiceClassName:            instance.ServiceName,
    81  		ServicePlanID:               instance.ServicePlanID,
    82  		ServicePlanName:             instance.ServicePlanName,
    83  		Provider:                    string(instance.Provider),
    84  		ProviderRegion:              instance.ProviderRegion,
    85  		UserID:                      instance.Parameters.ErsContext.UserID,
    86  		ShootName:                   instance.InstanceDetails.ShootName,
    87  		Status: pkg.RuntimeStatus{
    88  			CreatedAt:  instance.CreatedAt,
    89  			ModifiedAt: instance.UpdatedAt,
    90  			ExpiredAt:  instance.ExpiredAt,
    91  		},
    92  	}
    93  	if !instance.DeletedAt.IsZero() {
    94  		toReturn.Status.DeletedAt = &instance.DeletedAt
    95  	}
    96  
    97  	c.setRegionOrDefault(instance, &toReturn)
    98  
    99  	return toReturn, nil
   100  }
   101  
   102  func (c *converter) ApplyUpgradingKymaOperations(dto *pkg.RuntimeDTO, oprs []internal.UpgradeKymaOperation, totalCount int) {
   103  	if len(oprs) <= 0 {
   104  		return
   105  	}
   106  	dto.Status.UpgradingKyma = &pkg.OperationsData{}
   107  	dto.Status.UpgradingKyma.TotalCount = totalCount
   108  	dto.Status.UpgradingKyma.Count = len(oprs)
   109  	dto.Status.UpgradingKyma.Data = make([]pkg.Operation, 0)
   110  	for _, o := range oprs {
   111  		op := pkg.Operation{}
   112  		c.applyOperation(&o.Operation, &op)
   113  		dto.Status.UpgradingKyma.Data = append(dto.Status.UpgradingKyma.Data, op)
   114  	}
   115  	c.adjustRuntimeState(dto)
   116  }
   117  
   118  func (c *converter) ApplyUpgradingClusterOperations(dto *pkg.RuntimeDTO, oprs []internal.UpgradeClusterOperation, totalCount int) {
   119  	if len(oprs) <= 0 {
   120  		return
   121  	}
   122  	dto.Status.UpgradingCluster = &pkg.OperationsData{}
   123  	dto.Status.UpgradingCluster.Data = make([]pkg.Operation, 0)
   124  	for _, o := range oprs {
   125  		op := pkg.Operation{}
   126  		c.applyOperation(&o.Operation, &op)
   127  		dto.Status.UpgradingCluster.Data = append(dto.Status.UpgradingCluster.Data, op)
   128  	}
   129  	dto.Status.UpgradingCluster.TotalCount = totalCount
   130  	dto.Status.UpgradingCluster.Count = len(dto.Status.UpgradingCluster.Data)
   131  	c.adjustRuntimeState(dto)
   132  }
   133  
   134  func (c *converter) ApplySuspensionOperations(dto *pkg.RuntimeDTO, oprs []internal.DeprovisioningOperation) {
   135  	if len(oprs) <= 0 {
   136  		return
   137  	}
   138  	suspension := &pkg.OperationsData{}
   139  	suspension.Data = make([]pkg.Operation, 0)
   140  
   141  	for _, o := range oprs {
   142  		if !o.Temporary || o.Operation.State == orchestration.Pending {
   143  			continue
   144  		}
   145  		op := pkg.Operation{}
   146  		c.applyOperation(&o.Operation, &op)
   147  		suspension.Data = append(suspension.Data, op)
   148  	}
   149  	suspension.TotalCount = len(suspension.Data)
   150  	suspension.Count = len(suspension.Data)
   151  	if suspension.Count > 0 {
   152  		dto.Status.Suspension = suspension
   153  	}
   154  	c.adjustRuntimeState(dto)
   155  }
   156  
   157  func (c *converter) ApplyUnsuspensionOperations(dto *pkg.RuntimeDTO, oprs []internal.ProvisioningOperation) {
   158  	if len(oprs) <= 0 {
   159  		return
   160  	}
   161  	dto.Status.Unsuspension = &pkg.OperationsData{}
   162  	dto.Status.Unsuspension.Data = make([]pkg.Operation, 0)
   163  
   164  	dto.Status.Unsuspension.TotalCount = len(oprs)
   165  	dto.Status.Unsuspension.Count = len(oprs)
   166  
   167  	for _, o := range oprs {
   168  		op := pkg.Operation{}
   169  		c.applyOperation(&o.Operation, &op)
   170  		dto.Status.Unsuspension.Data = append(dto.Status.Unsuspension.Data, op)
   171  	}
   172  	c.adjustRuntimeState(dto)
   173  }
   174  
   175  func (c *converter) ApplyUpdateOperations(dto *pkg.RuntimeDTO, oprs []internal.UpdatingOperation, totalCount int) {
   176  	if len(oprs) <= 0 {
   177  		return
   178  	}
   179  
   180  	dto.Status.Update = &pkg.OperationsData{}
   181  	dto.Status.Update.Data = make([]pkg.Operation, 0)
   182  	dto.Status.Update.Count = len(oprs)
   183  	dto.Status.Update.TotalCount = totalCount
   184  	for _, o := range oprs {
   185  		op := pkg.Operation{}
   186  		c.applyOperation(&o.Operation, &op)
   187  		dto.Status.Update.Data = append(dto.Status.Update.Data, op)
   188  	}
   189  	c.adjustRuntimeState(dto)
   190  }
   191  
   192  func (c *converter) adjustRuntimeState(dto *pkg.RuntimeDTO) {
   193  	lastOp := dto.LastOperation()
   194  	switch lastOp.State {
   195  	case string(domain.Succeeded):
   196  		dto.Status.State = pkg.StateSucceeded
   197  		switch lastOp.Type {
   198  		case pkg.Suspension:
   199  			dto.Status.State = pkg.StateSuspended
   200  		case pkg.Deprovision:
   201  			if len(lastOp.ExecutedButNotCompletedSteps) == 0 {
   202  				dto.Status.State = pkg.StateDeprovisioned
   203  			} else {
   204  				dto.Status.State = pkg.StateDeprovisionIncomplete
   205  			}
   206  		}
   207  	case string(domain.Failed):
   208  		dto.Status.State = pkg.StateFailed
   209  		switch lastOp.Type {
   210  		case pkg.UpgradeKyma, pkg.UpgradeCluster, pkg.Update:
   211  			dto.Status.State = pkg.StateError
   212  		}
   213  	case string(domain.InProgress):
   214  		switch lastOp.Type {
   215  		case pkg.Provision, pkg.Unsuspension:
   216  			dto.Status.State = pkg.StateProvisioning
   217  		case pkg.Deprovision, pkg.Suspension:
   218  			dto.Status.State = pkg.StateDeprovisioning
   219  		case pkg.UpgradeKyma, pkg.UpgradeCluster:
   220  			dto.Status.State = pkg.StateUpgrading
   221  		case pkg.Update:
   222  			dto.Status.State = pkg.StateUpdating
   223  		}
   224  	default:
   225  		dto.Status.State = pkg.StateSucceeded
   226  	}
   227  
   228  	if dto.Status.Suspension != nil && dto.Status.Suspension.Count > 0 {
   229  		// there is no unsuspension operation or the suspension is started after last unsuspension
   230  		if dto.Status.Unsuspension == nil ||
   231  			(dto.Status.Unsuspension.Count > 0 && dto.Status.Unsuspension.Data[0].CreatedAt.Before(dto.Status.Suspension.Data[0].CreatedAt)) {
   232  
   233  			switch dto.Status.Suspension.Data[0].State {
   234  			case string(domain.InProgress):
   235  				dto.Status.State = pkg.StateDeprovisioning
   236  			case string(domain.Failed):
   237  				dto.Status.State = pkg.StateFailed
   238  			default:
   239  				dto.Status.State = pkg.StateSuspended
   240  			}
   241  		}
   242  	}
   243  }