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

     1  package handlers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     7  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    10  )
    11  
    12  type Converter struct{}
    13  
    14  func (*Converter) OrchestrationToDTO(o *internal.Orchestration, stats map[string]int) (*orchestration.StatusResponse, error) {
    15  	return &orchestration.StatusResponse{
    16  		OrchestrationID: o.OrchestrationID,
    17  		Type:            o.Type,
    18  		State:           o.State,
    19  		Description:     o.Description,
    20  		CreatedAt:       o.CreatedAt,
    21  		UpdatedAt:       o.UpdatedAt,
    22  		Parameters:      o.Parameters,
    23  		OperationStats:  stats,
    24  	}, nil
    25  }
    26  
    27  func (c *Converter) OrchestrationListToDTO(orchestrations []internal.Orchestration, count, totalCount int) (orchestration.StatusResponseList, error) {
    28  	responses := make([]orchestration.StatusResponse, 0)
    29  
    30  	for _, o := range orchestrations {
    31  		status, err := c.OrchestrationToDTO(&o, nil)
    32  		if err != nil {
    33  			return orchestration.StatusResponseList{}, fmt.Errorf("while converting orchestration to DTO: %w", err)
    34  		}
    35  		responses = append(responses, *status)
    36  	}
    37  
    38  	return orchestration.StatusResponseList{
    39  		Data:       responses,
    40  		Count:      count,
    41  		TotalCount: totalCount,
    42  	}, nil
    43  }
    44  
    45  func (c *Converter) UpgradeKymaOperationToDTO(op internal.UpgradeKymaOperation) (orchestration.OperationResponse, error) {
    46  	return orchestration.OperationResponse{
    47  		OperationID:            op.Operation.ID,
    48  		RuntimeID:              op.RuntimeOperation.RuntimeID,
    49  		GlobalAccountID:        op.GlobalAccountID,
    50  		SubAccountID:           op.RuntimeOperation.SubAccountID,
    51  		OrchestrationID:        op.OrchestrationID,
    52  		ServicePlanID:          op.ProvisioningParameters.PlanID,
    53  		ServicePlanName:        broker.PlanNamesMapping[op.ProvisioningParameters.PlanID],
    54  		DryRun:                 op.DryRun,
    55  		ShootName:              op.RuntimeOperation.ShootName,
    56  		MaintenanceWindowBegin: op.MaintenanceWindowBegin,
    57  		MaintenanceWindowEnd:   op.MaintenanceWindowEnd,
    58  		State:                  string(op.Operation.State),
    59  		Description:            op.Operation.Description,
    60  	}, nil
    61  }
    62  
    63  func (c *Converter) UpgradeKymaOperationListToDTO(ops []internal.UpgradeKymaOperation, count, totalCount int) (orchestration.OperationResponseList, error) {
    64  	data := make([]orchestration.OperationResponse, 0)
    65  
    66  	for _, op := range ops {
    67  		o, err := c.UpgradeKymaOperationToDTO(op)
    68  		if err != nil {
    69  			return orchestration.OperationResponseList{}, fmt.Errorf("while converting operation to DTO: %w", err)
    70  		}
    71  		data = append(data, o)
    72  	}
    73  
    74  	return orchestration.OperationResponseList{
    75  		Data:       data,
    76  		Count:      count,
    77  		TotalCount: totalCount,
    78  	}, nil
    79  }
    80  
    81  func (c *Converter) UpgradeKymaOperationToDetailDTO(op internal.UpgradeKymaOperation, kymaConfig *gqlschema.KymaConfigInput) (orchestration.OperationDetailResponse, error) {
    82  	resp, err := c.UpgradeKymaOperationToDTO(op)
    83  	if err != nil {
    84  		return orchestration.OperationDetailResponse{}, fmt.Errorf("while converting operation to DTO: %w", err)
    85  	}
    86  	return orchestration.OperationDetailResponse{
    87  		OperationResponse: resp,
    88  		KymaConfig:        kymaConfig,
    89  	}, nil
    90  }
    91  
    92  func (c *Converter) UpgradeClusterOperationToDTO(op internal.UpgradeClusterOperation) (orchestration.OperationResponse, error) {
    93  	return orchestration.OperationResponse{
    94  		OperationID:            op.Operation.ID,
    95  		RuntimeID:              op.RuntimeOperation.RuntimeID,
    96  		GlobalAccountID:        op.GlobalAccountID,
    97  		SubAccountID:           op.RuntimeOperation.SubAccountID,
    98  		OrchestrationID:        op.OrchestrationID,
    99  		ServicePlanID:          op.ProvisioningParameters.PlanID,
   100  		ServicePlanName:        broker.PlanNamesMapping[op.ProvisioningParameters.PlanID],
   101  		DryRun:                 op.DryRun,
   102  		ShootName:              op.RuntimeOperation.ShootName,
   103  		MaintenanceWindowBegin: op.MaintenanceWindowBegin,
   104  		MaintenanceWindowEnd:   op.MaintenanceWindowEnd,
   105  		State:                  string(op.Operation.State),
   106  		Description:            op.Operation.Description,
   107  	}, nil
   108  }
   109  
   110  func (c *Converter) UpgradeClusterOperationListToDTO(ops []internal.UpgradeClusterOperation, count, totalCount int) (orchestration.OperationResponseList, error) {
   111  	data := make([]orchestration.OperationResponse, 0, len(ops))
   112  
   113  	for _, op := range ops {
   114  		o, err := c.UpgradeClusterOperationToDTO(op)
   115  		if err != nil {
   116  			return orchestration.OperationResponseList{}, fmt.Errorf("while converting operation to DTO: %w", err)
   117  		}
   118  		data = append(data, o)
   119  	}
   120  
   121  	return orchestration.OperationResponseList{
   122  		Data:       data,
   123  		Count:      count,
   124  		TotalCount: totalCount,
   125  	}, nil
   126  }
   127  
   128  func (c *Converter) UpgradeClusterOperationToDetailDTO(op internal.UpgradeClusterOperation, clusterConfig *gqlschema.GardenerConfigInput) (orchestration.OperationDetailResponse, error) {
   129  	resp, err := c.UpgradeClusterOperationToDTO(op)
   130  	if err != nil {
   131  		return orchestration.OperationDetailResponse{}, fmt.Errorf("while converting operation to DTO: %w", err)
   132  	}
   133  	return orchestration.OperationDetailResponse{
   134  		OperationResponse: resp,
   135  		ClusterConfig:     clusterConfig,
   136  	}, nil
   137  }