github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/integrationArtifactDownload_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 integrationArtifactDownloadOptions struct { 19 APIServiceKey string `json:"apiServiceKey,omitempty"` 20 IntegrationFlowID string `json:"integrationFlowId,omitempty"` 21 IntegrationFlowVersion string `json:"integrationFlowVersion,omitempty"` 22 DownloadPath string `json:"downloadPath,omitempty"` 23 } 24 25 // IntegrationArtifactDownloadCommand Download integration flow runtime artefact 26 func IntegrationArtifactDownloadCommand() *cobra.Command { 27 const STEP_NAME = "integrationArtifactDownload" 28 29 metadata := integrationArtifactDownloadMetadata() 30 var stepConfig integrationArtifactDownloadOptions 31 var startTime time.Time 32 var logCollector *log.CollectorHook 33 var splunkClient *splunk.Splunk 34 telemetryClient := &telemetry.Telemetry{} 35 36 var createIntegrationArtifactDownloadCmd = &cobra.Command{ 37 Use: STEP_NAME, 38 Short: "Download integration flow runtime artefact", 39 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/d1679a80543f46509a7329243b595bdb.html).`, 40 PreRunE: func(cmd *cobra.Command, _ []string) error { 41 startTime = time.Now() 42 log.SetStepName(STEP_NAME) 43 log.SetVerbose(GeneralConfig.Verbose) 44 45 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 46 47 path, _ := os.Getwd() 48 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 49 log.RegisterHook(fatalHook) 50 51 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 52 if err != nil { 53 log.SetErrorCategory(log.ErrorConfiguration) 54 return err 55 } 56 log.RegisterSecret(stepConfig.APIServiceKey) 57 58 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 59 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 60 log.RegisterHook(&sentryHook) 61 } 62 63 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 64 splunkClient = &splunk.Splunk{} 65 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 66 log.RegisterHook(logCollector) 67 } 68 69 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 70 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 71 } 72 73 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 74 if err != nil { 75 return err 76 } 77 if err = validation.ValidateStruct(stepConfig); err != nil { 78 log.SetErrorCategory(log.ErrorConfiguration) 79 return err 80 } 81 82 return nil 83 }, 84 Run: func(_ *cobra.Command, _ []string) { 85 stepTelemetryData := telemetry.CustomData{} 86 stepTelemetryData.ErrorCode = "1" 87 handler := func() { 88 config.RemoveVaultSecretFiles() 89 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 90 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 91 stepTelemetryData.PiperCommitHash = GitCommit 92 telemetryClient.SetData(&stepTelemetryData) 93 telemetryClient.Send() 94 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 95 splunkClient.Initialize(GeneralConfig.CorrelationID, 96 GeneralConfig.HookConfig.SplunkConfig.Dsn, 97 GeneralConfig.HookConfig.SplunkConfig.Token, 98 GeneralConfig.HookConfig.SplunkConfig.Index, 99 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 100 splunkClient.Send(telemetryClient.GetData(), logCollector) 101 } 102 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 103 splunkClient.Initialize(GeneralConfig.CorrelationID, 104 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 105 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 106 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 107 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 108 splunkClient.Send(telemetryClient.GetData(), logCollector) 109 } 110 } 111 log.DeferExitHandler(handler) 112 defer handler() 113 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 114 integrationArtifactDownload(stepConfig, &stepTelemetryData) 115 stepTelemetryData.ErrorCode = "0" 116 log.Entry().Info("SUCCESS") 117 }, 118 } 119 120 addIntegrationArtifactDownloadFlags(createIntegrationArtifactDownloadCmd, &stepConfig) 121 return createIntegrationArtifactDownloadCmd 122 } 123 124 func addIntegrationArtifactDownloadFlags(cmd *cobra.Command, stepConfig *integrationArtifactDownloadOptions) { 125 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'") 126 cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact") 127 cmd.Flags().StringVar(&stepConfig.IntegrationFlowVersion, "integrationFlowVersion", os.Getenv("PIPER_integrationFlowVersion"), "Specifies the version of the Integration Flow artifact") 128 cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies integration artifact download location.") 129 130 cmd.MarkFlagRequired("apiServiceKey") 131 cmd.MarkFlagRequired("integrationFlowId") 132 cmd.MarkFlagRequired("integrationFlowVersion") 133 cmd.MarkFlagRequired("downloadPath") 134 } 135 136 // retrieve step metadata 137 func integrationArtifactDownloadMetadata() config.StepData { 138 var theMetaData = config.StepData{ 139 Metadata: config.StepMetadata{ 140 Name: "integrationArtifactDownload", 141 Aliases: []config.Alias{}, 142 Description: "Download integration flow runtime artefact", 143 }, 144 Spec: config.StepSpec{ 145 Inputs: config.StepInputs{ 146 Secrets: []config.StepSecrets{ 147 {Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"}, 148 }, 149 Parameters: []config.StepParameters{ 150 { 151 Name: "apiServiceKey", 152 ResourceRef: []config.ResourceReference{ 153 { 154 Name: "cpiApiServiceKeyCredentialsId", 155 Param: "apiServiceKey", 156 Type: "secret", 157 }, 158 }, 159 Scope: []string{"PARAMETERS"}, 160 Type: "string", 161 Mandatory: true, 162 Aliases: []config.Alias{}, 163 Default: os.Getenv("PIPER_apiServiceKey"), 164 }, 165 { 166 Name: "integrationFlowId", 167 ResourceRef: []config.ResourceReference{}, 168 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 169 Type: "string", 170 Mandatory: true, 171 Aliases: []config.Alias{}, 172 Default: os.Getenv("PIPER_integrationFlowId"), 173 }, 174 { 175 Name: "integrationFlowVersion", 176 ResourceRef: []config.ResourceReference{}, 177 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 178 Type: "string", 179 Mandatory: true, 180 Aliases: []config.Alias{}, 181 Default: os.Getenv("PIPER_integrationFlowVersion"), 182 }, 183 { 184 Name: "downloadPath", 185 ResourceRef: []config.ResourceReference{}, 186 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 187 Type: "string", 188 Mandatory: true, 189 Aliases: []config.Alias{}, 190 Default: os.Getenv("PIPER_downloadPath"), 191 }, 192 }, 193 }, 194 }, 195 } 196 return theMetaData 197 }