1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/integrationArtifactUpdateConfiguration.go

88 lines
3.6 KiB
Go
Raw Normal View History

package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"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 integrationArtifactUpdateConfiguration(config integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData) {
// 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 := runIntegrationArtifactUpdateConfiguration(&config, telemetryData, httpClient)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runIntegrationArtifactUpdateConfiguration(config *integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
clientOptions := piperhttp.ClientOptions{}
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
serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
if err != nil {
return err
}
configUpdateURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$links/Configurations('%s')", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion, config.ParameterKey)
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 := "PUT"
header := make(http.Header)
header.Add("Content-Type", "application/json")
header.Add("Accept", "application/json")
jsonObj := gabs.New()
jsonObj.Set(config.ParameterValue, "ParameterValue")
jsonBody, jsonErr := json.Marshal(jsonObj)
if jsonErr != nil {
return errors.Wrapf(jsonErr, "input json body is invalid for parameterValue %q", config.ParameterValue)
}
configUpdateResp, httpErr := httpClient.SendRequest(httpMethod, configUpdateURL, bytes.NewBuffer(jsonBody), header, nil)
if httpErr != nil {
return errors.Wrapf(httpErr, "HTTP %q request to %q failed with error", httpMethod, configUpdateURL)
}
if configUpdateResp != nil && configUpdateResp.Body != nil {
defer configUpdateResp.Body.Close()
}
if configUpdateResp == nil {
return errors.Errorf("did not retrieve a HTTP response")
}
if configUpdateResp.StatusCode == http.StatusAccepted {
log.Entry().
WithField("IntegrationFlowID", config.IntegrationFlowID).
Info("successfully updated the integration flow configuration parameter")
return nil
}
response, readErr := io.ReadAll(configUpdateResp.Body)
if readErr != nil {
return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", configUpdateResp.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(response), configUpdateResp.StatusCode)
return errors.Errorf("Failed to update the integration flow configuration parameter, Response Status code: %v", configUpdateResp.StatusCode)
}