github.com/jaylevin/jenkins-library@v1.230.4/cmd/integrationArtifactUpdateConfiguration.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  
    10  	"github.com/Jeffail/gabs/v2"
    11  	"github.com/SAP/jenkins-library/pkg/command"
    12  	"github.com/SAP/jenkins-library/pkg/cpi"
    13  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    14  	"github.com/SAP/jenkins-library/pkg/log"
    15  	"github.com/SAP/jenkins-library/pkg/telemetry"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  type integrationArtifactUpdateConfigurationUtils interface {
    20  	command.ExecRunner
    21  
    22  	// Add more methods here, or embed additional interfaces, or remove/replace as required.
    23  	// The integrationArtifactUpdateConfigurationUtils interface should be descriptive of your runtime dependencies,
    24  	// i.e. include everything you need to be able to mock in tests.
    25  	// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
    26  }
    27  
    28  type integrationArtifactUpdateConfigurationUtilsBundle struct {
    29  	*command.Command
    30  
    31  	// Embed more structs as necessary to implement methods or interfaces you add to integrationArtifactUpdateConfigurationUtils.
    32  	// Structs embedded in this way must each have a unique set of methods attached.
    33  	// If there is no struct which implements the method you need, attach the method to
    34  	// integrationArtifactUpdateConfigurationUtilsBundle and forward to the implementation of the dependency.
    35  }
    36  
    37  func newIntegrationArtifactUpdateConfigurationUtils() integrationArtifactUpdateConfigurationUtils {
    38  	utils := integrationArtifactUpdateConfigurationUtilsBundle{
    39  		Command: &command.Command{},
    40  	}
    41  	// Reroute command output to logging framework
    42  	utils.Stdout(log.Writer())
    43  	utils.Stderr(log.Writer())
    44  	return &utils
    45  }
    46  
    47  func integrationArtifactUpdateConfiguration(config integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData) {
    48  	// Utils can be used wherever the command.ExecRunner interface is expected.
    49  	// It can also be used for example as a mavenExecRunner.
    50  	httpClient := &piperhttp.Client{}
    51  
    52  	// For HTTP calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    53  	// and use a  &piperhttp.Client{} in a custom system
    54  	// Example: step checkmarxExecuteScan.go
    55  
    56  	// Error situations should be bubbled up until they reach the line below which will then stop execution
    57  	// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
    58  	err := runIntegrationArtifactUpdateConfiguration(&config, telemetryData, httpClient)
    59  	if err != nil {
    60  		log.Entry().WithError(err).Fatal("step execution failed")
    61  	}
    62  }
    63  
    64  func runIntegrationArtifactUpdateConfiguration(config *integrationArtifactUpdateConfigurationOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
    65  	clientOptions := piperhttp.ClientOptions{}
    66  	serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	configUpdateURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$links/Configurations('%s')", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion, config.ParameterKey)
    71  	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
    72  	token, err := cpi.CommonUtils.GetBearerToken(tokenParameters)
    73  	if err != nil {
    74  		return errors.Wrap(err, "failed to fetch Bearer Token")
    75  	}
    76  	clientOptions.Token = fmt.Sprintf("Bearer %s", token)
    77  	httpClient.SetOptions(clientOptions)
    78  	httpMethod := "PUT"
    79  	header := make(http.Header)
    80  	header.Add("Content-Type", "application/json")
    81  	header.Add("Accept", "application/json")
    82  	jsonObj := gabs.New()
    83  	jsonObj.Set(config.ParameterValue, "ParameterValue")
    84  	jsonBody, jsonErr := json.Marshal(jsonObj)
    85  
    86  	if jsonErr != nil {
    87  		return errors.Wrapf(jsonErr, "input json body is invalid for parameterValue %q", config.ParameterValue)
    88  	}
    89  	configUpdateResp, httpErr := httpClient.SendRequest(httpMethod, configUpdateURL, bytes.NewBuffer(jsonBody), header, nil)
    90  	if httpErr != nil {
    91  		return errors.Wrapf(httpErr, "HTTP %q request to %q failed with error", httpMethod, configUpdateURL)
    92  	}
    93  
    94  	if configUpdateResp != nil && configUpdateResp.Body != nil {
    95  		defer configUpdateResp.Body.Close()
    96  	}
    97  
    98  	if configUpdateResp == nil {
    99  		return errors.Errorf("did not retrieve a HTTP response")
   100  	}
   101  
   102  	if configUpdateResp.StatusCode == http.StatusAccepted {
   103  		log.Entry().
   104  			WithField("IntegrationFlowID", config.IntegrationFlowID).
   105  			Info("successfully updated the integration flow configuration parameter")
   106  		return nil
   107  	}
   108  	response, readErr := ioutil.ReadAll(configUpdateResp.Body)
   109  
   110  	if readErr != nil {
   111  		return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", configUpdateResp.StatusCode)
   112  	}
   113  
   114  	log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(response), configUpdateResp.StatusCode)
   115  	return errors.Errorf("Failed to update the integration flow configuration parameter, Response Status code: %v", configUpdateResp.StatusCode)
   116  }