github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentCreateSystem.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/abaputils"
     8  	"github.com/SAP/jenkins-library/pkg/cloudfoundry"
     9  	"github.com/SAP/jenkins-library/pkg/command"
    10  	"github.com/SAP/jenkins-library/pkg/log"
    11  	"github.com/SAP/jenkins-library/pkg/telemetry"
    12  	"github.com/google/uuid"
    13  )
    14  
    15  func abapEnvironmentCreateSystem(config abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData) {
    16  
    17  	cf := cloudfoundry.CFUtils{Exec: &command.Command{}}
    18  	u := &googleUUID{}
    19  
    20  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    21  	err := runAbapEnvironmentCreateSystem(&config, telemetryData, cf, u)
    22  	if err != nil {
    23  		log.Entry().WithError(err).Fatal("step execution failed")
    24  	}
    25  }
    26  
    27  func runAbapEnvironmentCreateSystem(config *abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, u uuidGenerator) error {
    28  
    29  	if config.ServiceManifest != "" {
    30  		// if the manifest file is provided, it is directly passed through to cloudFoundryCreateService
    31  		createServiceConfig := cloudFoundryCreateServiceOptions{
    32  			CfAPIEndpoint:   config.CfAPIEndpoint,
    33  			CfOrg:           config.CfOrg,
    34  			CfSpace:         config.CfSpace,
    35  			Username:        config.Username,
    36  			Password:        config.Password,
    37  			ServiceManifest: config.ServiceManifest,
    38  		}
    39  		return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
    40  	}
    41  
    42  	cfConfig, err := generateServiceParameterString(config)
    43  	if err != nil {
    44  		log.Entry().Fatalf("Could not generate parameter string")
    45  	}
    46  	// Calling cloudFoundryCreateService with the respective parameters
    47  	createServiceConfig := cloudFoundryCreateServiceOptions{
    48  		CfAPIEndpoint:         config.CfAPIEndpoint,
    49  		CfOrg:                 config.CfOrg,
    50  		CfSpace:               config.CfSpace,
    51  		Username:              config.Username,
    52  		Password:              config.Password,
    53  		CfService:             config.CfService,
    54  		CfServicePlan:         config.CfServicePlan,
    55  		CfServiceInstanceName: config.CfServiceInstance,
    56  		CfCreateServiceConfig: cfConfig,
    57  		CfAsync:               false,
    58  	}
    59  	return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
    60  }
    61  
    62  func generateServiceParameterString(config *abapEnvironmentCreateSystemOptions) (string, error) {
    63  	addonProduct := ""
    64  	addonVersion := ""
    65  	parentSaaSAppName := ""
    66  	if config.AddonDescriptorFileName != "" && config.IncludeAddon {
    67  		descriptor, err := abaputils.ReadAddonDescriptor(config.AddonDescriptorFileName)
    68  		if err != nil {
    69  			log.SetErrorCategory(log.ErrorConfiguration)
    70  			return "", fmt.Errorf("Cloud not read addonProduct and addonVersion from %s: %w", config.AddonDescriptorFileName, err)
    71  		}
    72  		addonProduct = descriptor.AddonProduct
    73  		addonVersion = descriptor.AddonVersionYAML
    74  		parentSaaSAppName = "addon_test"
    75  
    76  	}
    77  	params := abapSystemParameters{
    78  		AdminEmail:           config.AbapSystemAdminEmail,
    79  		Description:          config.AbapSystemDescription,
    80  		IsDevelopmentAllowed: &config.AbapSystemIsDevelopmentAllowed,
    81  		SapSystemName:        config.AbapSystemID,
    82  		SizeOfPersistence:    config.AbapSystemSizeOfPersistence,
    83  		SizeOfRuntime:        config.AbapSystemSizeOfRuntime,
    84  		AddonProductName:     addonProduct,
    85  		AddonProductVersion:  addonVersion,
    86  		ParentSaaSAppName:    parentSaaSAppName,
    87  	}
    88  
    89  	serviceParameters, err := json.Marshal(params)
    90  	serviceParametersString := string(serviceParameters)
    91  	log.Entry().Debugf("Service Parameters: %s", serviceParametersString)
    92  	if err != nil {
    93  		log.SetErrorCategory(log.ErrorConfiguration)
    94  		return "", fmt.Errorf("Could not generate parameter string for the cloud foundry cli: %w", err)
    95  	}
    96  
    97  	return serviceParametersString, nil
    98  }
    99  
   100  type abapSystemParameters struct {
   101  	AdminEmail           string `json:"admin_email,omitempty"`
   102  	Description          string `json:"description,omitempty"`
   103  	IsDevelopmentAllowed *bool  `json:"is_development_allowed,omitempty"`
   104  	SapSystemName        string `json:"sapsystemname,omitempty"`
   105  	SizeOfPersistence    int    `json:"size_of_persistence,omitempty"`
   106  	SizeOfRuntime        int    `json:"size_of_runtime,omitempty"`
   107  	AddonProductName     string `json:"addon_product_name,omitempty"`
   108  	AddonProductVersion  string `json:"addon_product_version,omitempty"`
   109  	ParentSaaSAppName    string `json:"parent_saas_appname,omitempty"`
   110  }
   111  
   112  type serviceManifest struct {
   113  	CreateServices []Service `json:"create-services"`
   114  }
   115  
   116  // Service struct for creating a cloud foundry service
   117  type Service struct {
   118  	Name       string `json:"name"`
   119  	Broker     string `json:"broker"`
   120  	Plan       string `json:"plan"`
   121  	Parameters string `json:"parameters,omitempty"`
   122  }
   123  
   124  type uuidGenerator interface {
   125  	getUUID() string
   126  }
   127  
   128  type googleUUID struct {
   129  }
   130  
   131  func (u *googleUUID) getUUID() string {
   132  	return uuid.New().String()
   133  }