Skip to content

Commit

Permalink
Add parameter for async execution (cfCreateServiceKey) (SAP#4209)
Browse files Browse the repository at this point in the history
* Add parameter for async execution

* Add default

* Adapt tests

* Add defaults for ABAP

---------

Co-authored-by: tiloKo <[email protected]>
  • Loading branch information
2 people authored and maxatsap committed Jul 23, 2024
1 parent 8d14577 commit 3ba6320
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
10 changes: 8 additions & 2 deletions cmd/cloudFoundryCreateServiceKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ func runCloudFoundryCreateServiceKey(options *cloudFoundryCreateServiceKeyOption
var cfCreateServiceKeyScript []string
// 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
if options.CfServiceKeyConfig == "" {
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, cfCliSynchronousRequestFlag}
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName}
} else {
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig, cfCliSynchronousRequestFlag}
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig}
}

// If a synchronous execution is requested, the "--wait" flag needs to be added
if !options.CfAsync {
cfCreateServiceKeyScript = append(cfCreateServiceKeyScript, cfCliSynchronousRequestFlag)
}

err := c.RunExecutable("cf", cfCreateServiceKeyScript...)
if err != nil {
return fmt.Errorf("Failed to Create Service Key: %w", err)
Expand Down
11 changes: 11 additions & 0 deletions cmd/cloudFoundryCreateServiceKey_generated.go

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

14 changes: 10 additions & 4 deletions cmd/cloudFoundryCreateServiceKey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
Password: "testPassword",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfAsync: true,
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
Expand All @@ -29,10 +30,10 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", cfCliSynchronousRequestFlag}, execRunner.Calls[0].Params)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey"}, execRunner.Calls[0].Params)
}
})
t.Run("CF Create Service Key with service Key config: Success case", func(t *testing.T) {
t.Run("CF Create Service Key asynchronous with service Key config: Success case", func(t *testing.T) {
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
Expand All @@ -42,6 +43,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "testconfig.yml",
CfAsync: true,
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
Expand All @@ -50,10 +52,10 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml", cfCliSynchronousRequestFlag}, execRunner.Calls[0].Params)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml"}, execRunner.Calls[0].Params)
}
})
t.Run("CF Create Service Key with service Key config: Success case", func(t *testing.T) {
t.Run("CF Create Service Key synchronous with service Key config: Success case", func(t *testing.T) {
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
Expand All @@ -63,6 +65,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
CfAsync: false,
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
Expand All @@ -89,6 +92,7 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
CfAsync: true,
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{
Expand All @@ -110,6 +114,7 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
CfAsync: true,
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{
Expand All @@ -132,6 +137,7 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
CfAsync: false,
}
m := make(map[string]error)
m["cf create-service-key testInstance testKey -c {\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"} --wait"] = errors.New(errorMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ stages:
abapSystemSizeOfPersistence: 4
abapSystemSizeOfRuntime: 1
cfServiceKeyName: 'sap_com_0510'
cfAsync: false

'Clone Repositories':
cfServiceKeyName: 'sap_com_0510'
cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0510","type":"basic"}'
cfAsync: false
ordinal: 30

'ATC':
ordinal: 40
cfServiceKeyName: 'sap_com_0901'
cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0901","type":"basic"}'
cfAsync: false

'AUnit':
ordinal: 50
cfServiceKeyName: 'sap_com_0735'
cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0735","type":"basic"}'
cfAsync: false

Build:
ordinal: 60
cfServiceKeyName: 'sap_com_0582'
cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0582","type":"basic"}'
cfAsync: false

'Integration Tests':
ordinal: 70
Expand All @@ -45,6 +50,7 @@ stages:
includeAddon: 'true'
cfServiceKeyName: 'sap_com_0582'
cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0582","type":"basic"}'
cfAsync: false
cfDeleteServiceKeys: true

Confirm:
Expand Down
10 changes: 10 additions & 0 deletions resources/metadata/cloudFoundryCreateServiceKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ spec:
mandatory: false
aliases:
- name: cloudFoundry/serviceKeyConfig
- name: cfAsync
type: bool
description: Decides if the service key creation runs asynchronously
scope:
- GENERAL
- PARAMETERS
- STAGES
- STEPS
mandatory: false
default: true
containers:
- name: cf
image: ppiper/cf-cli:latest

0 comments on commit 3ba6320

Please sign in to comment.