2021-01-12 13:17:42 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-22 16:55:50 +02:00
|
|
|
"io/ioutil"
|
2021-01-12 13:17:42 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
2021-01-22 16:55:50 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/cpi"
|
2021-01-12 13:17:42 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
type integrationArtifactDeployUtils interface {
|
2021-01-12 13:17:42 +02:00
|
|
|
command.ExecRunner
|
|
|
|
|
|
|
|
// Add more methods here, or embed additional interfaces, or remove/replace as required.
|
2021-01-22 16:55:50 +02:00
|
|
|
// The integrationArtifactDeployUtils interface should be descriptive of your runtime dependencies,
|
2021-01-12 13:17:42 +02:00
|
|
|
// i.e. include everything you need to be able to mock in tests.
|
|
|
|
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
type integrationArtifactDeployUtilsBundle struct {
|
2021-01-12 13:17:42 +02:00
|
|
|
*command.Command
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
// Embed more structs as necessary to implement methods or interfaces you add to integrationArtifactDeployUtils.
|
2021-01-12 13:17:42 +02:00
|
|
|
// Structs embedded in this way must each have a unique set of methods attached.
|
|
|
|
// If there is no struct which implements the method you need, attach the method to
|
2021-01-22 16:55:50 +02:00
|
|
|
// integrationArtifactDeployUtilsBundle and forward to the implementation of the dependency.
|
2021-01-12 13:17:42 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
func newIntegrationArtifactDeployUtils() integrationArtifactDeployUtils {
|
|
|
|
utils := integrationArtifactDeployUtilsBundle{
|
2021-01-12 13:17:42 +02:00
|
|
|
Command: &command.Command{},
|
|
|
|
}
|
|
|
|
// Reroute command output to logging framework
|
|
|
|
utils.Stdout(log.Writer())
|
|
|
|
utils.Stderr(log.Writer())
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
func integrationArtifactDeploy(config integrationArtifactDeployOptions, telemetryData *telemetry.CustomData) {
|
2021-01-12 13:17:42 +02:00
|
|
|
// Utils can be used wherever the command.ExecRunner interface is expected.
|
|
|
|
// It can also be used for example as a mavenExecRunner.
|
2021-01-22 16:55:50 +02:00
|
|
|
utils := newIntegrationArtifactDeployUtils()
|
2021-01-12 13:17:42 +02:00
|
|
|
utils.Stdout(log.Writer())
|
|
|
|
httpClient := &piperhttp.Client{}
|
2021-01-22 16:55:50 +02:00
|
|
|
|
2021-01-12 13:17:42 +02:00
|
|
|
// 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.
|
2021-01-22 16:55:50 +02:00
|
|
|
err := runIntegrationArtifactDeploy(&config, telemetryData, httpClient)
|
2021-01-12 13:17:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
func runIntegrationArtifactDeploy(config *integrationArtifactDeployOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
|
2021-01-12 13:17:42 +02:00
|
|
|
clientOptions := piperhttp.ClientOptions{}
|
|
|
|
httpClient.SetOptions(clientOptions)
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
deployURL := fmt.Sprintf("%s/api/v1/DeployIntegrationDesigntimeArtifact?Id='%s'&Version='%s'", config.Host, config.IntegrationFlowID, config.IntegrationFlowVersion)
|
|
|
|
tokenParameters := cpi.TokenParameters{TokenURL: config.OAuthTokenProviderURL, Username: config.Username, Password: config.Password, 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 := "POST"
|
|
|
|
deployResp, httpErr := httpClient.SendRequest(httpMethod, deployURL, nil, header, nil)
|
|
|
|
if httpErr != nil {
|
|
|
|
return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, deployURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deployResp != nil && deployResp.Body != nil {
|
|
|
|
defer deployResp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if deployResp == nil {
|
|
|
|
return errors.Errorf("did not retrieve a HTTP response")
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
if deployResp.StatusCode == http.StatusAccepted {
|
2021-01-12 13:17:42 +02:00
|
|
|
log.Entry().
|
|
|
|
WithField("IntegrationFlowID", config.IntegrationFlowID).
|
|
|
|
Info("successfully deployed into CPI runtime")
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 16:55:50 +02:00
|
|
|
responseBody, readErr := ioutil.ReadAll(deployResp.Body)
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code : %v", deployResp.StatusCode)
|
|
|
|
}
|
2021-01-12 13:17:42 +02:00
|
|
|
|
2021-01-22 16:55:50 +02:00
|
|
|
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code : %v", responseBody, deployResp.StatusCode)
|
2021-01-12 13:17:42 +02:00
|
|
|
return errors.Errorf("Integration Flow deployment failed, Response Status code: %v", deployResp.StatusCode)
|
|
|
|
}
|