You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	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>
This commit is contained in:
		| @@ -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) | ||||
|   | ||||
| @@ -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{ | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user