github.com/xgoffin/jenkins-library@v1.154.0/cmd/apiProxyDownload_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 apiProxyDownloadOptions struct { 19 APIServiceKey string `json:"apiServiceKey,omitempty"` 20 APIProxyName string `json:"apiProxyName,omitempty"` 21 DownloadPath string `json:"downloadPath,omitempty"` 22 } 23 24 // ApiProxyDownloadCommand Download a specific API Proxy from the API Portal 25 func ApiProxyDownloadCommand() *cobra.Command { 26 const STEP_NAME = "apiProxyDownload" 27 28 metadata := apiProxyDownloadMetadata() 29 var stepConfig apiProxyDownloadOptions 30 var startTime time.Time 31 var logCollector *log.CollectorHook 32 var splunkClient *splunk.Splunk 33 telemetryClient := &telemetry.Telemetry{} 34 35 var createApiProxyDownloadCmd = &cobra.Command{ 36 Use: STEP_NAME, 37 Short: "Download a specific API Proxy from the API Portal", 38 Long: `With this step you can download a specific API Proxy from the API Portal, which returns a zip file with the api proxy contents in to current workspace using the OData API. Learn more about the SAP API Management API for downloading an api proxy artifact [here](https://help.sap.com/viewer/66d066d903c2473f81ec33acfe2ccdb4/Cloud/en-US/e26b3320cd534ae4bc743af8013a8abb.html).`, 39 PreRunE: func(cmd *cobra.Command, _ []string) error { 40 startTime = time.Now() 41 log.SetStepName(STEP_NAME) 42 log.SetVerbose(GeneralConfig.Verbose) 43 44 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 45 46 path, _ := os.Getwd() 47 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 48 log.RegisterHook(fatalHook) 49 50 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 51 if err != nil { 52 log.SetErrorCategory(log.ErrorConfiguration) 53 return err 54 } 55 log.RegisterSecret(stepConfig.APIServiceKey) 56 57 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 58 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 59 log.RegisterHook(&sentryHook) 60 } 61 62 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 63 splunkClient = &splunk.Splunk{} 64 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 65 log.RegisterHook(logCollector) 66 } 67 68 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 69 if err != nil { 70 return err 71 } 72 if err = validation.ValidateStruct(stepConfig); err != nil { 73 log.SetErrorCategory(log.ErrorConfiguration) 74 return err 75 } 76 77 return nil 78 }, 79 Run: func(_ *cobra.Command, _ []string) { 80 stepTelemetryData := telemetry.CustomData{} 81 stepTelemetryData.ErrorCode = "1" 82 handler := func() { 83 config.RemoveVaultSecretFiles() 84 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 85 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 86 stepTelemetryData.PiperCommitHash = GitCommit 87 telemetryClient.SetData(&stepTelemetryData) 88 telemetryClient.Send() 89 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 90 splunkClient.Send(telemetryClient.GetData(), logCollector) 91 } 92 } 93 log.DeferExitHandler(handler) 94 defer handler() 95 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 96 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 97 splunkClient.Initialize(GeneralConfig.CorrelationID, 98 GeneralConfig.HookConfig.SplunkConfig.Dsn, 99 GeneralConfig.HookConfig.SplunkConfig.Token, 100 GeneralConfig.HookConfig.SplunkConfig.Index, 101 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 102 } 103 apiProxyDownload(stepConfig, &stepTelemetryData) 104 stepTelemetryData.ErrorCode = "0" 105 log.Entry().Info("SUCCESS") 106 }, 107 } 108 109 addApiProxyDownloadFlags(createApiProxyDownloadCmd, &stepConfig) 110 return createApiProxyDownloadCmd 111 } 112 113 func addApiProxyDownloadFlags(cmd *cobra.Command, stepConfig *apiProxyDownloadOptions) { 114 cmd.Flags().StringVar(&stepConfig.APIServiceKey, "apiServiceKey", os.Getenv("PIPER_apiServiceKey"), "Service key JSON string to access the API Management Runtime service instance of plan 'api'") 115 cmd.Flags().StringVar(&stepConfig.APIProxyName, "apiProxyName", os.Getenv("PIPER_apiProxyName"), "Specifies the name of the API Proxy.") 116 cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies api proxy download directory location. The file name should not be included in the path.") 117 118 cmd.MarkFlagRequired("apiServiceKey") 119 cmd.MarkFlagRequired("apiProxyName") 120 cmd.MarkFlagRequired("downloadPath") 121 } 122 123 // retrieve step metadata 124 func apiProxyDownloadMetadata() config.StepData { 125 var theMetaData = config.StepData{ 126 Metadata: config.StepMetadata{ 127 Name: "apiProxyDownload", 128 Aliases: []config.Alias{}, 129 Description: "Download a specific API Proxy from the API Portal", 130 }, 131 Spec: config.StepSpec{ 132 Inputs: config.StepInputs{ 133 Secrets: []config.StepSecrets{ 134 {Name: "apimApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the API Management Runtime service instance of plan 'api'", Type: "jenkins"}, 135 }, 136 Parameters: []config.StepParameters{ 137 { 138 Name: "apiServiceKey", 139 ResourceRef: []config.ResourceReference{ 140 { 141 Name: "apimApiServiceKeyCredentialsId", 142 Param: "apiServiceKey", 143 Type: "secret", 144 }, 145 }, 146 Scope: []string{"PARAMETERS"}, 147 Type: "string", 148 Mandatory: true, 149 Aliases: []config.Alias{}, 150 Default: os.Getenv("PIPER_apiServiceKey"), 151 }, 152 { 153 Name: "apiProxyName", 154 ResourceRef: []config.ResourceReference{}, 155 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 156 Type: "string", 157 Mandatory: true, 158 Aliases: []config.Alias{}, 159 Default: os.Getenv("PIPER_apiProxyName"), 160 }, 161 { 162 Name: "downloadPath", 163 ResourceRef: []config.ResourceReference{}, 164 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 165 Type: "string", 166 Mandatory: true, 167 Aliases: []config.Alias{}, 168 Default: os.Getenv("PIPER_downloadPath"), 169 }, 170 }, 171 }, 172 }, 173 } 174 return theMetaData 175 }