github.com/SAP/jenkins-library@v1.362.0/cmd/cloudFoundryCreateServiceKey.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/SAP/jenkins-library/pkg/cloudfoundry" 7 "github.com/SAP/jenkins-library/pkg/command" 8 "github.com/SAP/jenkins-library/pkg/log" 9 "github.com/SAP/jenkins-library/pkg/telemetry" 10 ) 11 12 const cfCliSynchronousRequestFlag = "--wait" 13 14 func cloudFoundryCreateServiceKey(options cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData) { 15 // for command execution use Command 16 c := command.Command{} 17 // reroute command output to logging framework 18 c.Stdout(log.Writer()) 19 c.Stderr(log.Writer()) 20 21 cfUtils := cloudfoundry.CFUtils{ 22 Exec: &c, 23 } 24 25 err := runCloudFoundryCreateServiceKey(&options, telemetryData, &c, &cfUtils) 26 if err != nil { 27 log.Entry(). 28 WithError(err). 29 Fatal("Error occurred during step.") 30 } 31 } 32 33 func runCloudFoundryCreateServiceKey(options *cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) { 34 35 // Login via cf cli 36 config := cloudfoundry.LoginOptions{ 37 CfAPIEndpoint: options.CfAPIEndpoint, 38 CfOrg: options.CfOrg, 39 CfSpace: options.CfSpace, 40 Username: options.Username, 41 Password: options.Password, 42 } 43 loginErr := cfUtils.Login(config) 44 if loginErr != nil { 45 return fmt.Errorf("Error while logging in occurred: %w", loginErr) 46 } 47 defer func() { 48 logoutErr := cfUtils.Logout() 49 if logoutErr != nil && returnedError == nil { 50 returnedError = fmt.Errorf("Error while logging out occurred: %w", logoutErr) 51 } 52 }() 53 log.Entry().Info("Creating Service Key") 54 55 var cfCreateServiceKeyScript []string 56 // the --wait option was added for cf cli v8 in order to ensure a synchronous creation of the servie key that was default in v7 or earlier 57 if options.CfServiceKeyConfig == "" { 58 cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName} 59 } else { 60 cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig} 61 } 62 63 // If a synchronous execution is requested, the "--wait" flag needs to be added 64 if !options.CfAsync { 65 cfCreateServiceKeyScript = append(cfCreateServiceKeyScript, cfCliSynchronousRequestFlag) 66 } 67 68 err := c.RunExecutable("cf", cfCreateServiceKeyScript...) 69 if err != nil { 70 return fmt.Errorf("Failed to Create Service Key: %w", err) 71 } 72 73 return returnedError 74 }