1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

IntegrationArtifactDownload Command (#2587)

* IntegrationArtifactDownload Command

Co-authored-by: Marcus Holl <marcus.holl@sap.com>
This commit is contained in:
Mayur Belur Mohan
2021-02-10 21:38:23 +05:30
committed by GitHub
parent d47a17c8fc
commit f030bc9861
12 changed files with 572 additions and 0 deletions

View File

@@ -0,0 +1,118 @@
package cmd
import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path/filepath"
"github.com/SAP/jenkins-library/pkg/command"
"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"
)
type integrationArtifactDownloadUtils interface {
command.ExecRunner
// Add more methods here, or embed additional interfaces, or remove/replace as required.
// The integrationArtifactDownloadUtils interface should be descriptive of your runtime dependencies,
// 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.
}
type integrationArtifactDownloadUtilsBundle struct {
*command.Command
// Embed more structs as necessary to implement methods or interfaces you add to integrationArtifactDownloadUtils.
// 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
// integrationArtifactDownloadUtilsBundle and forward to the implementation of the dependency.
}
func newIntegrationArtifactDownloadUtils() integrationArtifactDownloadUtils {
utils := integrationArtifactDownloadUtilsBundle{
Command: &command.Command{},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func integrationArtifactDownload(config integrationArtifactDownloadOptions, 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 := runIntegrationArtifactDownload(&config, telemetryData, httpClient)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runIntegrationArtifactDownload(config *integrationArtifactDownloadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
clientOptions := piperhttp.ClientOptions{}
header := make(http.Header)
header.Add("Accept", "application/zip")
downloadArtifactURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$value", 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 := "GET"
downloadResp, httpErr := httpClient.SendRequest(httpMethod, downloadArtifactURL, nil, header, nil)
if httpErr != nil {
return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, downloadArtifactURL)
}
if downloadResp == nil {
return errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
}
contentDisposition := downloadResp.Header.Get("Content-Disposition")
disposition, params, err := mime.ParseMediaType(contentDisposition)
if err != nil {
return errors.Wrapf(err, "failed to read filename from http response headers, Content-Disposition "+disposition)
}
filename := params["filename"]
if downloadResp != nil && downloadResp.Body != nil {
defer downloadResp.Body.Close()
}
if downloadResp.StatusCode == 200 {
workspaceRelativePath := config.DownloadPath
err = os.MkdirAll(workspaceRelativePath, 755)
if err != nil {
return errors.Wrapf(err, "Failed to create workspace directory")
}
zipFileName := filepath.Join(workspaceRelativePath, filename)
file, err := os.Create(zipFileName)
if err != nil {
return errors.Wrapf(err, "Failed to create integration flow artifact file")
}
io.Copy(file, downloadResp.Body)
return nil
}
responseBody, readErr := ioutil.ReadAll(downloadResp.Body)
if readErr != nil {
return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code : %v", downloadResp.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code : %v", responseBody, downloadResp.StatusCode)
return errors.Errorf("Integration Flow artifact download failed, Response Status code: %v", downloadResp.StatusCode)
}

View File

@@ -0,0 +1,186 @@
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type integrationArtifactDownloadOptions struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
IntegrationFlowID string `json:"integrationFlowId,omitempty"`
IntegrationFlowVersion string `json:"integrationFlowVersion,omitempty"`
Host string `json:"host,omitempty"`
OAuthTokenProviderURL string `json:"oAuthTokenProviderUrl,omitempty"`
DownloadPath string `json:"downloadPath,omitempty"`
}
// IntegrationArtifactDownloadCommand Download integration flow runtime artefact
func IntegrationArtifactDownloadCommand() *cobra.Command {
const STEP_NAME = "integrationArtifactDownload"
metadata := integrationArtifactDownloadMetadata()
var stepConfig integrationArtifactDownloadOptions
var startTime time.Time
var createIntegrationArtifactDownloadCmd = &cobra.Command{
Use: STEP_NAME,
Short: "Download integration flow runtime artefact",
Long: `With this step you can download a integration flow runtime artifact, which returns a zip file with the integration flow contents in to current workspace using the OData API. Learn more about the SAP Cloud Integration remote API for downloading an integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/460dee19f9c141e88ba0454ce892247a.html).`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
startTime = time.Now()
log.SetStepName(STEP_NAME)
log.SetVerbose(GeneralConfig.Verbose)
path, _ := os.Getwd()
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
log.RegisterHook(fatalHook)
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return err
}
log.RegisterSecret(stepConfig.Username)
log.RegisterSecret(stepConfig.Password)
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
log.RegisterHook(&sentryHook)
}
return nil
},
Run: func(_ *cobra.Command, _ []string) {
telemetryData := telemetry.CustomData{}
telemetryData.ErrorCode = "1"
handler := func() {
config.RemoveVaultSecretFiles()
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
telemetryData.ErrorCategory = log.GetErrorCategory().String()
telemetry.Send(&telemetryData)
}
log.DeferExitHandler(handler)
defer handler()
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
integrationArtifactDownload(stepConfig, &telemetryData)
telemetryData.ErrorCode = "0"
log.Entry().Info("SUCCESS")
},
}
addIntegrationArtifactDownloadFlags(createIntegrationArtifactDownloadCmd, &stepConfig)
return createIntegrationArtifactDownloadCmd
}
func addIntegrationArtifactDownloadFlags(cmd *cobra.Command, stepConfig *integrationArtifactDownloadOptions) {
cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User to authenticate to the SAP Cloud Platform Integration Service")
cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password to authenticate to the SAP Cloud Platform Integration Service")
cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
cmd.Flags().StringVar(&stepConfig.IntegrationFlowVersion, "integrationFlowVersion", os.Getenv("PIPER_integrationFlowVersion"), "Specifies the version of the Integration Flow artifact")
cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
cmd.Flags().StringVar(&stepConfig.OAuthTokenProviderURL, "oAuthTokenProviderUrl", os.Getenv("PIPER_oAuthTokenProviderUrl"), "Specifies the oAuth Provider protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies integration artifact download location.")
cmd.MarkFlagRequired("username")
cmd.MarkFlagRequired("password")
cmd.MarkFlagRequired("integrationFlowId")
cmd.MarkFlagRequired("integrationFlowVersion")
cmd.MarkFlagRequired("host")
cmd.MarkFlagRequired("oAuthTokenProviderUrl")
cmd.MarkFlagRequired("downloadPath")
}
// retrieve step metadata
func integrationArtifactDownloadMetadata() config.StepData {
var theMetaData = config.StepData{
Metadata: config.StepMetadata{
Name: "integrationArtifactDownload",
Aliases: []config.Alias{},
Description: "Download integration flow runtime artefact",
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{
Name: "username",
ResourceRef: []config.ResourceReference{
{
Name: "cpiCredentialsId",
Param: "username",
Type: "secret",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "password",
ResourceRef: []config.ResourceReference{
{
Name: "cpiCredentialsId",
Param: "password",
Type: "secret",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "integrationFlowId",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "integrationFlowVersion",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "host",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "oAuthTokenProviderUrl",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "downloadPath",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
},
},
},
}
return theMetaData
}

View File

@@ -0,0 +1,17 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntegrationArtifactDownloadCommand(t *testing.T) {
t.Parallel()
testCmd := IntegrationArtifactDownloadCommand()
// only high level testing performed - details are tested in step generation procedure
assert.Equal(t, "integrationArtifactDownload", testCmd.Use, "command name incorrect")
}

View File

@@ -0,0 +1,77 @@
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type integrationArtifactDownloadMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newIntegrationArtifactDownloadTestsUtils() integrationArtifactDownloadMockUtils {
utils := integrationArtifactDownloadMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunIntegrationArtifactDownload(t *testing.T) {
t.Parallel()
t.Run("Successfull Download of Integration flow Artifact", func(t *testing.T) {
tempDir, tmpErr := ioutil.TempDir("", "")
defer os.RemoveAll(tempDir) // clean up
assert.NoError(t, tmpErr, "Error when creating temp dir")
config := integrationArtifactDownloadOptions{
Host: "https://demo",
OAuthTokenProviderURL: "https://demo/oauth/token",
Username: "demouser",
Password: "******",
IntegrationFlowID: "flow1",
IntegrationFlowVersion: "1.0.1",
DownloadPath: tempDir,
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "PositiveAndGetetIntegrationArtifactDownloadResBody"}
err := runIntegrationArtifactDownload(&config, nil, &httpClient)
absolutePath := filepath.Join(tempDir, "flow1.zip")
assert.DirExists(t, tempDir)
if assert.NoError(t, err) {
assert.Equal(t, fileExists(absolutePath), true)
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow1',Version='1.0.1')/$value", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
}
})
t.Run("Failed case of Integration Flow artifact Download", func(t *testing.T) {
config := integrationArtifactDownloadOptions{
Host: "https://demo",
OAuthTokenProviderURL: "https://demo/oauth/token",
Username: "demouser",
Password: "******",
IntegrationFlowID: "flow1",
IntegrationFlowVersion: "1.0.1",
DownloadPath: "tmp",
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "Negative"}
err := runIntegrationArtifactDownload(&config, nil, &httpClient)
assert.EqualError(t, err, "HTTP GET request to https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow1',Version='1.0.1')/$value failed with error: Unable to download integration artifact, Response Status code:400")
})
}

View File

@@ -45,6 +45,7 @@ func GetAllStepMetadata() map[string]config.StepData {
"gitopsUpdateDeployment": gitopsUpdateDeploymentMetadata(),
"hadolintExecute": hadolintExecuteMetadata(),
"integrationArtifactDeploy": integrationArtifactDeployMetadata(),
"integrationArtifactDownload": integrationArtifactDownloadMetadata(),
"integrationArtifactGetMplStatus": integrationArtifactGetMplStatusMetadata(),
"integrationArtifactGetServiceEndpoint": integrationArtifactGetServiceEndpointMetadata(),
"integrationArtifactUpdateConfiguration": integrationArtifactUpdateConfigurationMetadata(),

View File

@@ -125,6 +125,7 @@ func Execute() {
rootCmd.AddCommand(IntegrationArtifactDeployCommand())
rootCmd.AddCommand(IntegrationArtifactUpdateConfigurationCommand())
rootCmd.AddCommand(IntegrationArtifactGetMplStatusCommand())
rootCmd.AddCommand(IntegrationArtifactDownloadCommand())
rootCmd.AddCommand(AbapEnvironmentAssembleConfirmCommand())
addRootFlags(rootCmd)

View File

@@ -0,0 +1,33 @@
# ${docGenStepName}
## ${docGenDescription}
## Prerequisites
## ${docGenParameters}
## ${docGenConfiguration}
## ${docJenkinsPluginDependencies}
## Example
Example configuration for the use in a `Jenkinsfile`.
```groovy
integrationArtifactDownload script: this
```
Example for the use in a YAML configuration file (such as `.pipeline/config.yaml`).
```yaml
steps:
<...>
integrationArtifactDownload:
cpiCredentialsId: 'MY_CPI_OAUTH_CREDENTIALSID_IN_JENKINS'
integrationFlowId: 'MY_INTEGRATION_FLOW_NAME'
integrationFlowVersion: 'MY_INTEGRATION_FLOW_VERSION'
host: https://CPI_HOST_ITSPACES_URL
oAuthTokenProviderUrl: https://CPI_HOST_OAUTH_URL
downloadPath: MY_INTEGRATION_FLOW_DOWNLOAD_PATH
```

View File

@@ -102,6 +102,7 @@ nav:
- healthExecuteCheck: steps/healthExecuteCheck.md
- influxWriteData: steps/influxWriteData.md
- integrationArtifactDeploy: steps/integrationArtifactDeploy.md
- integrationArtifactDownload: steps/integrationArtifactDownload.md
- integrationArtifactGetMplStatus: steps/integrationArtifactGetMplStatus.md
- integrationArtifactGetServiceEndpoint: steps/integrationArtifactGetServiceEndpoint.md
- integrationArtifactUpdateConfiguration: steps/integrationArtifactUpdateConfiguration.md

View File

@@ -50,6 +50,8 @@ func GetCPIFunctionMockResponse(functionName, testType string) (*http.Response,
return GetIntegrationArtifactGetMplStatusCommandMockResponse(testType)
case "IntegrationArtifactGetServiceEndpoint":
return GetIntegrationArtifactGetServiceEndpointCommandMockResponse(testType)
case "IntegrationArtifactDownload":
return IntegrationArtifactDownloadCommandMockResponse(testType)
default:
res := http.Response{
StatusCode: 404,
@@ -186,3 +188,48 @@ func GetIntegrationArtifactGetServiceEndpointPositiveCaseRespBody() (*http.Respo
}
return &resp, nil
}
//IntegrationArtifactDownloadCommandMockResponse -Provide http respose body
func IntegrationArtifactDownloadCommandMockResponse(testType string) (*http.Response, error) {
response, error := GetPositiveCaseResponseByTestType(testType)
if response == nil && error == nil {
res := http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{
"code": "Bad Request",
"message": {
"@lang": "en",
"#text": "invalid request"
}
}`))),
}
return &res, errors.New("Unable to download integration artifact, Response Status code:400")
}
return response, error
}
//IntegrationArtifactDownloadCommandMockResponsePositiveCaseRespBody -Provide http respose body for positive case
func IntegrationArtifactDownloadCommandMockResponsePositiveCaseRespBody() (*http.Response, error) {
header := make(http.Header)
headerValue := "attachment; filename=flow1.zip"
header.Add("Content-Disposition", headerValue)
resp := http.Response{
StatusCode: 200,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`UEsDBBQACAgIADQ2clAAAAAAAAAAAAAAAAAUAAQATU`))),
}
return &resp, nil
}
//GetPositiveCaseResponseByTestType - get postive response by test case type
func GetPositiveCaseResponseByTestType(testType string) (*http.Response, error) {
switch testType {
case "PositiveAndGetetIntegrationArtifactDownloadResBody":
return IntegrationArtifactDownloadCommandMockResponsePositiveCaseRespBody()
default:
return nil, nil
}
}

View File

@@ -0,0 +1,79 @@
metadata:
name: integrationArtifactDownload
description: Download integration flow runtime artefact
longDescription: |
With this step you can download a integration flow runtime artifact, which returns a zip file with the integration flow contents in to current workspace using the OData API. Learn more about the SAP Cloud Integration remote API for downloading an integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/460dee19f9c141e88ba0454ce892247a.html).
spec:
inputs:
secrets:
- name: cpiCredentialsId
description: Jenkins credentials ID containing username and password for authentication to the SAP Cloud Platform Integration API's
type: jenkins
params:
- name: username
type: string
description: User to authenticate to the SAP Cloud Platform Integration Service
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
secret: true
resourceRef:
- name: cpiCredentialsId
type: secret
param: username
- name: password
type: string
description: Password to authenticate to the SAP Cloud Platform Integration Service
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
secret: true
resourceRef:
- name: cpiCredentialsId
type: secret
param: password
- name: integrationFlowId
type: string
description: Specifies the ID of the Integration Flow artifact
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
- name: integrationFlowVersion
type: string
description: Specifies the version of the Integration Flow artifact
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
- name: host
type: string
description: Specifies the protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
- name: oAuthTokenProviderUrl
type: string
description: Specifies the oAuth Provider protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true
- name: downloadPath
type: string
description: Specifies integration artifact download location.
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: true

View File

@@ -177,6 +177,7 @@ public class CommonStepsTest extends BasePiperTest{
'integrationArtifactUpdateConfiguration', //implementing new golang pattern without fields
'integrationArtifactGetMplStatus', //implementing new golang pattern without fields
'integrationArtifactGetServiceEndpoint', //implementing new golang pattern without fields
'integrationArtifactDownload', //implementing new golang pattern without fields
]
@Test

View File

@@ -0,0 +1,11 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/integrationArtifactDownload.yaml'
void call(Map parameters = [:]) {
List credentials = [
[type: 'usernamePassword', id: 'cpiCredentialsId', env: ['PIPER_username', 'PIPER_password']]
]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
}