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 
			
		
		
		
	integrationArtifactUpload Fixes (#2987)
* integrationArtifactUpload Fixes * format fixes * Doc fixes * Fix unit tests Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							ee6d46a77c
						
					
				
				
					commit
					a763112fe6
				
			| @@ -73,7 +73,7 @@ func runIntegrationArtifactUpload(config *integrationArtifactUploadOptions, tele | ||||
| 	clientOptions := piperhttp.ClientOptions{} | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("Accept", "application/json") | ||||
| 	iFlowStatusServiceURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion) | ||||
| 	iFlowStatusServiceURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')", serviceKey.OAuth.Host, config.IntegrationFlowID, "Active") | ||||
| 	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient} | ||||
| 	token, err := cpi.CommonUtils.GetBearerToken(tokenParameters) | ||||
| 	if err != nil { | ||||
| @@ -150,10 +150,10 @@ func UploadIntegrationArtifact(config *integrationArtifactUploadOptions, httpCli | ||||
|  | ||||
| //UpdateIntegrationArtifact - Update existing integration artifact | ||||
| func UpdateIntegrationArtifact(config *integrationArtifactUploadOptions, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils, apiHost string) error { | ||||
| 	httpMethod := "POST" | ||||
| 	httpMethod := "PUT" | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("content-type", "application/json") | ||||
| 	updateIflowStatusURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='%s'&SaveAsVersion='%s'", apiHost, config.IntegrationFlowID, config.IntegrationFlowVersion) | ||||
| 	updateIflowStatusURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')", apiHost, config.IntegrationFlowID, "Active") | ||||
| 	payload, jsonError := GetJSONPayloadAsByteArray(config, "update", fileUtils) | ||||
| 	if jsonError != nil { | ||||
| 		return errors.Wrapf(jsonError, "Failed to get json payload for file %v, failed with error", config.FilePath) | ||||
| @@ -193,11 +193,12 @@ func GetJSONPayloadAsByteArray(config *integrationArtifactUploadOptions, mode st | ||||
| 	} | ||||
| 	jsonObj := gabs.New() | ||||
| 	if mode == "create" { | ||||
| 		jsonObj.Set(config.IntegrationFlowID, "Name") | ||||
| 		jsonObj.Set(config.IntegrationFlowName, "Name") | ||||
| 		jsonObj.Set(config.IntegrationFlowID, "Id") | ||||
| 		jsonObj.Set(config.PackageID, "PackageId") | ||||
| 		jsonObj.Set(b64.StdEncoding.EncodeToString(fileContent), "ArtifactContent") | ||||
| 	} else if mode == "update" { | ||||
| 		jsonObj.Set(config.IntegrationFlowName, "Name") | ||||
| 		jsonObj.Set(b64.StdEncoding.EncodeToString(fileContent), "ArtifactContent") | ||||
| 	} else { | ||||
| 		return nil, fmt.Errorf("Unkown node: '%s'", mode) | ||||
|   | ||||
| @@ -15,12 +15,11 @@ import ( | ||||
| ) | ||||
|  | ||||
| type integrationArtifactUploadOptions struct { | ||||
| 	APIServiceKey          string `json:"apiServiceKey,omitempty"` | ||||
| 	IntegrationFlowID      string `json:"integrationFlowId,omitempty"` | ||||
| 	IntegrationFlowVersion string `json:"integrationFlowVersion,omitempty"` | ||||
| 	IntegrationFlowName    string `json:"integrationFlowName,omitempty"` | ||||
| 	PackageID              string `json:"packageId,omitempty"` | ||||
| 	FilePath               string `json:"filePath,omitempty"` | ||||
| 	APIServiceKey       string `json:"apiServiceKey,omitempty"` | ||||
| 	IntegrationFlowID   string `json:"integrationFlowId,omitempty"` | ||||
| 	IntegrationFlowName string `json:"integrationFlowName,omitempty"` | ||||
| 	PackageID           string `json:"packageId,omitempty"` | ||||
| 	FilePath            string `json:"filePath,omitempty"` | ||||
| } | ||||
|  | ||||
| // IntegrationArtifactUploadCommand Upload or Update an integration flow designtime artifact | ||||
| @@ -101,14 +100,12 @@ func IntegrationArtifactUploadCommand() *cobra.Command { | ||||
| func addIntegrationArtifactUploadFlags(cmd *cobra.Command, stepConfig *integrationArtifactUploadOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.APIServiceKey, "apiServiceKey", os.Getenv("PIPER_apiServiceKey"), "Service key JSON string to access the Process Integration Runtime service instance of plan 'api'") | ||||
| 	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.IntegrationFlowVersion, "integrationFlowVersion", os.Getenv("PIPER_integrationFlowVersion"), "Specifies the version of the Integration Flow artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.IntegrationFlowName, "integrationFlowName", os.Getenv("PIPER_integrationFlowName"), "Specifies the Name of the Integration Flow artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.PackageID, "packageId", os.Getenv("PIPER_packageId"), "Specifies the ID of the Integration Package") | ||||
| 	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Specifies integration artifact relative file path.") | ||||
|  | ||||
| 	cmd.MarkFlagRequired("apiServiceKey") | ||||
| 	cmd.MarkFlagRequired("integrationFlowId") | ||||
| 	cmd.MarkFlagRequired("integrationFlowVersion") | ||||
| 	cmd.MarkFlagRequired("integrationFlowName") | ||||
| 	cmd.MarkFlagRequired("packageId") | ||||
| 	cmd.MarkFlagRequired("filePath") | ||||
| @@ -152,15 +149,6 @@ func integrationArtifactUploadMetadata() config.StepData { | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_integrationFlowId"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "integrationFlowVersion", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_integrationFlowVersion"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "integrationFlowName", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
|   | ||||
| @@ -42,12 +42,11 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 		}` | ||||
|  | ||||
| 		config := integrationArtifactUploadOptions{ | ||||
| 			APIServiceKey:          apiServiceKey, | ||||
| 			IntegrationFlowName:    "flow4", | ||||
| 			IntegrationFlowID:      "flow4", | ||||
| 			IntegrationFlowVersion: "1.0.4", | ||||
| 			PackageID:              "CICD", | ||||
| 			FilePath:               path, | ||||
| 			APIServiceKey:       apiServiceKey, | ||||
| 			IntegrationFlowName: "flow4", | ||||
| 			IntegrationFlowID:   "flow4", | ||||
| 			PackageID:           "CICD", | ||||
| 			FilePath:            path, | ||||
| 		} | ||||
|  | ||||
| 		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndCreateIntegrationDesigntimeArtifactResBody"} | ||||
| @@ -57,11 +56,11 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 		if assert.NoError(t, err) { | ||||
|  | ||||
| 			t.Run("check url", func(t *testing.T) { | ||||
| 				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='flow4'&SaveAsVersion='1.0.4'", httpClient.URL) | ||||
| 				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='Active')", httpClient.URL) | ||||
| 			}) | ||||
|  | ||||
| 			t.Run("check method", func(t *testing.T) { | ||||
| 				assert.Equal(t, "POST", httpClient.Method) | ||||
| 				assert.Equal(t, "PUT", httpClient.Method) | ||||
| 			}) | ||||
| 		} | ||||
| 	}) | ||||
| @@ -83,12 +82,11 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactUploadOptions{ | ||||
| 			APIServiceKey:          apiServiceKey, | ||||
| 			IntegrationFlowName:    "flow4", | ||||
| 			IntegrationFlowID:      "flow4", | ||||
| 			IntegrationFlowVersion: "1.0.4", | ||||
| 			PackageID:              "CICD", | ||||
| 			FilePath:               path, | ||||
| 			APIServiceKey:       apiServiceKey, | ||||
| 			IntegrationFlowName: "flow4", | ||||
| 			IntegrationFlowID:   "flow4", | ||||
| 			PackageID:           "CICD", | ||||
| 			FilePath:            path, | ||||
| 		} | ||||
|  | ||||
| 		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndUpdateIntegrationDesigntimeArtifactResBody"} | ||||
| @@ -118,12 +116,11 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactUploadOptions{ | ||||
| 			APIServiceKey:          apiServiceKey, | ||||
| 			IntegrationFlowName:    "flow4", | ||||
| 			IntegrationFlowID:      "flow4", | ||||
| 			IntegrationFlowVersion: "1.0.4", | ||||
| 			PackageID:              "CICD", | ||||
| 			FilePath:               "path", | ||||
| 			APIServiceKey:       apiServiceKey, | ||||
| 			IntegrationFlowName: "flow4", | ||||
| 			IntegrationFlowID:   "flow4", | ||||
| 			PackageID:           "CICD", | ||||
| 			FilePath:            "path", | ||||
| 		} | ||||
|  | ||||
| 		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndGetIntegrationDesigntimeArtifactResBody"} | ||||
| @@ -149,18 +146,17 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactUploadOptions{ | ||||
| 			APIServiceKey:          apiServiceKey, | ||||
| 			IntegrationFlowName:    "flow4", | ||||
| 			IntegrationFlowID:      "flow4", | ||||
| 			IntegrationFlowVersion: "1.0.4", | ||||
| 			PackageID:              "CICD", | ||||
| 			FilePath:               path, | ||||
| 			APIServiceKey:       apiServiceKey, | ||||
| 			IntegrationFlowName: "flow4", | ||||
| 			IntegrationFlowID:   "flow4", | ||||
| 			PackageID:           "CICD", | ||||
| 			FilePath:            path, | ||||
| 		} | ||||
|  | ||||
| 		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndCreateIntegrationDesigntimeArtifactResBody"} | ||||
|  | ||||
| 		err = runIntegrationArtifactUpload(&config, nil, &files, &httpClient) | ||||
| 		assert.EqualError(t, err, "HTTP POST request to https://demo/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='flow4'&SaveAsVersion='1.0.4' failed with error: : 401 Unauthorized") | ||||
| 		assert.EqualError(t, err, "HTTP PUT request to https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='Active') failed with error: : 401 Unauthorized") | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Failed case of Integration Flow Create Test", func(t *testing.T) { | ||||
| @@ -180,12 +176,11 @@ func TestRunIntegrationArtifactUpload(t *testing.T) { | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactUploadOptions{ | ||||
| 			APIServiceKey:          apiServiceKey, | ||||
| 			IntegrationFlowName:    "flow4", | ||||
| 			IntegrationFlowID:      "flow4", | ||||
| 			IntegrationFlowVersion: "1.0.4", | ||||
| 			PackageID:              "CICD", | ||||
| 			FilePath:               path, | ||||
| 			APIServiceKey:       apiServiceKey, | ||||
| 			IntegrationFlowName: "flow4", | ||||
| 			IntegrationFlowID:   "flow4", | ||||
| 			PackageID:           "CICD", | ||||
| 			FilePath:            path, | ||||
| 		} | ||||
|  | ||||
| 		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndUpdateIntegrationDesigntimeArtifactResBody"} | ||||
|   | ||||
| @@ -26,7 +26,6 @@ steps: | ||||
|   integrationArtifactUpload: | ||||
|     cpiApiServiceKeyCredentialsId: 'MY_API_SERVICE_KEY' | ||||
|     integrationFlowId: 'MY_INTEGRATION_FLOW_ID' | ||||
|     integrationFlowVersion: 'MY_INTEGRATION_FLOW_VERSION' | ||||
|     integrationFlowName: 'MY_INTEGRATION_FLOW_Name' | ||||
|     packageId: 'MY_INTEGRATION_Package_ID' | ||||
|     filePath: 'MY_INTEGRATION_FLOW_Artifact_Relative_Path' | ||||
|   | ||||
| @@ -413,7 +413,7 @@ func GetCPIFunctionNameByURLCheck(url, method, testType string) string { | ||||
| 	case "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='1.0.4')": | ||||
| 		return GetFunctionNameByTestTypeAndMethod(method, testType) | ||||
|  | ||||
| 	case "https://demo/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='flow4'&SaveAsVersion='1.0.4'": | ||||
| 	case "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='Active')": | ||||
| 		return GetFunctionNameByTestTypeAndMethod(method, testType) | ||||
|  | ||||
| 	case "https://demo/api/v1/IntegrationDesigntimeArtifacts": | ||||
| @@ -448,10 +448,9 @@ func GetFunctionNameByTestTypeAndMethod(method, testType string) string { | ||||
| 		if method == "GET" { | ||||
| 			return "GetIntegrationDesigntimeArtifact" | ||||
| 		} | ||||
| 		if method == "POST" { | ||||
| 		if method == "PUT" { | ||||
| 			return "UploadIntegrationDesigntimeArtifactNegative" | ||||
| 		} | ||||
|  | ||||
| 	case "NegativeAndUpdateIntegrationDesigntimeArtifactResBody": | ||||
| 		if method == "GET" { | ||||
| 			return "GetIntegrationDesigntimeArtifact" | ||||
| @@ -487,7 +486,7 @@ func GetFunctionNamePositiveAndCreateIntegrationDesigntimeArtifactResBody(method | ||||
| 	if method == "GET" { | ||||
| 		return "GetIntegrationDesigntimeArtifact" | ||||
| 	} | ||||
| 	if method == "POST" { | ||||
| 	if method == "PUT" { | ||||
| 		return "UploadIntegrationDesigntimeArtifact" | ||||
| 	} | ||||
| 	return "" | ||||
|   | ||||
| @@ -31,15 +31,6 @@ spec: | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|       - name: integrationFlowVersion | ||||
|         type: string | ||||
|         description: Specifies the version of the Integration Flow artifact | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - GENERAL | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|       - name: integrationFlowName | ||||
|         type: string | ||||
|         description: Specifies the Name of the Integration Flow artifact | ||||
|   | ||||
		Reference in New Issue
	
	Block a user