github.com/xgoffin/jenkins-library@v1.154.0/cmd/cloudFoundryCreateService_generated.go (about) 1 // Code generated by piper's step-generator. DO NOT EDIT. 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "time" 9 10 "github.com/SAP/jenkins-library/pkg/config" 11 "github.com/SAP/jenkins-library/pkg/log" 12 "github.com/SAP/jenkins-library/pkg/splunk" 13 "github.com/SAP/jenkins-library/pkg/telemetry" 14 "github.com/SAP/jenkins-library/pkg/validation" 15 "github.com/spf13/cobra" 16 ) 17 18 type cloudFoundryCreateServiceOptions struct { 19 CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"` 20 Username string `json:"username,omitempty"` 21 Password string `json:"password,omitempty"` 22 CfOrg string `json:"cfOrg,omitempty"` 23 CfSpace string `json:"cfSpace,omitempty"` 24 CfService string `json:"cfService,omitempty"` 25 CfServicePlan string `json:"cfServicePlan,omitempty"` 26 CfServiceInstanceName string `json:"cfServiceInstanceName,omitempty"` 27 CfServiceBroker string `json:"cfServiceBroker,omitempty"` 28 CfCreateServiceConfig string `json:"cfCreateServiceConfig,omitempty"` 29 CfServiceTags string `json:"cfServiceTags,omitempty"` 30 ServiceManifest string `json:"serviceManifest,omitempty"` 31 ManifestVariables []string `json:"manifestVariables,omitempty"` 32 ManifestVariablesFiles []string `json:"manifestVariablesFiles,omitempty"` 33 } 34 35 // CloudFoundryCreateServiceCommand Creates one or multiple Services in Cloud Foundry 36 func CloudFoundryCreateServiceCommand() *cobra.Command { 37 const STEP_NAME = "cloudFoundryCreateService" 38 39 metadata := cloudFoundryCreateServiceMetadata() 40 var stepConfig cloudFoundryCreateServiceOptions 41 var startTime time.Time 42 var logCollector *log.CollectorHook 43 var splunkClient *splunk.Splunk 44 telemetryClient := &telemetry.Telemetry{} 45 46 var createCloudFoundryCreateServiceCmd = &cobra.Command{ 47 Use: STEP_NAME, 48 Short: "Creates one or multiple Services in Cloud Foundry", 49 Long: `Creates one or multiple Cloud Foundry Services in Cloud Foundry 50 Mandatory: 51 * Cloud Foundry API endpoint, Organization, Space and user are available 52 53 Please provide either of the following options: 54 * If you chose to create a single Service the Service Instance Name, Service Plan and Service Broker of the Service to be created have to be available. You can set the optional ` + "`" + `cfCreateServiceConfig` + "`" + ` flag to configure the Service creation with your respective JSON configuration. The JSON configuration can either be an in-line JSON string or the path a dedicated JSON configuration file containing the JSON configuration. If you chose a dedicated config file, you must store the file in the same folder as your ` + "`" + `Jenkinsfile` + "`" + ` that starts the Pipeline in order for the Pipeline to be able to find the file. Most favourable SCM is Git. If you want the service to be created from a particular broker you can set the optional ` + "`" + `cfServiceBroker` + "`" + `flag. You can set user provided tags for the Service creation using a flat list as the value for the optional ` + "`" + `cfServiceTags` + "`" + ` flag. The optional ` + "`" + `cfServiceBroker` + "`" + ` flag can be used when the service name is ambiguous. 55 * For creating one or multiple Cloud Foundry Services at once with the Cloud Foundry Create-Service-Push Plugin using the optional ` + "`" + `serviceManifest` + "`" + ` flag. If you chose to set this flag, the Create-Service-Push Plugin will be used for all Service creations in this step and you will need to provide a ` + "`" + `serviceManifest.yml` + "`" + ` file. In that case, above described flags and options will not be used for the Service creations, since you chose to use the Create-Service-Push Plugin. Please see below examples for more information on how to make use of the plugin with the appropriate step configuation. Additionally the Plugin provides the option to make use of variable substitution for the Service creations. You can find further information regarding the functionality of the Cloud Foundry Create-Service-Push Plugin in the respective documentation: [Cloud Foundry Create-Service-Push Plugin](https://github.com/dawu415/CF-CLI-Create-Service-Push-Plugin)`, 56 PreRunE: func(cmd *cobra.Command, _ []string) error { 57 startTime = time.Now() 58 log.SetStepName(STEP_NAME) 59 log.SetVerbose(GeneralConfig.Verbose) 60 61 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 62 63 path, _ := os.Getwd() 64 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 65 log.RegisterHook(fatalHook) 66 67 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 68 if err != nil { 69 log.SetErrorCategory(log.ErrorConfiguration) 70 return err 71 } 72 log.RegisterSecret(stepConfig.Username) 73 log.RegisterSecret(stepConfig.Password) 74 75 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 76 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 77 log.RegisterHook(&sentryHook) 78 } 79 80 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 81 splunkClient = &splunk.Splunk{} 82 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 83 log.RegisterHook(logCollector) 84 } 85 86 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 87 if err != nil { 88 return err 89 } 90 if err = validation.ValidateStruct(stepConfig); err != nil { 91 log.SetErrorCategory(log.ErrorConfiguration) 92 return err 93 } 94 95 return nil 96 }, 97 Run: func(_ *cobra.Command, _ []string) { 98 stepTelemetryData := telemetry.CustomData{} 99 stepTelemetryData.ErrorCode = "1" 100 handler := func() { 101 config.RemoveVaultSecretFiles() 102 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 103 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 104 stepTelemetryData.PiperCommitHash = GitCommit 105 telemetryClient.SetData(&stepTelemetryData) 106 telemetryClient.Send() 107 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 108 splunkClient.Send(telemetryClient.GetData(), logCollector) 109 } 110 } 111 log.DeferExitHandler(handler) 112 defer handler() 113 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 114 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 115 splunkClient.Initialize(GeneralConfig.CorrelationID, 116 GeneralConfig.HookConfig.SplunkConfig.Dsn, 117 GeneralConfig.HookConfig.SplunkConfig.Token, 118 GeneralConfig.HookConfig.SplunkConfig.Index, 119 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 120 } 121 cloudFoundryCreateService(stepConfig, &stepTelemetryData) 122 stepTelemetryData.ErrorCode = "0" 123 log.Entry().Info("SUCCESS") 124 }, 125 } 126 127 addCloudFoundryCreateServiceFlags(createCloudFoundryCreateServiceCmd, &stepConfig) 128 return createCloudFoundryCreateServiceCmd 129 } 130 131 func addCloudFoundryCreateServiceFlags(cmd *cobra.Command, stepConfig *cloudFoundryCreateServiceOptions) { 132 cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", `https://api.cf.eu10.hana.ondemand.com`, "Cloud Foundry API endpoint") 133 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF") 134 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for Cloud Foundry User") 135 cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org") 136 cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry Space") 137 cmd.Flags().StringVar(&stepConfig.CfService, "cfService", os.Getenv("PIPER_cfService"), "Parameter for Cloud Foundry Service to be used for creating Cloud Foundry Service") 138 cmd.Flags().StringVar(&stepConfig.CfServicePlan, "cfServicePlan", os.Getenv("PIPER_cfServicePlan"), "Parameter for Cloud Foundry Service Plan to be used when creating a Cloud Foundry Service") 139 cmd.Flags().StringVar(&stepConfig.CfServiceInstanceName, "cfServiceInstanceName", os.Getenv("PIPER_cfServiceInstanceName"), "Parameter for naming the Service Instance when creating a Cloud Foundry Service") 140 cmd.Flags().StringVar(&stepConfig.CfServiceBroker, "cfServiceBroker", os.Getenv("PIPER_cfServiceBroker"), "Parameter for Service Broker to be used when creating a Cloud Foundry Service") 141 cmd.Flags().StringVar(&stepConfig.CfCreateServiceConfig, "cfCreateServiceConfig", os.Getenv("PIPER_cfCreateServiceConfig"), "Path to JSON file or JSON in-line string for a Cloud Foundry Service creation") 142 cmd.Flags().StringVar(&stepConfig.CfServiceTags, "cfServiceTags", os.Getenv("PIPER_cfServiceTags"), "Flat list of Tags to be used when creating a Cloud Foundry Service in a single string") 143 cmd.Flags().StringVar(&stepConfig.ServiceManifest, "serviceManifest", `service-manifest.yml`, "Path to Cloud Foundry Service Manifest in YAML format for multiple service creations that are being passed to a Create-Service-Push Cloud Foundry cli plugin") 144 cmd.Flags().StringSliceVar(&stepConfig.ManifestVariables, "manifestVariables", []string{}, "Defines a List of variables as key-value Map objects used for variable substitution within the file given by the Manifest. Defaults to an empty list, if not specified otherwise. This can be used to set variables like it is provided by `cf push --var key=value`. The order of the maps of variables given in the list is relevant in case there are conflicting variable names and values between maps contained within the list. In case of conflicts, the last specified map in the list will win. Though each map entry in the list can contain more than one key-value pair for variable substitution, it is recommended to stick to one entry per map, and rather declare more maps within the list. The reason is that if a map in the list contains more than one key-value entry, and the entries are conflicting, the conflict resolution behavior is undefined (since map entries have no sequence). Variables defined via `manifestVariables` always win over conflicting variables defined via any file given by `manifestVariablesFiles` - no matter what is declared before. This is the same behavior as can be observed when using `cf push --var` in combination with `cf push --vars-file`") 145 cmd.Flags().StringSliceVar(&stepConfig.ManifestVariablesFiles, "manifestVariablesFiles", []string{}, "Defines the manifest variables Yaml files to be used to replace variable references in manifest. This parameter is optional and will default to `manifest-variables.yml`. This can be used to set variable files like it is provided by `cf push --vars-file <file>`. If the manifest is present and so are all variable files, a variable substitution will be triggered that uses the `cfManifestSubstituteVariables` step before deployment. The format of variable references follows the Cloud Foundry standard in `https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#variable-substitution`") 146 147 cmd.MarkFlagRequired("cfApiEndpoint") 148 cmd.MarkFlagRequired("username") 149 cmd.MarkFlagRequired("password") 150 cmd.MarkFlagRequired("cfOrg") 151 cmd.MarkFlagRequired("cfSpace") 152 } 153 154 // retrieve step metadata 155 func cloudFoundryCreateServiceMetadata() config.StepData { 156 var theMetaData = config.StepData{ 157 Metadata: config.StepMetadata{ 158 Name: "cloudFoundryCreateService", 159 Aliases: []config.Alias{}, 160 Description: "Creates one or multiple Services in Cloud Foundry", 161 }, 162 Spec: config.StepSpec{ 163 Inputs: config.StepInputs{ 164 Secrets: []config.StepSecrets{ 165 {Name: "cfCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate to the Cloud Foundry API.", Type: "jenkins", Aliases: []config.Alias{{Name: "cloudFoundry/credentialsId", Deprecated: false}}}, 166 }, 167 Resources: []config.StepResources{ 168 {Name: "deployDescriptor", Type: "stash"}, 169 }, 170 Parameters: []config.StepParameters{ 171 { 172 Name: "cfApiEndpoint", 173 ResourceRef: []config.ResourceReference{}, 174 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 175 Type: "string", 176 Mandatory: true, 177 Aliases: []config.Alias{{Name: "cloudFoundry/apiEndpoint"}}, 178 Default: `https://api.cf.eu10.hana.ondemand.com`, 179 }, 180 { 181 Name: "username", 182 ResourceRef: []config.ResourceReference{ 183 { 184 Name: "cfCredentialsId", 185 Param: "username", 186 Type: "secret", 187 }, 188 189 { 190 Name: "cloudfoundryVaultSecretName", 191 Type: "vaultSecret", 192 Default: "cloudfoundry-$(org)-$(space)", 193 }, 194 }, 195 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 196 Type: "string", 197 Mandatory: true, 198 Aliases: []config.Alias{}, 199 Default: os.Getenv("PIPER_username"), 200 }, 201 { 202 Name: "password", 203 ResourceRef: []config.ResourceReference{ 204 { 205 Name: "cfCredentialsId", 206 Param: "password", 207 Type: "secret", 208 }, 209 210 { 211 Name: "cloudfoundryVaultSecretName", 212 Type: "vaultSecret", 213 Default: "cloudfoundry-$(org)-$(space)", 214 }, 215 }, 216 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 217 Type: "string", 218 Mandatory: true, 219 Aliases: []config.Alias{}, 220 Default: os.Getenv("PIPER_password"), 221 }, 222 { 223 Name: "cfOrg", 224 ResourceRef: []config.ResourceReference{}, 225 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 226 Type: "string", 227 Mandatory: true, 228 Aliases: []config.Alias{{Name: "cloudFoundry/org"}}, 229 Default: os.Getenv("PIPER_cfOrg"), 230 }, 231 { 232 Name: "cfSpace", 233 ResourceRef: []config.ResourceReference{}, 234 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 235 Type: "string", 236 Mandatory: true, 237 Aliases: []config.Alias{{Name: "cloudFoundry/space"}}, 238 Default: os.Getenv("PIPER_cfSpace"), 239 }, 240 { 241 Name: "cfService", 242 ResourceRef: []config.ResourceReference{}, 243 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 244 Type: "string", 245 Mandatory: false, 246 Aliases: []config.Alias{{Name: "cloudFoundry/service"}}, 247 Default: os.Getenv("PIPER_cfService"), 248 }, 249 { 250 Name: "cfServicePlan", 251 ResourceRef: []config.ResourceReference{}, 252 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 253 Type: "string", 254 Mandatory: false, 255 Aliases: []config.Alias{{Name: "cloudFoundry/servicePlan"}}, 256 Default: os.Getenv("PIPER_cfServicePlan"), 257 }, 258 { 259 Name: "cfServiceInstanceName", 260 ResourceRef: []config.ResourceReference{}, 261 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 262 Type: "string", 263 Mandatory: false, 264 Aliases: []config.Alias{{Name: "cloudFoundry/serviceInstanceName"}}, 265 Default: os.Getenv("PIPER_cfServiceInstanceName"), 266 }, 267 { 268 Name: "cfServiceBroker", 269 ResourceRef: []config.ResourceReference{}, 270 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 271 Type: "string", 272 Mandatory: false, 273 Aliases: []config.Alias{{Name: "cloudFoundry/serviceBroker"}}, 274 Default: os.Getenv("PIPER_cfServiceBroker"), 275 }, 276 { 277 Name: "cfCreateServiceConfig", 278 ResourceRef: []config.ResourceReference{}, 279 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 280 Type: "string", 281 Mandatory: false, 282 Aliases: []config.Alias{{Name: "cloudFoundry/createServiceConfig"}}, 283 Default: os.Getenv("PIPER_cfCreateServiceConfig"), 284 }, 285 { 286 Name: "cfServiceTags", 287 ResourceRef: []config.ResourceReference{}, 288 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 289 Type: "string", 290 Mandatory: false, 291 Aliases: []config.Alias{{Name: "cloudFoundry/serviceTags"}}, 292 Default: os.Getenv("PIPER_cfServiceTags"), 293 }, 294 { 295 Name: "serviceManifest", 296 ResourceRef: []config.ResourceReference{}, 297 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 298 Type: "string", 299 Mandatory: false, 300 Aliases: []config.Alias{{Name: "cloudFoundry/serviceManifest"}, {Name: "cfServiceManifest"}}, 301 Default: `service-manifest.yml`, 302 }, 303 { 304 Name: "manifestVariables", 305 ResourceRef: []config.ResourceReference{}, 306 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 307 Type: "[]string", 308 Mandatory: false, 309 Aliases: []config.Alias{{Name: "cloudFoundry/manifestVariables"}, {Name: "cfManifestVariables"}}, 310 Default: []string{}, 311 }, 312 { 313 Name: "manifestVariablesFiles", 314 ResourceRef: []config.ResourceReference{}, 315 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 316 Type: "[]string", 317 Mandatory: false, 318 Aliases: []config.Alias{{Name: "cloudFoundry/manifestVariablesFiles"}, {Name: "cfManifestVariablesFiles"}}, 319 Default: []string{}, 320 }, 321 }, 322 }, 323 Containers: []config.Container{ 324 {Name: "cf", Image: "ppiper/cf-cli:7"}, 325 }, 326 }, 327 } 328 return theMetaData 329 }