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"
|
2021-03-11 13:03:32 +02:00
|
|
|
"time"
|
2021-01-12 13:17:42 +02:00
|
|
|
|
2021-03-11 13:03:32 +02:00
|
|
|
"github.com/Jeffail/gabs/v2"
|
2021-01-12 13:17:42 +02:00
|
|
|
"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-03-11 13:03:32 +02:00
|
|
|
const retryCount = 14
|
|
|
|
|
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{}
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Add("Accept", "application/json")
|
2021-06-28 10:50:33 +02:00
|
|
|
serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deployURL := fmt.Sprintf("%s/api/v1/DeployIntegrationDesigntimeArtifact?Id='%s'&Version='%s'", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion)
|
|
|
|
|
|
|
|
tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
|
2021-01-12 13:17:42 +02:00
|
|
|
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")
|
2021-06-28 10:50:33 +02:00
|
|
|
deploymentError := pollIFlowDeploymentStatus(retryCount, config, httpClient, serviceKey.OAuth.Host)
|
2021-03-11 13:03:32 +02:00
|
|
|
return deploymentError
|
2021-01-12 13:17:42 +02:00
|
|
|
}
|
2021-01-22 16:55:50 +02:00
|
|
|
responseBody, readErr := ioutil.ReadAll(deployResp.Body)
|
|
|
|
|
|
|
|
if readErr != nil {
|
2021-03-11 13:03:32 +02:00
|
|
|
return errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", deployResp.StatusCode)
|
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-03-11 13:03:32 +02:00
|
|
|
return errors.Errorf("integration flow deployment failed, response Status code: %v", deployResp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
//pollIFlowDeploymentStatus - Poll the integration flow deployment status, return status or error details
|
2021-06-28 10:50:33 +02:00
|
|
|
func pollIFlowDeploymentStatus(retryCount int, config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string) error {
|
2021-03-11 13:03:32 +02:00
|
|
|
|
|
|
|
if retryCount <= 0 {
|
|
|
|
return errors.New("failed to start integration artifact after retrying several times")
|
|
|
|
}
|
2021-06-28 10:50:33 +02:00
|
|
|
deployStatus, err := getIntegrationArtifactDeployStatus(config, httpClient, apiHost)
|
2021-03-11 13:03:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//if artifact starting, then retry based on provided retry count
|
|
|
|
//with specific delay between each retry
|
|
|
|
if deployStatus == "STARTING" {
|
|
|
|
// Calling Sleep method
|
|
|
|
sleepTime := int(retryCount * 3)
|
|
|
|
time.Sleep(time.Duration(sleepTime) * time.Second)
|
|
|
|
retryCount--
|
2021-06-28 10:50:33 +02:00
|
|
|
return pollIFlowDeploymentStatus(retryCount, config, httpClient, apiHost)
|
2021-03-11 13:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//if artifact started, then just return
|
|
|
|
if deployStatus == "STARTED" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//if error return immediately with error details
|
|
|
|
if deployStatus == "ERROR" {
|
2021-06-28 10:50:33 +02:00
|
|
|
resp, err := getIntegrationArtifactDeployError(config, httpClient, apiHost)
|
2021-03-11 13:03:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return errors.New(resp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetHTTPErrorMessage - Return HTTP failure message
|
|
|
|
func getHTTPErrorMessage(httpErr error, response *http.Response, httpMethod, statusURL string) (string, error) {
|
|
|
|
responseBody, readErr := ioutil.ReadAll(response.Body)
|
|
|
|
if readErr != nil {
|
|
|
|
return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", response.StatusCode)
|
|
|
|
}
|
|
|
|
log.Entry().Errorf("a HTTP error occurred! Response body: %v, response status code: %v", string(responseBody), response.StatusCode)
|
|
|
|
return "", errors.Wrapf(httpErr, "HTTP %v request to %v failed with error: %v", httpMethod, statusURL, responseBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
//getIntegrationArtifactDeployStatus - Get integration artifact Deploy Status
|
2021-06-28 10:50:33 +02:00
|
|
|
func getIntegrationArtifactDeployStatus(config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string) (string, error) {
|
2021-03-11 13:03:32 +02:00
|
|
|
httpMethod := "GET"
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Add("content-type", "application/json")
|
|
|
|
header.Add("Accept", "application/json")
|
2021-06-28 10:50:33 +02:00
|
|
|
deployStatusURL := fmt.Sprintf("%s/api/v1/IntegrationRuntimeArtifacts('%s')", apiHost, config.IntegrationFlowID)
|
2021-03-11 13:03:32 +02:00
|
|
|
deployStatusResp, httpErr := httpClient.SendRequest(httpMethod, deployStatusURL, nil, header, nil)
|
|
|
|
|
|
|
|
if deployStatusResp != nil && deployStatusResp.Body != nil {
|
|
|
|
defer deployStatusResp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if deployStatusResp == nil {
|
|
|
|
return "", errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deployStatusResp.StatusCode == http.StatusOK {
|
|
|
|
log.Entry().
|
|
|
|
WithField("IntegrationFlowID", config.IntegrationFlowID).
|
|
|
|
Info("Successfully started integration flow artefact in CPI runtime")
|
|
|
|
|
|
|
|
bodyText, readErr := ioutil.ReadAll(deployStatusResp.Body)
|
|
|
|
if readErr != nil {
|
|
|
|
return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", deployStatusResp.StatusCode)
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
deployStatus := jsonResponse.Path("d.Status").Data().(string)
|
|
|
|
return deployStatus, nil
|
|
|
|
}
|
|
|
|
if httpErr != nil {
|
|
|
|
return getHTTPErrorMessage(httpErr, deployStatusResp, httpMethod, deployStatusURL)
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("failed to get Integration Flow artefact runtime status, response Status code: %v", deployStatusResp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
//getIntegrationArtifactDeployError - Get integration artifact deploy error details
|
2021-06-28 10:50:33 +02:00
|
|
|
func getIntegrationArtifactDeployError(config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string) (string, error) {
|
2021-03-11 13:03:32 +02:00
|
|
|
httpMethod := "GET"
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Add("content-type", "application/json")
|
2021-06-28 10:50:33 +02:00
|
|
|
errorStatusURL := fmt.Sprintf("%s/api/v1/IntegrationRuntimeArtifacts('%s')/ErrorInformation/$value", apiHost, config.IntegrationFlowID)
|
2021-03-11 13:03:32 +02:00
|
|
|
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("IntegrationFlowID", config.IntegrationFlowID).
|
|
|
|
Info("Successfully retrieved Integration Flow artefact deploy error details")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", responseBody, errorStatusResp.StatusCode)
|
|
|
|
errorDetails := string(responseBody)
|
|
|
|
return errorDetails, nil
|
|
|
|
}
|
|
|
|
if httpErr != nil {
|
|
|
|
return getHTTPErrorMessage(httpErr, errorStatusResp, httpMethod, errorStatusURL)
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("failed to get Integration Flow artefact deploy error details, response Status code: %v", errorStatusResp.StatusCode)
|
2021-01-12 13:17:42 +02:00
|
|
|
}
|