From 3ba6320458c69bb2f9434cf14b4ef4abac845b15 Mon Sep 17 00:00:00 2001 From: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com> Date: Wed, 8 Feb 2023 15:51:52 +0100 Subject: [PATCH] Add parameter for async execution (cfCreateServiceKey) (#4209) * Add parameter for async execution * Add default * Adapt tests * Add defaults for ABAP --------- Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com> --- cmd/cloudFoundryCreateServiceKey.go | 10 ++++++++-- cmd/cloudFoundryCreateServiceKey_generated.go | 11 +++++++++++ cmd/cloudFoundryCreateServiceKey_test.go | 14 ++++++++++---- .../pipeline/abapEnvironmentPipelineDefaults.yml | 6 ++++++ .../metadata/cloudFoundryCreateServiceKey.yaml | 10 ++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/cmd/cloudFoundryCreateServiceKey.go b/cmd/cloudFoundryCreateServiceKey.go index cf62ed1924..7cfeb6fbb9 100644 --- a/cmd/cloudFoundryCreateServiceKey.go +++ b/cmd/cloudFoundryCreateServiceKey.go @@ -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) diff --git a/cmd/cloudFoundryCreateServiceKey_generated.go b/cmd/cloudFoundryCreateServiceKey_generated.go index 38175290cd..0fdcdfcfed 100644 --- a/cmd/cloudFoundryCreateServiceKey_generated.go +++ b/cmd/cloudFoundryCreateServiceKey_generated.go @@ -24,6 +24,7 @@ type cloudFoundryCreateServiceKeyOptions struct { CfServiceInstance string `json:"cfServiceInstance,omitempty"` CfServiceKeyName string `json:"cfServiceKeyName,omitempty"` CfServiceKeyConfig string `json:"cfServiceKeyConfig,omitempty"` + CfAsync bool `json:"cfAsync,omitempty"` } // CloudFoundryCreateServiceKeyCommand cloudFoundryCreateServiceKey @@ -129,6 +130,7 @@ func addCloudFoundryCreateServiceKeyFlags(cmd *cobra.Command, stepConfig *cloudF cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter for CloudFoundry Service Instance Name") cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter for Service Key name for CloudFoundry Service Key to be created") cmd.Flags().StringVar(&stepConfig.CfServiceKeyConfig, "cfServiceKeyConfig", os.Getenv("PIPER_cfServiceKeyConfig"), "Path to JSON config file path or JSON in-line string for Cloud Foundry Service Key creation") + cmd.Flags().BoolVar(&stepConfig.CfAsync, "cfAsync", true, "Decides if the service key creation runs asynchronously") cmd.MarkFlagRequired("cfApiEndpoint") cmd.MarkFlagRequired("username") @@ -249,6 +251,15 @@ func cloudFoundryCreateServiceKeyMetadata() config.StepData { Aliases: []config.Alias{{Name: "cloudFoundry/serviceKeyConfig"}}, Default: os.Getenv("PIPER_cfServiceKeyConfig"), }, + { + Name: "cfAsync", + ResourceRef: []config.ResourceReference{}, + Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, + Type: "bool", + Mandatory: false, + Aliases: []config.Alias{}, + Default: true, + }, }, }, Containers: []config.Container{ diff --git a/cmd/cloudFoundryCreateServiceKey_test.go b/cmd/cloudFoundryCreateServiceKey_test.go index 4ded418daf..2f4ff997bc 100644 --- a/cmd/cloudFoundryCreateServiceKey_test.go +++ b/cmd/cloudFoundryCreateServiceKey_test.go @@ -21,6 +21,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { Password: "testPassword", CfServiceInstance: "testInstance", CfServiceKeyName: "testKey", + CfAsync: true, } execRunner := mock.ExecMockRunner{} cfUtilsMock := cloudfoundry.CfUtilsMock{} @@ -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", @@ -42,6 +43,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { CfServiceInstance: "testInstance", CfServiceKeyName: "testKey", CfServiceKeyConfig: "testconfig.yml", + CfAsync: true, } execRunner := mock.ExecMockRunner{} cfUtilsMock := cloudfoundry.CfUtilsMock{} @@ -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", @@ -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{} @@ -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{ @@ -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{ @@ -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) diff --git a/resources/com.sap.piper/pipeline/abapEnvironmentPipelineDefaults.yml b/resources/com.sap.piper/pipeline/abapEnvironmentPipelineDefaults.yml index 7a8fcfec57..6de85ed998 100644 --- a/resources/com.sap.piper/pipeline/abapEnvironmentPipelineDefaults.yml +++ b/resources/com.sap.piper/pipeline/abapEnvironmentPipelineDefaults.yml @@ -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 @@ -45,6 +50,7 @@ stages: includeAddon: 'true' cfServiceKeyName: 'sap_com_0582' cfServiceKeyConfig: '{"scenario_id":"SAP_COM_0582","type":"basic"}' + cfAsync: false cfDeleteServiceKeys: true Confirm: diff --git a/resources/metadata/cloudFoundryCreateServiceKey.yaml b/resources/metadata/cloudFoundryCreateServiceKey.yaml index b410ec8d8e..9ebaea6d49 100644 --- a/resources/metadata/cloudFoundryCreateServiceKey.yaml +++ b/resources/metadata/cloudFoundryCreateServiceKey.yaml @@ -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