Skip to content

Commit

Permalink
Change to default cf create-service implementation (SAP#4224)
Browse files Browse the repository at this point in the history
* Change to default cf create-service

* Adapt test

* Adapt tests

* Remove comment

---------

Co-authored-by: tiloKo <[email protected]>
  • Loading branch information
DanielMieg and tiloKo authored Mar 8, 2023
1 parent cfe21eb commit 8084ce1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 141 deletions.
71 changes: 16 additions & 55 deletions cmd/abapEnvironmentCreateSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/SAP/jenkins-library/pkg/abaputils"
"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"
"github.com/ghodss/yaml"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -41,45 +38,36 @@ func runAbapEnvironmentCreateSystem(config *abapEnvironmentCreateSystemOptions,
}
return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
}
// if no manifest file is provided, it is created with the provided config values
manifestYAML, err := generateManifestYAML(config)
if err != nil {
return err
}

// writing the yaml into a temporary file
path, _ := os.Getwd()
path = path + "/generated_service_manifest-" + u.getUUID() + ".yml"
log.Entry().Debugf("Path: %s", path)
err = ioutil.WriteFile(path, manifestYAML, 0644)
cfConfig, err := generateServiceParameterString(config)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return fmt.Errorf("%s: %w", "Could not generate manifest file for the cloud foundry cli", err)
log.Entry().Fatalf("Could not generate parameter string")
}

defer os.Remove(path)

// Calling cloudFoundryCreateService with the respective parameters
createServiceConfig := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: config.CfAPIEndpoint,
CfOrg: config.CfOrg,
CfSpace: config.CfSpace,
Username: config.Username,
Password: config.Password,
ServiceManifest: path,
CfAPIEndpoint: config.CfAPIEndpoint,
CfOrg: config.CfOrg,
CfSpace: config.CfSpace,
Username: config.Username,
Password: config.Password,
CfService: config.CfService,
CfServicePlan: config.CfServicePlan,
CfServiceInstanceName: config.CfServiceInstance,
CfCreateServiceConfig: cfConfig,
CfAsync: false,
}
return runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
}

