github.com/xgoffin/jenkins-library@v1.154.0/cmd/integrationArtifactUpload_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type integrationArtifactUploadOptions struct {
    19  	APIServiceKey       string `json:"apiServiceKey,omitempty"`
    20  	IntegrationFlowID   string `json:"integrationFlowId,omitempty"`
    21  	IntegrationFlowName string `json:"integrationFlowName,omitempty"`
    22  	PackageID           string `json:"packageId,omitempty"`
    23  	FilePath            string `json:"filePath,omitempty"`
    24  }
    25  
    26  // IntegrationArtifactUploadCommand Upload or Update an integration flow designtime artifact
    27  func IntegrationArtifactUploadCommand() *cobra.Command {
    28  	const STEP_NAME = "integrationArtifactUpload"
    29  
    30  	metadata := integrationArtifactUploadMetadata()
    31  	var stepConfig integrationArtifactUploadOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createIntegrationArtifactUploadCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Upload or Update an integration flow designtime artifact",
    40  		Long:  `With this step you can either upload or update a integration flow designtime artifact using the OData API. Learn more about the SAP Cloud Integration remote API for updating an integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html).`,
    41  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    42  			startTime = time.Now()
    43  			log.SetStepName(STEP_NAME)
    44  			log.SetVerbose(GeneralConfig.Verbose)
    45  
    46  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    47  
    48  			path, _ := os.Getwd()
    49  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    50  			log.RegisterHook(fatalHook)
    51  
    52  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    53  			if err != nil {
    54  				log.SetErrorCategory(log.ErrorConfiguration)
    55  				return err
    56  			}
    57  			log.RegisterSecret(stepConfig.APIServiceKey)
    58  
    59  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    60  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    61  				log.RegisterHook(&sentryHook)
    62  			}
    63  
    64  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    65  				splunkClient = &splunk.Splunk{}
    66  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    67  				log.RegisterHook(logCollector)
    68  			}
    69  
    70  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    71  			if err != nil {
    72  				return err
    73  			}
    74  			if err = validation.ValidateStruct(stepConfig); err != nil {
    75  				log.SetErrorCategory(log.ErrorConfiguration)
    76  				return err
    77  			}
    78  
    79  			return nil
    80  		},
    81  		Run: func(_ *cobra.Command, _ []string) {
    82  			stepTelemetryData := telemetry.CustomData{}
    83  			stepTelemetryData.ErrorCode = "1"
    84  			handler := func() {
    85  				config.RemoveVaultSecretFiles()
    86  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    87  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    88  				stepTelemetryData.PiperCommitHash = GitCommit
    89  				telemetryClient.SetData(&stepTelemetryData)
    90  				telemetryClient.Send()
    91  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    92  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    93  				}
    94  			}
    95  			log.DeferExitHandler(handler)
    96  			defer handler()
    97  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    98  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    99  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   100  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   101  					GeneralConfig.HookConfig.SplunkConfig.Token,
   102  					GeneralConfig.HookConfig.SplunkConfig.Index,
   103  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   104  			}
   105  			integrationArtifactUpload(stepConfig, &stepTelemetryData)
   106  			stepTelemetryData.ErrorCode = "0"
   107  			log.Entry().Info("SUCCESS")
   108  		},
   109  	}
   110  
   111  	addIntegrationArtifactUploadFlags(createIntegrationArtifactUploadCmd, &stepConfig)
   112  	return createIntegrationArtifactUploadCmd
   113  }
   114  
   115  func addIntegrationArtifactUploadFlags(cmd *cobra.Command, stepConfig *integrationArtifactUploadOptions) {
   116  	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'")
   117  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   118  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowName, "integrationFlowName", os.Getenv("PIPER_integrationFlowName"), "Specifies the Name of the Integration Flow artifact")
   119  	cmd.Flags().StringVar(&stepConfig.PackageID, "packageId", os.Getenv("PIPER_packageId"), "Specifies the ID of the Integration Package")
   120  	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Specifies integration artifact relative file path.")
   121  
   122  	cmd.MarkFlagRequired("apiServiceKey")
   123  	cmd.MarkFlagRequired("integrationFlowId")
   124  	cmd.MarkFlagRequired("integrationFlowName")
   125  	cmd.MarkFlagRequired("filePath")
   126  }
   127  
   128  // retrieve step metadata
   129  func integrationArtifactUploadMetadata() config.StepData {
   130  	var theMetaData = config.StepData{
   131  		Metadata: config.StepMetadata{
   132  			Name:        "integrationArtifactUpload",
   133  			Aliases:     []config.Alias{},
   134  			Description: "Upload or Update an integration flow designtime artifact",
   135  		},
   136  		Spec: config.StepSpec{
   137  			Inputs: config.StepInputs{
   138  				Secrets: []config.StepSecrets{
   139  					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"},
   140  				},
   141  				Parameters: []config.StepParameters{
   142  					{
   143  						Name: "apiServiceKey",
   144  						ResourceRef: []config.ResourceReference{
   145  							{
   146  								Name:  "cpiApiServiceKeyCredentialsId",
   147  								Param: "apiServiceKey",
   148  								Type:  "secret",
   149  							},
   150  						},
   151  						Scope:     []string{"PARAMETERS"},
   152  						Type:      "string",
   153  						Mandatory: true,
   154  						Aliases:   []config.Alias{},
   155  						Default:   os.Getenv("PIPER_apiServiceKey"),
   156  					},
   157  					{
   158  						Name:        "integrationFlowId",
   159  						ResourceRef: []config.ResourceReference{},
   160  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   161  						Type:        "string",
   162  						Mandatory:   true,
   163  						Aliases:     []config.Alias{},
   164  						Default:     os.Getenv("PIPER_integrationFlowId"),
   165  					},
   166  					{
   167  						Name:        "integrationFlowName",
   168  						ResourceRef: []config.ResourceReference{},
   169  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   170  						Type:        "string",
   171  						Mandatory:   true,
   172  						Aliases:     []config.Alias{},
   173  						Default:     os.Getenv("PIPER_integrationFlowName"),
   174  					},
   175  					{
   176  						Name:        "packageId",
   177  						ResourceRef: []config.ResourceReference{},
   178  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   179  						Type:        "string",
   180  						Mandatory:   false,
   181  						Aliases:     []config.Alias{},
   182  						Default:     os.Getenv("PIPER_packageId"),
   183  					},
   184  					{
   185  						Name:        "filePath",
   186  						ResourceRef: []config.ResourceReference{},
   187  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   188  						Type:        "string",
   189  						Mandatory:   true,
   190  						Aliases:     []config.Alias{},
   191  						Default:     os.Getenv("PIPER_filePath"),
   192  					},
   193  				},
   194  			},
   195  		},
   196  	}
   197  	return theMetaData
   198  }