1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/integrationArtifactGetMplStatus.go

142 lines
5.9 KiB
Go
Raw Normal View History

package cmd
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/Jeffail/gabs/v2"
"github.com/SAP/jenkins-library/pkg/cpi"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
func integrationArtifactGetMplStatus(config integrationArtifactGetMplStatusOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *integrationArtifactGetMplStatusCommonPipelineEnvironment) {
// Utils can be used wherever the command.ExecRunner interface is expected.
// It can also be used for example as a mavenExecRunner.
httpClient := &piperhttp.Client{}
// For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
// Error situations should be bubbled up until they reach the line below which will then stop execution
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
err := runIntegrationArtifactGetMplStatus(&config, telemetryData, httpClient, commonPipelineEnvironment)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
CPI - Introduce service key (#2901) * Switch to service key for CPI GetMplStatus Introduces read method for service key files, mock utils and tests. * Use secret text instead of file * Change serviceKey definition * Update cpiUpload to use Service Key retrieved the host and uaa information from service key * Update cpiDeploy to use service key retrieved the host and uaa information from service key * Update cpiServiceEndpoint to use Service Key retrieved the host and uaa information from service key * Update cpiDownload to use Service Key retrieved the host and uaa information from service key * Update cpiUpdateConfig to use Service Key retrieved the host and uaa information from service key * Refactor serviceKey var name * Fixed references to service key to follow the real format they should be accessed through oauth instead of uaa because of the format of the json * Rename ServiceKey to APIServiceKey To support having a different service key(and for readability), we need to change the name to API. * Add STAGES and STEPS yaml add in to each yaml file of cpi integration * Revert "Add STAGES and STEPS yaml" This reverts commit aa2665d158b0f864cfee95ff999a7dc8ea3477f1. * Change comments/formatting commonUtils Make comments more understandable and follow code climate suggestions * Change documentation files for steps remove OAuth and host and change credentials to be servicekey Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
2021-06-28 10:50:33 +02:00
func runIntegrationArtifactGetMplStatus(
config *integrationArtifactGetMplStatusOptions,
telemetryData *telemetry.CustomData,
httpClient piperhttp.Sender,
commonPipelineEnvironment *integrationArtifactGetMplStatusCommonPipelineEnvironment) error {
serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
if err != nil {
return err
}
clientOptions := piperhttp.ClientOptions{}
httpClient.SetOptions(clientOptions)
header := make(http.Header)
header.Add("Accept", "application/json")
mplStatusEncodedURL := fmt.Sprintf("%s/api/v1/MessageProcessingLogs?$filter=IntegrationArtifact/Id"+url.QueryEscape(" eq ")+"'%s'"+
url.QueryEscape(" and Status ne ")+"'DISCARDED'"+"&$orderby="+url.QueryEscape("LogEnd desc")+"&$top=1", serviceKey.OAuth.Host, config.IntegrationFlowID)
CPI - Introduce service key (#2901) * Switch to service key for CPI GetMplStatus Introduces read method for service key files, mock utils and tests. * Use secret text instead of file * Change serviceKey definition * Update cpiUpload to use Service Key retrieved the host and uaa information from service key * Update cpiDeploy to use service key retrieved the host and uaa information from service key * Update cpiServiceEndpoint to use Service Key retrieved the host and uaa information from service key * Update cpiDownload to use Service Key retrieved the host and uaa information from service key * Update cpiUpdateConfig to use Service Key retrieved the host and uaa information from service key * Refactor serviceKey var name * Fixed references to service key to follow the real format they should be accessed through oauth instead of uaa because of the format of the json * Rename ServiceKey to APIServiceKey To support having a different service key(and for readability), we need to change the name to API. * Add STAGES and STEPS yaml add in to each yaml file of cpi integration * Revert "Add STAGES and STEPS yaml" This reverts commit aa2665d158b0f864cfee95ff999a7dc8ea3477f1. * Change comments/formatting commonUtils Make comments more understandable and follow code climate suggestions * Change documentation files for steps remove OAuth and host and change credentials to be servicekey Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
2021-06-28 10:50:33 +02:00
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 {
return errors.Wrap(err, "failed to fetch Bearer Token")
}
clientOptions.Token = fmt.Sprintf("Bearer %s", token)
httpClient.SetOptions(clientOptions)
httpMethod := "GET"
mplStatusResp, httpErr := httpClient.SendRequest(httpMethod, mplStatusEncodedURL, nil, header, nil)
if httpErr != nil {
return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, mplStatusEncodedURL)
}
if mplStatusResp != nil && mplStatusResp.Body != nil {
defer mplStatusResp.Body.Close()
}
if mplStatusResp == nil {
return errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
}
if mplStatusResp.StatusCode == 200 {
bodyText, readErr := ioutil.ReadAll(mplStatusResp.Body)
if readErr != nil {
return errors.Wrap(readErr, "HTTP response body could not be read")
}
jsonResponse, parsingErr := gabs.ParseJSON([]byte(bodyText))
if parsingErr != nil {
return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText))
}
2021-08-04 14:15:52 +02:00
if jsonResponse == nil {
return errors.Errorf("Empty json response: %v", string(bodyText))
}
if jsonResponse.Exists("d", "results", "0") {
mplStatus := jsonResponse.Path("d.results.0.Status").Data().(string)
commonPipelineEnvironment.custom.integrationFlowMplStatus = 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)
if readErr != nil {
return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", mplStatusResp.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(responseBody), mplStatusResp.StatusCode)
return errors.Errorf("Unable to get integration flow MPL status, Response Status code: %v", mplStatusResp.StatusCode)
}
2022-11-08 09:47:38 +02:00
// 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.integrationFlowMplError = 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)
}