github.com/xgoffin/jenkins-library@v1.154.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 func cloudFoundryCreateServiceKey(options cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData) { 13 // for command execution use Command 14 c := command.Command{} 15 // reroute command output to logging framework 16 c.Stdout(log.Writer()) 17 c.Stderr(log.Writer()) 18 19 cfUtils := cloudfoundry.CFUtils{ 20 Exec: &c, 21 } 22 23 err := runCloudFoundryCreateServiceKey(&options, telemetryData, &c, &cfUtils) 24 if err != nil { 25 log.Entry(). 26 WithError(err). 27 Fatal("Error occurred during step.") 28 } 29 } 30 31 func runCloudFoundryCreateServiceKey(options *cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) { 32 33 // Login via cf cli 34 config := cloudfoundry.LoginOptions{ 35 CfAPIEndpoint: options.CfAPIEndpoint, 36 CfOrg: options.CfOrg, 37 CfSpace: options.CfSpace, 38 Username: options.Username, 39 Password: options.Password, 40 } 41 loginErr := cfUtils.Login(config) 42 if loginErr != nil { 43 return fmt.Errorf("Error while logging in occurred: %w", loginErr) 44 } 45 defer func() { 46 logoutErr := cfUtils.Logout() 47 if logoutErr != nil && returnedError == nil { 48 returnedError = fmt.Errorf("Error while logging out occurred: %w", logoutErr) 49 } 50 }() 51 log.Entry().Info("Creating Service Key") 52 53 var cfCreateServiceKeyScript []string 54 if options.CfServiceKeyConfig == "" { 55 cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName} 56 } else { 57 cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig} 58 } 59 err := c.RunExecutable("cf", cfCreateServiceKeyScript...) 60 if err != nil { 61 return fmt.Errorf("Failed to Create Service Key: %w", err) 62 } 63 64 return returnedError 65 }