func generateManifestYAML(config *abapEnvironmentCreateSystemOptions) ([]byte, error) {
func generateServiceParameterString(config *abapEnvironmentCreateSystemOptions) (string, error) {
addonProduct := ""
addonVersion := ""
parentSaaSAppName := ""
if config.AddonDescriptorFileName != "" && config.IncludeAddon {
descriptor, err := abaputils.ReadAddonDescriptor(config.AddonDescriptorFileName)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return nil, fmt.Errorf("Cloud not read addonProduct and addonVersion from %s: %w", config.AddonDescriptorFileName, err)
return "", fmt.Errorf("Cloud not read addonProduct and addonVersion from %s: %w", config.AddonDescriptorFileName, err)
}
addonProduct = descriptor.AddonProduct
addonVersion = descriptor.AddonVersionYAML
Expand All @@ -103,37 +91,10 @@ func generateManifestYAML(config *abapEnvironmentCreateSystemOptions) ([]byte, e
log.Entry().Debugf("Service Parameters: %s", serviceParametersString)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return nil, fmt.Errorf("Could not generate parameter string for the cloud foundry cli: %w", err)
}

/*
Generating the temporary manifest yaml file
*/
service := Service{
Name: config.CfServiceInstance,
Broker: config.CfService,
Plan: config.CfServicePlan,
Parameters: serviceParametersString,
return "", fmt.Errorf("Could not generate parameter string for the cloud foundry cli: %w", err)
}

serviceManifest := serviceManifest{CreateServices: []Service{service}}
errorMessage := "Could not generate manifest for the cloud foundry cli"

// converting the golang structure to json
manifestJSON, err := json.Marshal(serviceManifest)
if err != nil {
return nil, fmt.Errorf("%s: %w", errorMessage, err)
}

// converting the json to yaml
manifestYAML, err := yaml.JSONToYAML(manifestJSON)
if err != nil {
return nil, fmt.Errorf("%s: %w", errorMessage, err)
}

log.Entry().Debug(string(manifestYAML))

return manifestYAML, nil
return serviceParametersString, nil
}

type abapSystemParameters struct {
Expand Down
90 changes: 11 additions & 79 deletions cmd/abapEnvironmentCreateSystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) {
cf := cloudfoundry.CFUtils{Exec: m}
u := &uuidMock{}

t.Run("Create service with generated manifest", func(t *testing.T) {
t.Run("Create service with cf create-service", func(t *testing.T) {
defer cfMockCleanup(m)
config := abapEnvironmentCreateSystemOptions{
CfAPIEndpoint: "https://api.endpoint.com",
Expand All @@ -27,12 +27,11 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) {
CfServiceInstance: "testName",
CfServicePlan: "testPlan",
}
wd, _ := os.Getwd()
err := runAbapEnvironmentCreateSystem(&config, nil, cf, u)
if assert.NoError(t, err) {
assert.Equal(t, []mock.ExecCall{
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", wd + "/generated_service_manifest-my-uuid.yml"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstance, "-c", "{\"is_development_allowed\":false}", "--wait"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
Expand Down Expand Up @@ -88,57 +87,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) {

func TestManifestGeneration(t *testing.T) {

t.Run("Create service with generated manifest", func(t *testing.T) {
config := abapEnvironmentCreateSystemOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfService: "testService",
CfServiceInstance: "testName",
CfServicePlan: "testPlan",
AbapSystemAdminEmail: "[email protected]",
AbapSystemID: "H02",
AbapSystemIsDevelopmentAllowed: true,
AbapSystemSizeOfPersistence: 4,
AbapSystemSizeOfRuntime: 4,
AddonDescriptorFileName: "addon.yml",
}

dir := t.TempDir()
oldCWD, _ := os.Getwd()
_ = os.Chdir(dir)
// clean up tmp dir
defer func() {
_ = os.Chdir(oldCWD)
}()

addonYML := `addonProduct: myProduct
addonVersion: 1.2.3
repositories:
- name: '/DMO/REPO'
`

addonYMLBytes := []byte(addonYML)
err := ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)

expectedResult := `create-services:
- broker: testService
name: testName
parameters: '{"admin_email":"[email protected]","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4}'
plan: testPlan
`

resultBytes, err := generateManifestYAML(&config)

if assert.NoError(t, err) {
result := string(resultBytes)
assert.Equal(t, expectedResult, result, "Result not as expected")
}
})

t.Run("Create service with generated manifest - with addon", func(t *testing.T) {
t.Run("Create service with addon - development", func(t *testing.T) {
config := abapEnvironmentCreateSystemOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
Expand Down Expand Up @@ -174,17 +123,11 @@ repositories:
addonYMLBytes := []byte(addonYML)
err := ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)

expectedResult := `create-services:
- broker: testService
name: testName
parameters: '{"admin_email":"[email protected]","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4,"addon_product_name":"myProduct","addon_product_version":"1.2.3","parent_saas_appname":"addon_test"}'
plan: testPlan
`
expectedResult := "{\"admin_email\":\"[email protected]\",\"is_development_allowed\":true,\"sapsystemname\":\"H02\",\"size_of_persistence\":4,\"size_of_runtime\":4,\"addon_product_name\":\"myProduct\",\"addon_product_version\":\"1.2.3\",\"parent_saas_appname\":\"addon_test\"}"

resultBytes, err := generateManifestYAML(&config)
result, err := generateServiceParameterString(&config)

if assert.NoError(t, err) {
result := string(resultBytes)
assert.Equal(t, expectedResult, result, "Result not as expected")
}
})
Expand Down Expand Up @@ -224,22 +167,17 @@ repositories:
addonYMLBytes := []byte(addonYML)
err := ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)

expectedResult := `create-services:
- broker: testService
name: testName
parameters: '{"admin_email":"[email protected]","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4}'
plan: testPlan
`
expectedResult := "{\"admin_email\":\"[email protected]\",\"is_development_allowed\":true,\"sapsystemname\":\"H02\",\"size_of_persistence\":4,\"size_of_runtime\":4}"

resultBytes, err := generateManifestYAML(&config)
result, err := generateServiceParameterString(&config)

if assert.NoError(t, err) {
result := string(resultBytes)
assert.Equal(t, expectedResult, result, "Result not as expected")
}
})

t.Run("Create service with generated manifest - with addon", func(t *testing.T) {
t.Run("Create service with addon - no development", func(t *testing.T) {

config := abapEnvironmentCreateSystemOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
Expand Down Expand Up @@ -275,17 +213,11 @@ repositories:
addonYMLBytes := []byte(addonYML)
err := ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)

expectedResult := `create-services:
- broker: testService
name: testName
parameters: '{"admin_email":"[email protected]","is_development_allowed":false,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4,"addon_product_name":"myProduct","addon_product_version":"1.2.3","parent_saas_appname":"addon_test"}'
plan: testPlan
`
expectedResult := "{\"admin_email\":\"[email protected]\",\"is_development_allowed\":false,\"sapsystemname\":\"H02\",\"size_of_persistence\":4,\"size_of_runtime\":4,\"addon_product_name\":\"myProduct\",\"addon_product_version\":\"1.2.3\",\"parent_saas_appname\":\"addon_test\"}"

resultBytes, err := generateManifestYAML(&config)
result, err := generateServiceParameterString(&config)

if assert.NoError(t, err) {
result := string(resultBytes)
assert.Equal(t, expectedResult, result, "Result not as expected")
}
})
Expand Down
3 changes: 3 additions & 0 deletions cmd/cloudFoundryCreateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func cloudFoundryCreateServiceRequest(config *cloudFoundryCreateServiceOptions,
if config.CfServiceTags != "" {
cfCreateServiceScript = append(cfCreateServiceScript, "-t", config.CfServiceTags)
}
if !config.CfAsync {
cfCreateServiceScript = append(cfCreateServiceScript, "--wait")
}
if config.ServiceManifest != "" && fileExists(config.ServiceManifest) {

cfCreateServiceScript = []string{"create-service-push", "--no-push", "--service-manifest", config.ServiceManifest}
Expand Down
11 changes: 11 additions & 0 deletions cmd/cloudFoundryCreateService_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions cmd/cloudFoundryCreateService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func TestCloudFoundryCreateService(t *testing.T) {
CfService: "testService",
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfAsync: false,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "--wait"}},
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
Expand All @@ -56,6 +57,7 @@ func TestCloudFoundryCreateService(t *testing.T) {
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfServiceTags: "testTag, testTag2",
CfAsync: true,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
Expand All @@ -77,6 +79,7 @@ func TestCloudFoundryCreateService(t *testing.T) {
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfServiceBroker: "testBroker",
CfAsync: true,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
Expand All @@ -98,6 +101,7 @@ func TestCloudFoundryCreateService(t *testing.T) {
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfCreateServiceConfig: "testConfig.json",
CfAsync: true,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
Expand Down Expand Up @@ -126,17 +130,17 @@ func TestCloudFoundryCreateService(t *testing.T) {
_ = os.Chdir(oldCWD)
}()

manifestFileString := `
manifestFileString := `
---
create-services:
- name: ((name))
broker: "testBroker"
plan: "testPlan"
- name: ((name2))
broker: "testBroker"
plan: "testPlan"
- name: "test3"
broker: "testBroker"
plan: "testPlan"`
Expand All @@ -155,6 +159,7 @@ func TestCloudFoundryCreateService(t *testing.T) {
Password: "testPassword",
ServiceManifest: "manifestTest.yml",
ManifestVariables: manifestVariables,
CfAsync: false, // should be ignored
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
Expand All @@ -178,17 +183,17 @@ func TestCloudFoundryCreateService(t *testing.T) {
varsFileString := `name: test1
name2: test2`

manifestFileString := `
manifestFileString := `
---
create-services:
- name: ((name))
broker: "testBroker"
plan: "testPlan"
- name: ((name2))
broker: "testBroker"
plan: "testPlan"
- name: "test3"
broker: "testBroker"
plan: "testPlan"`
Expand Down
Loading

0 comments on commit 8084ce1

Please sign in to comment.