github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapEnvironmentCreateSystem.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 9 "github.com/SAP/jenkins-library/pkg/abaputils" 10 "github.com/SAP/jenkins-library/pkg/cloudfoundry" 11 "github.com/SAP/jenkins-library/pkg/command" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/telemetry" 14 "github.com/ghodss/yaml" 15 "github.com/google/uuid" 16 ) 17 18 func abapEnvironmentCreateSystem(config abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData) { 19 20 cf := cloudfoundry.CFUtils{Exec: &command.Command{}} 21 u := &googleUUID{} 22 23 // error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end 24 err := runAbapEnvironmentCreateSystem(&config, telemetryData, cf, u) 25 if err != nil { 26 log.Entry().WithError(err).Fatal("step execution failed") 27 } 28 } 29 30 func runAbapEnvironmentCreateSystem(config *abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, u uuidGenerator) error { 31 32 if config.ServiceManifest != "" { 33 // if the manifest file is provided, it is directly passed through to cloudFoundryCreateService 34 createServiceConfig := cloudFoundryCreateServiceOptions{ 35 CfAPIEndpoint: config.CfAPIEndpoint, 36 CfOrg: config.CfOrg, 37 CfSpace: config.CfSpace, 38 Username: config.Username, 39 Password: config.Password, 40 ServiceManifest: config.ServiceManifest, 41 } 42 return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf) 43 } 44 // if no manifest file is provided, it is created with the provided config values 45 manifestYAML, err := generateManifestYAML(config) 46 47 // writing the yaml into a temporary file 48 path, _ := os.Getwd() 49 path = path + "/generated_service_manifest-" + u.getUUID() + ".yml" 50 log.Entry().Debugf("Path: %s", path) 51 err = ioutil.WriteFile(path, manifestYAML, 0644) 52 if err != nil { 53 log.SetErrorCategory(log.ErrorConfiguration) 54 return fmt.Errorf("%s: %w", "Could not generate manifest file for the cloud foundry cli", err) 55 } 56 57 defer os.Remove(path) 58 59 // Calling cloudFoundryCreateService with the respective parameters 60 createServiceConfig := cloudFoundryCreateServiceOptions{ 61 CfAPIEndpoint: config.CfAPIEndpoint, 62 CfOrg: config.CfOrg, 63 CfSpace: config.CfSpace, 64 Username: config.Username, 65 Password: config.Password, 66 ServiceManifest: path, 67 } 68 return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf) 69 } 70 71 func generateManifestYAML(config *abapEnvironmentCreateSystemOptions) ([]byte, error) { 72 addonProduct := "" 73 addonVersion := "" 74 parentSaaSAppName := "" 75 if config.AddonDescriptorFileName != "" && config.IncludeAddon { 76 descriptor, err := abaputils.ReadAddonDescriptor(config.AddonDescriptorFileName) 77 if err != nil { 78 log.SetErrorCategory(log.ErrorConfiguration) 79 return nil, fmt.Errorf("Cloud not read addonProduct and addonVersion from %s: %w", config.AddonDescriptorFileName, err) 80 } 81 addonProduct = descriptor.AddonProduct 82 addonVersion = descriptor.AddonVersionYAML 83 parentSaaSAppName = "addon_test" 84 85 } 86 params := abapSystemParameters{ 87 AdminEmail: config.AbapSystemAdminEmail, 88 Description: config.AbapSystemDescription, 89 IsDevelopmentAllowed: &config.AbapSystemIsDevelopmentAllowed, 90 SapSystemName: config.AbapSystemID, 91 SizeOfPersistence: config.AbapSystemSizeOfPersistence, 92 SizeOfRuntime: config.AbapSystemSizeOfRuntime, 93 AddonProductName: addonProduct, 94 AddonProductVersion: addonVersion, 95 ParentSaaSAppName: parentSaaSAppName, 96 } 97 98 serviceParameters, err := json.Marshal(params) 99 serviceParametersString := string(serviceParameters) 100 log.Entry().Debugf("Service Parameters: %s", serviceParametersString) 101 if err != nil { 102 log.SetErrorCategory(log.ErrorConfiguration) 103 return nil, fmt.Errorf("Could not generate parameter string for the cloud foundry cli: %w", err) 104 } 105 106 /* 107 Generating the temporary manifest yaml file 108 */ 109 service := Service{ 110 Name: config.CfServiceInstance, 111 Broker: config.CfService, 112 Plan: config.CfServicePlan, 113 Parameters: serviceParametersString, 114 } 115 116 serviceManifest := serviceManifest{CreateServices: []Service{service}} 117 errorMessage := "Could not generate manifest for the cloud foundry cli" 118 119 // converting the golang structure to json 120 manifestJSON, err := json.Marshal(serviceManifest) 121 if err != nil { 122 return nil, fmt.Errorf("%s: %w", errorMessage, err) 123 } 124 125 // converting the json to yaml 126 manifestYAML, err := yaml.JSONToYAML(manifestJSON) 127 if err != nil { 128 return nil, fmt.Errorf("%s: %w", errorMessage, err) 129 } 130 131 log.Entry().Debug(string(manifestYAML)) 132 133 return manifestYAML, nil 134 } 135 136 type abapSystemParameters struct { 137 AdminEmail string `json:"admin_email,omitempty"` 138 Description string `json:"description,omitempty"` 139 IsDevelopmentAllowed *bool `json:"is_development_allowed,omitempty"` 140 SapSystemName string `json:"sapsystemname,omitempty"` 141 SizeOfPersistence int `json:"size_of_persistence,omitempty"` 142 SizeOfRuntime int `json:"size_of_runtime,omitempty"` 143 AddonProductName string `json:"addon_product_name,omitempty"` 144 AddonProductVersion string `json:"addon_product_version,omitempty"` 145 ParentSaaSAppName string `json:"parent_saas_appname,omitempty"` 146 } 147 148 type serviceManifest struct { 149 CreateServices []Service `json:"create-services"` 150 } 151 152 // Service struct for creating a cloud foundry service 153 type Service struct { 154 Name string `json:"name"` 155 Broker string `json:"broker"` 156 Plan string `json:"plan"` 157 Parameters string `json:"parameters,omitempty"` 158 } 159 160 type uuidGenerator interface { 161 getUUID() string 162 } 163 164 type googleUUID struct { 165 } 166 167 func (u *googleUUID) getUUID() string { 168 return uuid.New().String() 169 }