mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
7ea5b09555
* Improve Tests for cf steps: CreateServiceKey and Delete Service * Fix CodeClimate issues * Adapt descriptions * Defer logout
66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
)
|
|
|
|
func cloudFoundryCreateServiceKey(options cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData) {
|
|
// for command execution use Command
|
|
c := command.Command{}
|
|
// reroute command output to logging framework
|
|
c.Stdout(log.Writer())
|
|
c.Stderr(log.Writer())
|
|
|
|
cfUtils := cloudfoundry.CFUtils{
|
|
Exec: &c,
|
|
}
|
|
|
|
err := runCloudFoundryCreateServiceKey(&options, telemetryData, &c, &cfUtils)
|
|
if err != nil {
|
|
log.Entry().
|
|
WithError(err).
|
|
Fatal("Error occured during step.")
|
|
}
|
|
}
|
|
|
|
func runCloudFoundryCreateServiceKey(options *cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) {
|
|
|
|
// Login via cf cli
|
|
config := cloudfoundry.LoginOptions{
|
|
CfAPIEndpoint: options.CfAPIEndpoint,
|
|
CfOrg: options.CfOrg,
|
|
CfSpace: options.CfSpace,
|
|
Username: options.Username,
|
|
Password: options.Password,
|
|
}
|
|
loginErr := cfUtils.Login(config)
|
|
if loginErr != nil {
|
|
return fmt.Errorf("Error while logging in occured: %w", loginErr)
|
|
}
|
|
defer func() {
|
|
logoutErr := cfUtils.Logout()
|
|
if logoutErr != nil && returnedError == nil {
|
|
returnedError = fmt.Errorf("Error while logging out occured: %w", logoutErr)
|
|
}
|
|
}()
|
|
log.Entry().Info("Creating Service Key")
|
|
|
|
var cfCreateServiceKeyScript []string
|
|
if options.CfServiceKeyConfig == "" {
|
|
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName}
|
|
} else {
|
|
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig}
|
|
}
|
|
err := c.RunExecutable("cf", cfCreateServiceKeyScript...)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to Create Service Key: %w", err)
|
|
}
|
|
|
|
return returnedError
|
|
}
|