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 
			
		
		
		
	GetIntegrationArtifactMPLError function (#3000)
* GetIntegrationArtifactMPLError function * formating fixes * formating fixes * formatting fixes * formatting fixes * Formatting fixes * formatting fixes * Code Review Fixes * Code Review Fixes * Code Review Fixes * Code Review Fixes
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							7259ccc726
						
					
				
				
					commit
					2f2fd84193
				
			| @@ -106,8 +106,20 @@ func runIntegrationArtifactGetMplStatus( | ||||
| 		if parsingErr != nil { | ||||
| 			return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText)) | ||||
| 		} | ||||
| 		mplStatus := jsonResponse.Path("d.results.0.Status").Data().(string) | ||||
| 		commonPipelineEnvironment.custom.iFlowMplStatus = mplStatus | ||||
| 		if jsonResponse.Exists("d", "results", "0") { | ||||
| 			mplStatus := jsonResponse.Path("d.results.0.Status").Data().(string) | ||||
| 			commonPipelineEnvironment.custom.iFlowMplStatus = mplStatus | ||||
|  | ||||
| 			//if error, then return immediately with the error details | ||||
| 			if mplStatus == "FAILED" { | ||||
| 				mplID := jsonResponse.Path("d.results.0.MessageGuid").Data().(string) | ||||
| 				resp, err := getIntegrationArtifactMPLError(commonPipelineEnvironment, mplID, httpClient, serviceKey.OAuth.Host) | ||||
| 				if err != nil { | ||||
| 					return err | ||||
| 				} | ||||
| 				return errors.New(resp) | ||||
| 			} | ||||
| 		} | ||||
| 		return nil | ||||
| 	} | ||||
| 	responseBody, readErr := ioutil.ReadAll(mplStatusResp.Body) | ||||
| @@ -119,3 +131,37 @@ func runIntegrationArtifactGetMplStatus( | ||||
| 	log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", responseBody, mplStatusResp.StatusCode) | ||||
| 	return errors.Errorf("Unable to get integration flow MPL status, Response Status code: %v", mplStatusResp.StatusCode) | ||||
| } | ||||
|  | ||||
| //getIntegrationArtifactMPLError - Get integration artifact MPL error details | ||||
| func getIntegrationArtifactMPLError(commonPipelineEnvironment *integrationArtifactGetMplStatusCommonPipelineEnvironment, mplID string, httpClient piperhttp.Sender, apiHost string) (string, error) { | ||||
| 	httpMethod := "GET" | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("content-type", "application/json") | ||||
| 	errorStatusURL := fmt.Sprintf("%s/api/v1/MessageProcessingLogs('%s')/ErrorInformation/$value", apiHost, mplID) | ||||
| 	errorStatusResp, httpErr := httpClient.SendRequest(httpMethod, errorStatusURL, nil, header, nil) | ||||
|  | ||||
| 	if errorStatusResp != nil && errorStatusResp.Body != nil { | ||||
| 		defer errorStatusResp.Body.Close() | ||||
| 	} | ||||
|  | ||||
| 	if errorStatusResp == nil { | ||||
| 		return "", errors.Errorf("did not retrieve a HTTP response: %v", httpErr) | ||||
| 	} | ||||
|  | ||||
| 	if errorStatusResp.StatusCode == http.StatusOK { | ||||
| 		log.Entry(). | ||||
| 			WithField("MPLID", mplID). | ||||
| 			Info("Successfully retrieved Integration Flow artefact message processing error") | ||||
| 		responseBody, readErr := ioutil.ReadAll(errorStatusResp.Body) | ||||
| 		if readErr != nil { | ||||
| 			return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", errorStatusResp.StatusCode) | ||||
| 		} | ||||
| 		mplErrorDetails := string(responseBody) | ||||
| 		commonPipelineEnvironment.custom.iFlowMplError = mplErrorDetails | ||||
| 		return mplErrorDetails, nil | ||||
| 	} | ||||
| 	if httpErr != nil { | ||||
| 		return getHTTPErrorMessage(httpErr, errorStatusResp, httpMethod, errorStatusURL) | ||||
| 	} | ||||
| 	return "", errors.Errorf("failed to get Integration Flow artefact message processing error, response Status code: %v", errorStatusResp.StatusCode) | ||||
| } | ||||
|   | ||||
| @@ -24,6 +24,7 @@ type integrationArtifactGetMplStatusOptions struct { | ||||
| type integrationArtifactGetMplStatusCommonPipelineEnvironment struct { | ||||
| 	custom struct { | ||||
| 		iFlowMplStatus string | ||||
| 		iFlowMplError  string | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -34,6 +35,7 @@ func (p *integrationArtifactGetMplStatusCommonPipelineEnvironment) persist(path, | ||||
| 		value    interface{} | ||||
| 	}{ | ||||
| 		{category: "custom", name: "iFlowMplStatus", value: p.custom.iFlowMplStatus}, | ||||
| 		{category: "custom", name: "iFlowMplError", value: p.custom.iFlowMplError}, | ||||
| 	} | ||||
|  | ||||
| 	errCount := 0 | ||||
| @@ -181,6 +183,7 @@ func integrationArtifactGetMplStatusMetadata() config.StepData { | ||||
| 						Type: "piperEnvironment", | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "custom/iFlowMplStatus"}, | ||||
| 							{"Name": "custom/iFlowMplError"}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|   | ||||
| @@ -1,8 +1,10 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	piperhttp "github.com/SAP/jenkins-library/pkg/http" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| @@ -78,4 +80,16 @@ func TestRunIntegrationArtifactGetMplStatus(t *testing.T) { | ||||
| 			"Id+eq+'flow1'+and+Status+ne+'DISCARDED'&$orderby=LogEnd+desc&$top=1 failed with error: "+ | ||||
| 			"Unable to get integration flow MPL status, Response Status code:400") | ||||
| 	}) | ||||
|  | ||||
| 	t.Run(" Integration flow message processing get Error message test", func(t *testing.T) { | ||||
| 		clientOptions := piperhttp.ClientOptions{} | ||||
| 		clientOptions.Token = fmt.Sprintf("Bearer %s", "Demo") | ||||
| 		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactGetMplStatusError", Options: clientOptions, ResponseBody: ``, TestType: "Negative"} | ||||
| 		seOut := integrationArtifactGetMplStatusCommonPipelineEnvironment{} | ||||
| 		message, err := getIntegrationArtifactMPLError(&seOut, "1000111", &httpClient, "demo") | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotNil(t, message) | ||||
| 		assert.EqualValues(t, seOut.custom.iFlowMplError, "{\"message\": \"java.lang.IllegalStateException: No credentials for 'smtp' found\"}") | ||||
| 	}) | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -50,6 +50,8 @@ func GetCPIFunctionMockResponse(functionName, testType string) (*http.Response, | ||||
| 		return GetIntegrationArtifactDeployErrorDetailsMockResponse(testType) | ||||
| 	case "TriggerIntegrationTest": | ||||
| 		return TriggerIntegrationTestMockResponse(testType) | ||||
| 	case "IntegrationArtifactGetMplStatusError": | ||||
| 		return GetIntegrationArtifactDeployErrorStatusMockResponseBody() | ||||
| 	default: | ||||
| 		res := http.Response{ | ||||
| 			StatusCode: 404, | ||||
|   | ||||
| @@ -37,3 +37,4 @@ spec: | ||||
|         type: piperEnvironment | ||||
|         params: | ||||
|           - name: custom/iFlowMplStatus | ||||
|           - name: custom/iFlowMplError | ||||
|   | ||||
		Reference in New Issue
	
	Block a user