You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	IntegrationArtifactResource Command (#3027)
* IntegrationArtifactResource Command * Remove unused code * Formatting fix * formatting fix * formatting fix * formatting fix * formatting fix * CodeReview Fixes * Code Review Fix * Code Review Fixes * Code Review Fixes * Format fix * format fix * format fix * format fix * Code Review Fix Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							97b84429f1
						
					
				
				
					commit
					2d412d9f3c
				
			
							
								
								
									
										232
									
								
								cmd/integrationArtifactResource.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										232
									
								
								cmd/integrationArtifactResource.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,232 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	b64 "encoding/base64" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/Jeffail/gabs/v2" | ||||
| 	"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/piperutils" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/pkg/errors" | ||||
| ) | ||||
|  | ||||
| type integrationArtifactResourceData struct { | ||||
| 	Method     string | ||||
| 	URL        string | ||||
| 	IFlowID    string | ||||
| 	ScsMessage string | ||||
| 	FlrMessage string | ||||
| 	StatusCode int | ||||
| } | ||||
|  | ||||
| func integrationArtifactResource(config integrationArtifactResourceOptions, 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{} | ||||
| 	fileUtils := &piperutils.Files{} | ||||
|  | ||||
| 	// 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 := runIntegrationArtifactResource(&config, telemetryData, fileUtils, httpClient) | ||||
| 	if err != nil { | ||||
| 		log.Entry().WithError(err).Fatal("step execution failed") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func runIntegrationArtifactResource(config *integrationArtifactResourceOptions, telemetryData *telemetry.CustomData, fileUtils piperutils.FileUtils, httpClient piperhttp.Sender) error { | ||||
| 	serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	clientOptions := piperhttp.ClientOptions{} | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("Accept", "application/json") | ||||
| 	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, 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) | ||||
| 	mode := strings.ToLower(strings.TrimSpace(config.Operation)) | ||||
| 	switch mode { | ||||
| 	case "create": | ||||
| 		return UploadIntegrationArtifactResource(config, httpClient, fileUtils, serviceKey.OAuth.Host) | ||||
| 	case "update": | ||||
| 		return UpdateIntegrationArtifactResource(config, httpClient, fileUtils, serviceKey.OAuth.Host) | ||||
| 	case "delete": | ||||
| 		return DeleteIntegrationArtifactResource(config, httpClient, fileUtils, serviceKey.OAuth.Host) | ||||
| 	default: | ||||
| 		return errors.New("invalid input for resource operation") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| //UploadIntegrationArtifactResource - Upload new resource file to existing integration flow design time artefact | ||||
| func UploadIntegrationArtifactResource(config *integrationArtifactResourceOptions, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils, apiHost string) error { | ||||
| 	httpMethod := "POST" | ||||
| 	uploadIflowStatusURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/Resources", apiHost, config.IntegrationFlowID, "Active") | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("content-type", "application/json") | ||||
| 	payload, jsonError := GetJSONPayload(config, "create", fileUtils) | ||||
| 	if jsonError != nil { | ||||
| 		return errors.Wrapf(jsonError, "Failed to get json payload for file %v, failed with error", config.ResourcePath) | ||||
| 	} | ||||
|  | ||||
| 	uploadIflowStatusResp, httpErr := httpClient.SendRequest(httpMethod, uploadIflowStatusURL, payload, header, nil) | ||||
|  | ||||
| 	successMessage := "Successfully create a new resource file in the integration flow artefact" | ||||
| 	failureMessage := "Failed to create a new resource file in the integration flow artefact" | ||||
| 	integrationArtifactResourceData := integrationArtifactResourceData{ | ||||
| 		Method:     httpMethod, | ||||
| 		URL:        uploadIflowStatusURL, | ||||
| 		IFlowID:    config.IntegrationFlowID, | ||||
| 		ScsMessage: successMessage, | ||||
| 		FlrMessage: failureMessage, | ||||
| 		StatusCode: http.StatusCreated, | ||||
| 	} | ||||
|  | ||||
| 	return HttpResponseHandler(uploadIflowStatusResp, httpErr, &integrationArtifactResourceData) | ||||
| } | ||||
|  | ||||
| //UpdateIntegrationArtifactResource - Update integration artifact resource file | ||||
| func UpdateIntegrationArtifactResource(config *integrationArtifactResourceOptions, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils, apiHost string) error { | ||||
| 	httpMethod := "PUT" | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("content-type", "application/json") | ||||
| 	fileName := filepath.Base(config.ResourcePath) | ||||
| 	fileExt := GetResourceFileExtension(fileName) | ||||
| 	if fileExt == "" { | ||||
| 		return errors.New("invalid file extension in resource file") | ||||
| 	} | ||||
| 	updateIflowStatusURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$links/Resources(Name='%s',ResourceType='%s')", apiHost, config.IntegrationFlowID, "Active", fileName, fileExt) | ||||
| 	payload, jsonError := GetJSONPayload(config, "update", fileUtils) | ||||
| 	if jsonError != nil { | ||||
| 		return errors.Wrapf(jsonError, "Failed to get json payload for file %v, failed with error", config.ResourcePath) | ||||
| 	} | ||||
| 	updateIflowStatusResp, httpErr := httpClient.SendRequest(httpMethod, updateIflowStatusURL, payload, header, nil) | ||||
|  | ||||
| 	successMessage := "Successfully updated resource file of the integration flow artefact" | ||||
| 	failureMessage := "Failed to update rsource file of the integration flow artefact" | ||||
| 	integrationArtifactResourceData := integrationArtifactResourceData{ | ||||
| 		Method:     httpMethod, | ||||
| 		URL:        updateIflowStatusURL, | ||||
| 		IFlowID:    config.IntegrationFlowID, | ||||
| 		ScsMessage: successMessage, | ||||
| 		FlrMessage: failureMessage, | ||||
| 		StatusCode: http.StatusOK, | ||||
| 	} | ||||
|  | ||||
| 	return HttpResponseHandler(updateIflowStatusResp, httpErr, &integrationArtifactResourceData) | ||||
| } | ||||
|  | ||||
| //DeleteIntegrationArtifactResource - Delete integration artifact resource file | ||||
| func DeleteIntegrationArtifactResource(config *integrationArtifactResourceOptions, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils, apiHost string) error { | ||||
| 	httpMethod := "DELETE" | ||||
| 	header := make(http.Header) | ||||
| 	header.Add("content-type", "application/json") | ||||
| 	fileName := filepath.Base(config.ResourcePath) | ||||
| 	fileExt := GetResourceFileExtension(fileName) | ||||
| 	if fileExt == "" { | ||||
| 		return errors.New("invalid file extension in resource file") | ||||
| 	} | ||||
| 	deleteIflowResourceStatusURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$links/Resources(Name='%s',ResourceType='%s')", apiHost, config.IntegrationFlowID, "Active", fileName, fileExt) | ||||
| 	deleteIflowResourceStatusResp, httpErr := httpClient.SendRequest(httpMethod, deleteIflowResourceStatusURL, nil, header, nil) | ||||
|  | ||||
| 	successMessage := "Successfully deleted a resource file in the integration flow artefact" | ||||
| 	failureMessage := "Failed to delete a resource file in the integration flow artefact" | ||||
| 	integrationArtifactResourceData := integrationArtifactResourceData{ | ||||
| 		Method:     httpMethod, | ||||
| 		URL:        deleteIflowResourceStatusURL, | ||||
| 		IFlowID:    config.IntegrationFlowID, | ||||
| 		ScsMessage: successMessage, | ||||
| 		FlrMessage: failureMessage, | ||||
| 		StatusCode: http.StatusOK, | ||||
| 	} | ||||
|  | ||||
| 	return HttpResponseHandler(deleteIflowResourceStatusResp, httpErr, &integrationArtifactResourceData) | ||||
| } | ||||
|  | ||||
| //GetJSONPayload -return http payload as byte array | ||||
| func GetJSONPayload(config *integrationArtifactResourceOptions, mode string, fileUtils piperutils.FileUtils) (*bytes.Buffer, error) { | ||||
| 	fileContent, readError := fileUtils.FileRead(config.ResourcePath) | ||||
| 	if readError != nil { | ||||
| 		return nil, errors.Wrapf(readError, "Error reading file") | ||||
| 	} | ||||
| 	fileName := filepath.Base(config.ResourcePath) | ||||
| 	jsonObj := gabs.New() | ||||
| 	if mode == "create" { | ||||
| 		jsonObj.Set(fileName, "Name") | ||||
| 		jsonObj.Set(GetResourceFileExtension(fileName), "ResourceType") | ||||
| 		jsonObj.Set(b64.StdEncoding.EncodeToString(fileContent), "ResourceContent") | ||||
| 	} else if mode == "update" { | ||||
| 		jsonObj.Set(b64.StdEncoding.EncodeToString(fileContent), "ResourceContent") | ||||
| 	} else { | ||||
| 		return nil, fmt.Errorf("Unkown node: '%s'", mode) | ||||
| 	} | ||||
|  | ||||
| 	jsonBody, jsonErr := json.Marshal(jsonObj) | ||||
|  | ||||
| 	if jsonErr != nil { | ||||
| 		return nil, errors.Wrapf(jsonErr, "json payload is invalid for integration flow artifact %q", config.IntegrationFlowID) | ||||
| 	} | ||||
| 	return bytes.NewBuffer(jsonBody), nil | ||||
| } | ||||
|  | ||||
| //GetResourceFileExtension -return resource file extension | ||||
| func GetResourceFileExtension(filename string) string { | ||||
| 	fileExtension := filepath.Ext(filename) | ||||
| 	switch fileExtension { | ||||
| 	case ".xsl": | ||||
| 		return "xslt" | ||||
| 	case ".gsh", ".groovy": | ||||
| 		return "groovy" | ||||
| 	case ".js": | ||||
| 		return "js" | ||||
| 	case ".jar": | ||||
| 		return "jar" | ||||
| 	default: | ||||
| 		return "" | ||||
| 	} | ||||
| } | ||||
|  | ||||
| //HttpResponseHandler - handle http response object | ||||
| func HttpResponseHandler(resp *http.Response, httpErr error, integrationArtifactResourceData *integrationArtifactResourceData) error { | ||||
|  | ||||
| 	if resp != nil && resp.Body != nil { | ||||
| 		defer resp.Body.Close() | ||||
| 	} | ||||
|  | ||||
| 	if resp == nil { | ||||
| 		return errors.Errorf("did not retrieve a HTTP response: %v", httpErr) | ||||
| 	} | ||||
|  | ||||
| 	if resp.StatusCode == integrationArtifactResourceData.StatusCode { | ||||
| 		log.Entry(). | ||||
| 			WithField("IntegrationFlowID", integrationArtifactResourceData.IFlowID). | ||||
| 			Info(integrationArtifactResourceData.ScsMessage) | ||||
| 		return nil | ||||
| 	} | ||||
| 	if httpErr != nil { | ||||
| 		responseBody, readErr := ioutil.ReadAll(resp.Body) | ||||
| 		if readErr != nil { | ||||
| 			return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", resp.StatusCode) | ||||
| 		} | ||||
| 		log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", responseBody, resp.StatusCode) | ||||
| 		return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error: %v", integrationArtifactResourceData.Method, integrationArtifactResourceData.URL, string(responseBody)) | ||||
| 	} | ||||
| 	return errors.Errorf("%s, Response Status code: %v", integrationArtifactResourceData.FlrMessage, resp.StatusCode) | ||||
| } | ||||
							
								
								
									
										172
									
								
								cmd/integrationArtifactResource_generated.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										172
									
								
								cmd/integrationArtifactResource_generated.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,172 @@ | ||||
| // 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/splunk" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type integrationArtifactResourceOptions struct { | ||||
| 	APIServiceKey     string `json:"apiServiceKey,omitempty"` | ||||
| 	IntegrationFlowID string `json:"integrationFlowId,omitempty"` | ||||
| 	Operation         string `json:"operation,omitempty"` | ||||
| 	ResourcePath      string `json:"resourcePath,omitempty"` | ||||
| } | ||||
|  | ||||
| // IntegrationArtifactResourceCommand Add, Delete or Update an resource file of integration flow designtime artifact | ||||
| func IntegrationArtifactResourceCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "integrationArtifactResource" | ||||
|  | ||||
| 	metadata := integrationArtifactResourceMetadata() | ||||
| 	var stepConfig integrationArtifactResourceOptions | ||||
| 	var startTime time.Time | ||||
| 	var logCollector *log.CollectorHook | ||||
|  | ||||
| 	var createIntegrationArtifactResourceCmd = &cobra.Command{ | ||||
| 		Use:   STEP_NAME, | ||||
| 		Short: "Add, Delete or Update an resource file of integration flow designtime artifact", | ||||
| 		Long:  `With this step you can either add, delete or update a resource of integration flow designtime artifact using the OData API. Learn more about the SAP Cloud Integration remote API for managing an resource of integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html).`, | ||||
| 		PreRunE: func(cmd *cobra.Command, _ []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) | ||||
|  | ||||
| 			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.APIServiceKey) | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} | ||||
| 				log.RegisterHook(logCollector) | ||||
| 			} | ||||
|  | ||||
| 			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) | ||||
| 				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 					splunk.Send(&telemetryData, logCollector) | ||||
| 				} | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				splunk.Initialize(GeneralConfig.CorrelationID, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Dsn, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Token, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Index, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.SendLogs) | ||||
| 			} | ||||
| 			integrationArtifactResource(stepConfig, &telemetryData) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addIntegrationArtifactResourceFlags(createIntegrationArtifactResourceCmd, &stepConfig) | ||||
| 	return createIntegrationArtifactResourceCmd | ||||
| } | ||||
|  | ||||
| func addIntegrationArtifactResourceFlags(cmd *cobra.Command, stepConfig *integrationArtifactResourceOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.APIServiceKey, "apiServiceKey", os.Getenv("PIPER_apiServiceKey"), "Service key JSON string to access the Process Integration Runtime service instance of plan 'api'") | ||||
| 	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Operation, "operation", os.Getenv("PIPER_operation"), "Specifies the operation(create/update/delete) for resource file of the Integration Flow artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.ResourcePath, "resourcePath", os.Getenv("PIPER_resourcePath"), "Specifies integration artifact resource file relative path.") | ||||
|  | ||||
| 	cmd.MarkFlagRequired("apiServiceKey") | ||||
| 	cmd.MarkFlagRequired("integrationFlowId") | ||||
| 	cmd.MarkFlagRequired("operation") | ||||
| 	cmd.MarkFlagRequired("resourcePath") | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func integrationArtifactResourceMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:        "integrationArtifactResource", | ||||
| 			Aliases:     []config.Alias{}, | ||||
| 			Description: "Add, Delete or Update an resource file of integration flow designtime artifact", | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Secrets: []config.StepSecrets{ | ||||
| 					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"}, | ||||
| 				}, | ||||
| 				Parameters: []config.StepParameters{ | ||||
| 					{ | ||||
| 						Name: "apiServiceKey", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "cpiApiServiceKeyCredentialsId", | ||||
| 								Param: "apiServiceKey", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 						Default:   os.Getenv("PIPER_apiServiceKey"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "integrationFlowId", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_integrationFlowId"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "operation", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_operation"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "resourcePath", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_resourcePath"), | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
							
								
								
									
										17
									
								
								cmd/integrationArtifactResource_generated_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								cmd/integrationArtifactResource_generated_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestIntegrationArtifactResourceCommand(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	testCmd := IntegrationArtifactResourceCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procedure | ||||
| 	assert.Equal(t, "integrationArtifactResource", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
							
								
								
									
										137
									
								
								cmd/integrationArtifactResource_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								cmd/integrationArtifactResource_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,137 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"path/filepath" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestRunIntegrationArtifactResource(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("Create Resource Test", func(t *testing.T) { | ||||
| 		filesMock := mock.FilesMock{} | ||||
| 		path := filepath.Join("tempDir", "demo.xsl") | ||||
| 		filesMock.AddFile(path, []byte("dummy content")) | ||||
| 		exists, err := filesMock.FileExists(path) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.True(t, exists) | ||||
|  | ||||
| 		apiServiceKey := `{ | ||||
| 			"oauth": { | ||||
| 				"url": "https://demo", | ||||
| 				"clientid": "demouser", | ||||
| 				"clientsecret": "******", | ||||
| 				"tokenurl": "https://demo/oauth/token" | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactResourceOptions{ | ||||
| 			APIServiceKey:     apiServiceKey, | ||||
| 			IntegrationFlowID: "flow1", | ||||
| 			Operation:         "create", | ||||
| 			ResourcePath:      path, | ||||
| 		} | ||||
| 		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceCreate", ResponseBody: ``, TestType: "Positive"} | ||||
|  | ||||
| 		// test | ||||
| 		err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient) | ||||
|  | ||||
| 		// assert | ||||
| 		assert.NoError(t, err) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Update Resource Test", func(t *testing.T) { | ||||
| 		filesMock := mock.FilesMock{} | ||||
| 		path := filepath.Join("tempDir", "demo.xsl") | ||||
| 		filesMock.AddFile(path, []byte("dummy content")) | ||||
| 		exists, err := filesMock.FileExists(path) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.True(t, exists) | ||||
|  | ||||
| 		apiServiceKey := `{ | ||||
| 			"oauth": { | ||||
| 				"url": "https://demo", | ||||
| 				"clientid": "demouser", | ||||
| 				"clientsecret": "******", | ||||
| 				"tokenurl": "https://demo/oauth/token" | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactResourceOptions{ | ||||
| 			APIServiceKey:     apiServiceKey, | ||||
| 			IntegrationFlowID: "flow1", | ||||
| 			Operation:         "update", | ||||
| 			ResourcePath:      path, | ||||
| 		} | ||||
| 		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceUpdate", ResponseBody: ``, TestType: "Positive"} | ||||
|  | ||||
| 		// test | ||||
| 		err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient) | ||||
|  | ||||
| 		// assert | ||||
| 		assert.NoError(t, err) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Delete Resource Test", func(t *testing.T) { | ||||
| 		filesMock := mock.FilesMock{} | ||||
| 		path := filepath.Join("tempDir", "demo.xsl") | ||||
| 		filesMock.AddFile(path, []byte("dummy content")) | ||||
| 		exists, err := filesMock.FileExists(path) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.True(t, exists) | ||||
|  | ||||
| 		apiServiceKey := `{ | ||||
| 			"oauth": { | ||||
| 				"url": "https://demo", | ||||
| 				"clientid": "demouser", | ||||
| 				"clientsecret": "******", | ||||
| 				"tokenurl": "https://demo/oauth/token" | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactResourceOptions{ | ||||
| 			APIServiceKey:     apiServiceKey, | ||||
| 			IntegrationFlowID: "flow1", | ||||
| 			Operation:         "delete", | ||||
| 			ResourcePath:      path, | ||||
| 		} | ||||
| 		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceDelete", ResponseBody: ``, TestType: "Positive"} | ||||
|  | ||||
| 		// test | ||||
| 		err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient) | ||||
|  | ||||
| 		// assert | ||||
| 		assert.NoError(t, err) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Create Resource Negative Test", func(t *testing.T) { | ||||
| 		filesMock := mock.FilesMock{} | ||||
| 		path := filepath.Join("tempDir", "demo.xsl") | ||||
| 		filesMock.AddFile(path, []byte("dummy content")) | ||||
| 		exists, err := filesMock.FileExists(path) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.True(t, exists) | ||||
|  | ||||
| 		apiServiceKey := `{ | ||||
| 			"oauth": { | ||||
| 				"url": "https://demo", | ||||
| 				"clientid": "demouser", | ||||
| 				"clientsecret": "******", | ||||
| 				"tokenurl": "https://demo/oauth/token" | ||||
| 			} | ||||
| 		}` | ||||
| 		config := integrationArtifactResourceOptions{ | ||||
| 			APIServiceKey:     apiServiceKey, | ||||
| 			IntegrationFlowID: "flow1", | ||||
| 			Operation:         "create", | ||||
| 			ResourcePath:      path, | ||||
| 		} | ||||
| 		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceCreate", ResponseBody: ``, TestType: "Negative"} | ||||
|  | ||||
| 		// test | ||||
| 		err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient) | ||||
|  | ||||
| 		// assert | ||||
| 		assert.Error(t, err) | ||||
| 	}) | ||||
| } | ||||
| @@ -52,6 +52,7 @@ func GetAllStepMetadata() map[string]config.StepData { | ||||
| 		"integrationArtifactDownload":               integrationArtifactDownloadMetadata(), | ||||
| 		"integrationArtifactGetMplStatus":           integrationArtifactGetMplStatusMetadata(), | ||||
| 		"integrationArtifactGetServiceEndpoint":     integrationArtifactGetServiceEndpointMetadata(), | ||||
| 		"integrationArtifactResource":               integrationArtifactResourceMetadata(), | ||||
| 		"integrationArtifactTriggerIntegrationTest": integrationArtifactTriggerIntegrationTestMetadata(), | ||||
| 		"integrationArtifactUnDeploy":               integrationArtifactUnDeployMetadata(), | ||||
| 		"integrationArtifactUpdateConfiguration":    integrationArtifactUpdateConfigurationMetadata(), | ||||
|   | ||||
| @@ -150,6 +150,7 @@ func Execute() { | ||||
| 	rootCmd.AddCommand(IntegrationArtifactUploadCommand()) | ||||
| 	rootCmd.AddCommand(IntegrationArtifactTriggerIntegrationTestCommand()) | ||||
| 	rootCmd.AddCommand(IntegrationArtifactUnDeployCommand()) | ||||
| 	rootCmd.AddCommand(IntegrationArtifactResourceCommand()) | ||||
| 	rootCmd.AddCommand(TerraformExecuteCommand()) | ||||
| 	rootCmd.AddCommand(ContainerExecuteStructureTestsCommand()) | ||||
| 	rootCmd.AddCommand(GaugeExecuteTestsCommand()) | ||||
|   | ||||
							
								
								
									
										31
									
								
								documentation/docs/steps/integrationArtifactResource.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								documentation/docs/steps/integrationArtifactResource.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| # ${docGenStepName} | ||||
|  | ||||
| ## ${docGenDescription} | ||||
|  | ||||
| ## Prerequisites | ||||
|  | ||||
| ## ${docGenParameters} | ||||
|  | ||||
| ## ${docGenConfiguration} | ||||
|  | ||||
| ## ${docJenkinsPluginDependencies} | ||||
|  | ||||
| ## Example | ||||
|  | ||||
| Example configuration for the use in a `Jenkinsfile`. | ||||
|  | ||||
| ```groovy | ||||
| integrationArtifactUpload script: this | ||||
| ``` | ||||
|  | ||||
| Example for the use in a YAML configuration file (such as `.pipeline/config.yaml`). | ||||
|  | ||||
| ```yaml | ||||
| steps: | ||||
|   <...> | ||||
|   integrationArtifactResource: | ||||
|     cpiApiServiceKeyCredentialsId: 'MY_API_SERVICE_KEY' | ||||
|     integrationFlowId: 'MY_INTEGRATION_FLOW_ID' | ||||
|     operation: 'Create_OR_Modify_Delete_INTEGRATION_FLOW_Artifact_Resource' | ||||
|     resourcePath: 'MY_INTEGRATION_FLOW_Artifact_Resource_Relative_Path' | ||||
| ``` | ||||
| @@ -107,6 +107,7 @@ nav: | ||||
|         - integrationArtifactDownload: steps/integrationArtifactDownload.md | ||||
|         - integrationArtifactGetMplStatus: steps/integrationArtifactGetMplStatus.md | ||||
|         - integrationArtifactGetServiceEndpoint: steps/integrationArtifactGetServiceEndpoint.md | ||||
|         - integrationArtifactResource: steps/integrationArtifactResource.md | ||||
|         - integrationArtifactUnDeploy: steps/integrationArtifactUnDeploy.md | ||||
|         - integrationArtifactUpdateConfiguration: steps/integrationArtifactUpdateConfiguration.md | ||||
|         - integrationArtifactUpload: steps/integrationArtifactUpload.md | ||||
|   | ||||
| @@ -52,6 +52,13 @@ func GetCPIFunctionMockResponse(functionName, testType string) (*http.Response, | ||||
| 		return TriggerIntegrationTestMockResponse(testType) | ||||
| 	case "IntegrationArtifactGetMplStatusError": | ||||
| 		return GetIntegrationArtifactDeployErrorStatusMockResponseBody() | ||||
| 	case "IntegrationArtifactResourceCreate": | ||||
| 		if testType == "Negative" { | ||||
| 			return GetRespBodyHTTPStatusServiceErrorResponse() | ||||
| 		} | ||||
| 		return GetRespBodyHTTPStatusCreated() | ||||
| 	case "IntegrationArtifactResourceUpdate", "IntegrationArtifactResourceDelete": | ||||
| 		return GetRespBodyHTTPStatusOK() | ||||
| 	default: | ||||
| 		res := http.Response{ | ||||
| 			StatusCode: 404, | ||||
|   | ||||
							
								
								
									
										53
									
								
								resources/metadata/integrationArtifactResource.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								resources/metadata/integrationArtifactResource.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| metadata: | ||||
|   name: integrationArtifactResource | ||||
|   description: Add, Delete or Update an resource file of integration flow designtime artifact | ||||
|   longDescription: | | ||||
|     With this step you can either add, delete or update a resource of integration flow designtime artifact using the OData API. Learn more about the SAP Cloud Integration remote API for managing an resource of integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html). | ||||
|  | ||||
| spec: | ||||
|   inputs: | ||||
|     secrets: | ||||
|       - name: cpiApiServiceKeyCredentialsId | ||||
|         description: Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api' | ||||
|         type: jenkins | ||||
|     params: | ||||
|       - name: apiServiceKey | ||||
|         type: string | ||||
|         description: Service key JSON string to access the Process Integration Runtime service instance of plan 'api' | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|         mandatory: true | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: cpiApiServiceKeyCredentialsId | ||||
|             type: secret | ||||
|             param: apiServiceKey | ||||
|       - name: integrationFlowId | ||||
|         type: string | ||||
|         description: Specifies the ID of the Integration Flow artifact | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - GENERAL | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|       - name: operation | ||||
|         type: string | ||||
|         description: Specifies the operation(create/update/delete) for resource file of the Integration Flow artifact | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|         possibleValues: | ||||
|           - create | ||||
|           - update | ||||
|           - delete | ||||
|       - name: resourcePath | ||||
|         type: string | ||||
|         description: Specifies integration artifact resource file relative path. | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
| @@ -186,6 +186,7 @@ public class CommonStepsTest extends BasePiperTest{ | ||||
|         'integrationArtifactUpload', //implementing new golang pattern without fields | ||||
|         'integrationArtifactTriggerIntegrationTest', //implementing new golang pattern without fields | ||||
|         'integrationArtifactUnDeploy', //implementing new golang pattern without fields | ||||
|         'integrationArtifactResource', //implementing new golang pattern without fields | ||||
|         'containerExecuteStructureTests', //implementing new golang pattern without fields | ||||
|         'transportRequestUploadSOLMAN', //implementing new golang pattern without fields | ||||
|         'transportRequestReqIDFromGit', //implementing new golang pattern without fields | ||||
|   | ||||
							
								
								
									
										11
									
								
								vars/integrationArtifactResource.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								vars/integrationArtifactResource.groovy
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import groovy.transform.Field | ||||
|  | ||||
| @Field String STEP_NAME = getClass().getName() | ||||
| @Field String METADATA_FILE = 'metadata/integrationArtifactResource.yaml' | ||||
|  | ||||
| void call(Map parameters = [:]) { | ||||
|     List credentials = [ | ||||
|         [type: 'token', id: 'cpiApiServiceKeyCredentialsId', env: ['PIPER_apiServiceKey']] | ||||
|     ] | ||||
|     piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user