1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/cloudfoundry/Services.go
MuellerHenrik e3f914e09d
Add abap utils pkg - ABAP Environment Steps (#1757)
* Add abaputils pkg and go files

* Add ReadServiceKeyAbapEnvironment function

* Fixes

* Add structs for SC, Pull and Branch

* Minor Improvements

* Adapt unit tests to new abaputils pkg

* Fixes

* Add adapted tests

* Fixes

* Fix cloudfoundry test

* Add check for host prefix (HTTPS)

* Fix tests + cleanup

* Fixes

* Fixes

* Fix

* Add mock for abaputils pkg unit tests

* Adapt abaputils comments

* Add unit test for missing params case

* Fix for missing mapping of CfSpace

* Fix host schema

* Remove LogoutOption param of unit tests and steps

* Fix unit test

* Fix unit test CF ReadServiceKey

Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
2020-07-23 10:26:50 +02:00

71 lines
1.9 KiB
Go

package cloudfoundry
import (
"bytes"
"fmt"
"strings"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
)
// ReadServiceKey reads a cloud foundry service key based on provided service instance and service key name parameters
func (cf *CFUtils) ReadServiceKey(options ServiceKeyOptions) (string, error) {
_c := cf.Exec
if _c == nil {
_c = &command.Command{}
}
cfconfig := LoginOptions{
CfAPIEndpoint: options.CfAPIEndpoint,
CfOrg: options.CfOrg,
CfSpace: options.CfSpace,
Username: options.Username,
Password: options.Password,
}
err := cf.Login(cfconfig)
if err != nil {
// error while trying to run cf login
return "", fmt.Errorf("Login to Cloud Foundry failed: %w", err)
}
var serviceKeyBytes bytes.Buffer
_c.Stdout(&serviceKeyBytes)
// we are logged in --> read service key
log.Entry().WithField("cfServiceInstance", options.CfServiceInstance).WithField("cfServiceKey", options.CfServiceKeyName).Info("Read service key for service instance")
cfReadServiceKeyScript := []string{"service-key", options.CfServiceInstance, options.CfServiceKeyName}
err = _c.RunExecutable("cf", cfReadServiceKeyScript...)
if err != nil {
// error while reading service key
return "", fmt.Errorf("Reading service key failed: %w", err)
}
// parse and return service key
var serviceKeyJSON string
if len(serviceKeyBytes.String()) > 0 {
var lines []string = strings.Split(serviceKeyBytes.String(), "\n")
serviceKeyJSON = strings.Join(lines[2:], "")
}
err = cf.Logout()
if err != nil {
return serviceKeyJSON, fmt.Errorf("Logout of Cloud Foundry failed: %w", err)
}
return serviceKeyJSON, err
}
//ServiceKeyOptions for reading CF Service Key
type ServiceKeyOptions struct {
CfAPIEndpoint string
CfOrg string
CfSpace string
CfServiceInstance string
CfServiceKeyName string
Username string
Password string
}