github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapAddonAssemblyKitPublishTargetVector_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 abapAddonAssemblyKitPublishTargetVectorOptions struct { 19 AbapAddonAssemblyKitEndpoint string `json:"abapAddonAssemblyKitEndpoint,omitempty"` 20 Username string `json:"username,omitempty"` 21 Password string `json:"password,omitempty"` 22 TargetVectorScope string `json:"targetVectorScope,omitempty" validate:"possible-values=T P"` 23 MaxRuntimeInMinutes int `json:"maxRuntimeInMinutes,omitempty"` 24 PollingIntervalInSeconds int `json:"pollingIntervalInSeconds,omitempty"` 25 AddonDescriptor string `json:"addonDescriptor,omitempty"` 26 } 27 28 // AbapAddonAssemblyKitPublishTargetVectorCommand This step triggers the publication of the Target Vector according to the specified scope. 29 func AbapAddonAssemblyKitPublishTargetVectorCommand() *cobra.Command { 30 const STEP_NAME = "abapAddonAssemblyKitPublishTargetVector" 31 32 metadata := abapAddonAssemblyKitPublishTargetVectorMetadata() 33 var stepConfig abapAddonAssemblyKitPublishTargetVectorOptions 34 var startTime time.Time 35 var logCollector *log.CollectorHook 36 var splunkClient *splunk.Splunk 37 telemetryClient := &telemetry.Telemetry{} 38 39 var createAbapAddonAssemblyKitPublishTargetVectorCmd = &cobra.Command{ 40 Use: STEP_NAME, 41 Short: "This step triggers the publication of the Target Vector according to the specified scope.", 42 Long: `This step reads the Target Vector ID from the addonDescriptor in the commonPipelineEnvironment and triggers the publication of the Target Vector. 43 With targetVectorScope "T" the Target Vector will be published to the test environment and with targetVectorScope "P" it will be published to the productive environment. 44 <br /> 45 For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`, 46 PreRunE: func(cmd *cobra.Command, _ []string) error { 47 startTime = time.Now() 48 log.SetStepName(STEP_NAME) 49 log.SetVerbose(GeneralConfig.Verbose) 50 51 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 52 53 path, _ := os.Getwd() 54 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 55 log.RegisterHook(fatalHook) 56 57 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 58 if err != nil { 59 log.SetErrorCategory(log.ErrorConfiguration) 60 return err 61 } 62 log.RegisterSecret(stepConfig.Username) 63 log.RegisterSecret(stepConfig.Password) 64 65 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 66 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 67 log.RegisterHook(&sentryHook) 68 } 69 70 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 71 splunkClient = &splunk.Splunk{} 72 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 73 log.RegisterHook(logCollector) 74 } 75 76 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 77 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 78 } 79 80 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 81 if err != nil { 82 return err 83 } 84 if err = validation.ValidateStruct(stepConfig); err != nil { 85 log.SetErrorCategory(log.ErrorConfiguration) 86 return err 87 } 88 89 return nil 90 }, 91 Run: func(_ *cobra.Command, _ []string) { 92 stepTelemetryData := telemetry.CustomData{} 93 stepTelemetryData.ErrorCode = "1" 94 handler := func() { 95 config.RemoveVaultSecretFiles() 96 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 97 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 98 stepTelemetryData.PiperCommitHash = GitCommit 99 telemetryClient.SetData(&stepTelemetryData) 100 telemetryClient.Send() 101 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 102 splunkClient.Initialize(GeneralConfig.CorrelationID, 103 GeneralConfig.HookConfig.SplunkConfig.Dsn, 104 GeneralConfig.HookConfig.SplunkConfig.Token, 105 GeneralConfig.HookConfig.SplunkConfig.Index, 106 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 107 splunkClient.Send(telemetryClient.GetData(), logCollector) 108 } 109 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 110 splunkClient.Initialize(GeneralConfig.CorrelationID, 111 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 112 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 113 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 114 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 115 splunkClient.Send(telemetryClient.GetData(), logCollector) 116 } 117 } 118 log.DeferExitHandler(handler) 119 defer handler() 120 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 121 abapAddonAssemblyKitPublishTargetVector(stepConfig, &stepTelemetryData) 122 stepTelemetryData.ErrorCode = "0" 123 log.Entry().Info("SUCCESS") 124 }, 125 } 126 127 addAbapAddonAssemblyKitPublishTargetVectorFlags(createAbapAddonAssemblyKitPublishTargetVectorCmd, &stepConfig) 128 return createAbapAddonAssemblyKitPublishTargetVectorCmd 129 } 130 131 func addAbapAddonAssemblyKitPublishTargetVectorFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitPublishTargetVectorOptions) { 132 cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system") 133 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system") 134 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system") 135 cmd.Flags().StringVar(&stepConfig.TargetVectorScope, "targetVectorScope", `T`, "Determines whether the Target Vector is published to the productive ('P') or test ('T') environment") 136 cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 16, "Maximum runtime for status polling in minutes") 137 cmd.Flags().IntVar(&stepConfig.PollingIntervalInSeconds, "pollingIntervalInSeconds", 60, "Wait time in seconds between polling calls") 138 cmd.Flags().StringVar(&stepConfig.AddonDescriptor, "addonDescriptor", os.Getenv("PIPER_addonDescriptor"), "Structure in the commonPipelineEnvironment containing information about the Product Version and corresponding Software Component Versions") 139 140 cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint") 141 cmd.MarkFlagRequired("username") 142 cmd.MarkFlagRequired("password") 143 cmd.MarkFlagRequired("addonDescriptor") 144 } 145 146 // retrieve step metadata 147 func abapAddonAssemblyKitPublishTargetVectorMetadata() config.StepData { 148 var theMetaData = config.StepData{ 149 Metadata: config.StepMetadata{ 150 Name: "abapAddonAssemblyKitPublishTargetVector", 151 Aliases: []config.Alias{}, 152 Description: "This step triggers the publication of the Target Vector according to the specified scope.", 153 }, 154 Spec: config.StepSpec{ 155 Inputs: config.StepInputs{ 156 Secrets: []config.StepSecrets{ 157 {Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"}, 158 }, 159 Parameters: []config.StepParameters{ 160 { 161 Name: "abapAddonAssemblyKitEndpoint", 162 ResourceRef: []config.ResourceReference{}, 163 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 164 Type: "string", 165 Mandatory: true, 166 Aliases: []config.Alias{}, 167 Default: `https://apps.support.sap.com`, 168 }, 169 { 170 Name: "username", 171 ResourceRef: []config.ResourceReference{}, 172 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 173 Type: "string", 174 Mandatory: true, 175 Aliases: []config.Alias{}, 176 Default: os.Getenv("PIPER_username"), 177 }, 178 { 179 Name: "password", 180 ResourceRef: []config.ResourceReference{}, 181 Scope: []string{"PARAMETERS"}, 182 Type: "string", 183 Mandatory: true, 184 Aliases: []config.Alias{}, 185 Default: os.Getenv("PIPER_password"), 186 }, 187 { 188 Name: "targetVectorScope", 189 ResourceRef: []config.ResourceReference{}, 190 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 191 Type: "string", 192 Mandatory: false, 193 Aliases: []config.Alias{}, 194 Default: `T`, 195 }, 196 { 197 Name: "maxRuntimeInMinutes", 198 ResourceRef: []config.ResourceReference{}, 199 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 200 Type: "int", 201 Mandatory: false, 202 Aliases: []config.Alias{}, 203 Default: 16, 204 }, 205 { 206 Name: "pollingIntervalInSeconds", 207 ResourceRef: []config.ResourceReference{}, 208 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 209 Type: "int", 210 Mandatory: false, 211 Aliases: []config.Alias{}, 212 Default: 60, 213 }, 214 { 215 Name: "addonDescriptor", 216 ResourceRef: []config.ResourceReference{ 217 { 218 Name: "commonPipelineEnvironment", 219 Param: "abap/addonDescriptor", 220 }, 221 }, 222 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 223 Type: "string", 224 Mandatory: true, 225 Aliases: []config.Alias{}, 226 Default: os.Getenv("PIPER_addonDescriptor"), 227 }, 228 }, 229 }, 230 }, 231 } 232 return theMetaData 233 }