github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/integrationArtifactUpdateConfiguration.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  
    10  	"github.com/Jeffail/gabs/v2"
    11  	"github.com/SAP/jenkins-library/pkg/cpi"
    12  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    13  	"github.com/SAP/jenkins-library/pkg/log"
    14  	"github.com/SAP/jenkins-library/pkg/telemetry"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func integrationArtifactUpdateConfiguration(config integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData) {
    19  	// Utils can be used wherever the command.ExecRunner interface is expected.
    20  	// It can also be used for example as a mavenExecRunner.
    21  	httpClient := &piperhttp.Client{}
    22  
    23  	// For HTTP calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    24  	// and use a  &piperhttp.Client{} in a custom system
    25  	// Example: step checkmarxExecuteScan.go
    26  
    27  	// Error situations should be bubbled up until they reach the line below which will then stop execution
    28  	// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
    29  	err := runIntegrationArtifactUpdateConfiguration(&config, telemetryData, httpClient)
    30  	if err != nil {
    31  		log.Entry().WithError(err).Fatal("step execution failed")
    32  	}
    33  }
    34  
    35  func runIntegrationArtifactUpdateConfiguration(config *integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
    36  	clientOptions := piperhttp.ClientOptions{}
    37  	serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	configUpdateURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$links/Configurations('%s')", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion, config.ParameterKey)
    42  	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
    43  	token, err := cpi.CommonUtils.GetBearerToken(tokenParameters)
    44  	if err != nil {
    45  		return errors.Wrap(err, "failed to fetch Bearer Token")
    46  	}
    47  	clientOptions.Token = fmt.Sprintf("Bearer %s", token)
    48  	httpClient.SetOptions(clientOptions)
    49  	httpMethod := "PUT"
    50  	header := make(http.Header)
    51  	header.Add("Content-Type", "application/json")
    52  	header.Add("Accept", "application/json")
    53  	jsonObj := gabs.New()
    54  	jsonObj.Set(config.ParameterValue, "ParameterValue")
    55  	jsonBody, jsonErr := json.Marshal(jsonObj)
    56  
    57  	if jsonErr != nil {
    58  		return errors.Wrapf(jsonErr, "input json body is invalid for parameterValue %q", config.ParameterValue)
    59  	}
    60  	configUpdateResp, httpErr := httpClient.SendRequest(httpMethod, configUpdateURL, bytes.NewBuffer(jsonBody), header, nil)
    61  	if httpErr != nil {
    62  		return errors.Wrapf(httpErr, "HTTP %q request to %q failed with error", httpMethod, configUpdateURL)
    63  	}
    64  
    65  	if configUpdateResp != nil && configUpdateResp.Body != nil {
    66  		defer configUpdateResp.Body.Close()
    67  	}
    68  
    69  	if configUpdateResp == nil {
    70  		return errors.Errorf("did not retrieve a HTTP response")
    71  	}
    72  
    73  	if configUpdateResp.StatusCode == http.StatusAccepted {
    74  		log.Entry().
    75  			WithField("IntegrationFlowID", config.IntegrationFlowID).
    76  			Info("successfully updated the integration flow configuration parameter")
    77  		return nil
    78  	}
    79  	response, readErr := io.ReadAll(configUpdateResp.Body)
    80  
    81  	if readErr != nil {
    82  		return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", configUpdateResp.StatusCode)
    83  	}
    84  
    85  	log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(response), configUpdateResp.StatusCode)
    86  	return errors.Errorf("Failed to update the integration flow configuration parameter, Response Status code: %v", configUpdateResp.StatusCode)
    87  }