github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapAddonAssemblyKitReserveNextPackages_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 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type abapAddonAssemblyKitReserveNextPackagesOptions struct { 21 AbapAddonAssemblyKitEndpoint string `json:"abapAddonAssemblyKitEndpoint,omitempty"` 22 Username string `json:"username,omitempty"` 23 Password string `json:"password,omitempty"` 24 AddonDescriptor string `json:"addonDescriptor,omitempty"` 25 MaxRuntimeInMinutes int `json:"maxRuntimeInMinutes,omitempty"` 26 PollingIntervalInSeconds int `json:"pollingIntervalInSeconds,omitempty"` 27 } 28 29 type abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment struct { 30 abap struct { 31 addonDescriptor string 32 } 33 } 34 35 func (p *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) persist(path, resourceName string) { 36 content := []struct { 37 category string 38 name string 39 value interface{} 40 }{ 41 {category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor}, 42 } 43 44 errCount := 0 45 for _, param := range content { 46 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 47 if err != nil { 48 log.Entry().WithError(err).Error("Error persisting piper environment.") 49 errCount++ 50 } 51 } 52 if errCount > 0 { 53 log.Entry().Error("failed to persist Piper environment") 54 } 55 } 56 57 // AbapAddonAssemblyKitReserveNextPackagesCommand This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions. 58 func AbapAddonAssemblyKitReserveNextPackagesCommand() *cobra.Command { 59 const STEP_NAME = "abapAddonAssemblyKitReserveNextPackages" 60 61 metadata := abapAddonAssemblyKitReserveNextPackagesMetadata() 62 var stepConfig abapAddonAssemblyKitReserveNextPackagesOptions 63 var startTime time.Time 64 var commonPipelineEnvironment abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment 65 var logCollector *log.CollectorHook 66 var splunkClient *splunk.Splunk 67 telemetryClient := &telemetry.Telemetry{} 68 69 var createAbapAddonAssemblyKitReserveNextPackagesCmd = &cobra.Command{ 70 Use: STEP_NAME, 71 Short: "This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions.", 72 Long: `This step takes the list of Software Component Versions from the addonDescriptor in the commonPipelineEnvironment and determines the ABAP delivery packages. 73 If a package does not exist yet in the package registry, it is created there. The response contains detail information for this package and a package status, which determines the next actions: 74 "P": Package was created in the registry; production can be started / continued 75 "R": Package exists and is already released; production is not needed and will be skipped 76 The steps waits until the status "P" or "R" is achieved. 77 The name, type and namespace of each package is written back to the addonDescriptor in the commonPipelineEnvironment. 78 <br /> 79 For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`, 80 PreRunE: func(cmd *cobra.Command, _ []string) error { 81 startTime = time.Now() 82 log.SetStepName(STEP_NAME) 83 log.SetVerbose(GeneralConfig.Verbose) 84 85 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 86 87 path, _ := os.Getwd() 88 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 89 log.RegisterHook(fatalHook) 90 91 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 92 if err != nil { 93 log.SetErrorCategory(log.ErrorConfiguration) 94 return err 95 } 96 log.RegisterSecret(stepConfig.Username) 97 log.RegisterSecret(stepConfig.Password) 98 99 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 100 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 101 log.RegisterHook(&sentryHook) 102 } 103 104 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 105 splunkClient = &splunk.Splunk{} 106 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 107 log.RegisterHook(logCollector) 108 } 109 110 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 111 if err != nil { 112 return err 113 } 114 if err = validation.ValidateStruct(stepConfig); err != nil { 115 log.SetErrorCategory(log.ErrorConfiguration) 116 return err 117 } 118 119 return nil 120 }, 121 Run: func(_ *cobra.Command, _ []string) { 122 stepTelemetryData := telemetry.CustomData{} 123 stepTelemetryData.ErrorCode = "1" 124 handler := func() { 125 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 126 config.RemoveVaultSecretFiles() 127 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 128 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 129 stepTelemetryData.PiperCommitHash = GitCommit 130 telemetryClient.SetData(&stepTelemetryData) 131 telemetryClient.Send() 132 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 133 splunkClient.Send(telemetryClient.GetData(), logCollector) 134 } 135 } 136 log.DeferExitHandler(handler) 137 defer handler() 138 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 139 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 140 splunkClient.Initialize(GeneralConfig.CorrelationID, 141 GeneralConfig.HookConfig.SplunkConfig.Dsn, 142 GeneralConfig.HookConfig.SplunkConfig.Token, 143 GeneralConfig.HookConfig.SplunkConfig.Index, 144 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 145 } 146 abapAddonAssemblyKitReserveNextPackages(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 147 stepTelemetryData.ErrorCode = "0" 148 log.Entry().Info("SUCCESS") 149 }, 150 } 151 152 addAbapAddonAssemblyKitReserveNextPackagesFlags(createAbapAddonAssemblyKitReserveNextPackagesCmd, &stepConfig) 153 return createAbapAddonAssemblyKitReserveNextPackagesCmd 154 } 155 156 func addAbapAddonAssemblyKitReserveNextPackagesFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitReserveNextPackagesOptions) { 157 cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system") 158 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system") 159 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system") 160 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") 161 cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 5, "Maximum runtime for status polling in minutes") 162 cmd.Flags().IntVar(&stepConfig.PollingIntervalInSeconds, "pollingIntervalInSeconds", 30, "Wait time in seconds between polling calls") 163 164 cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint") 165 cmd.MarkFlagRequired("username") 166 cmd.MarkFlagRequired("password") 167 cmd.MarkFlagRequired("addonDescriptor") 168 } 169 170 // retrieve step metadata 171 func abapAddonAssemblyKitReserveNextPackagesMetadata() config.StepData { 172 var theMetaData = config.StepData{ 173 Metadata: config.StepMetadata{ 174 Name: "abapAddonAssemblyKitReserveNextPackages", 175 Aliases: []config.Alias{}, 176 Description: "This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions.", 177 }, 178 Spec: config.StepSpec{ 179 Inputs: config.StepInputs{ 180 Secrets: []config.StepSecrets{ 181 {Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"}, 182 }, 183 Parameters: []config.StepParameters{ 184 { 185 Name: "abapAddonAssemblyKitEndpoint", 186 ResourceRef: []config.ResourceReference{}, 187 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 188 Type: "string", 189 Mandatory: true, 190 Aliases: []config.Alias{}, 191 Default: `https://apps.support.sap.com`, 192 }, 193 { 194 Name: "username", 195 ResourceRef: []config.ResourceReference{}, 196 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 197 Type: "string", 198 Mandatory: true, 199 Aliases: []config.Alias{}, 200 Default: os.Getenv("PIPER_username"), 201 }, 202 { 203 Name: "password", 204 ResourceRef: []config.ResourceReference{}, 205 Scope: []string{"PARAMETERS"}, 206 Type: "string", 207 Mandatory: true, 208 Aliases: []config.Alias{}, 209 Default: os.Getenv("PIPER_password"), 210 }, 211 { 212 Name: "addonDescriptor", 213 ResourceRef: []config.ResourceReference{ 214 { 215 Name: "commonPipelineEnvironment", 216 Param: "abap/addonDescriptor", 217 }, 218 }, 219 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 220 Type: "string", 221 Mandatory: true, 222 Aliases: []config.Alias{}, 223 Default: os.Getenv("PIPER_addonDescriptor"), 224 }, 225 { 226 Name: "maxRuntimeInMinutes", 227 ResourceRef: []config.ResourceReference{}, 228 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 229 Type: "int", 230 Mandatory: false, 231 Aliases: []config.Alias{}, 232 Default: 5, 233 }, 234 { 235 Name: "pollingIntervalInSeconds", 236 ResourceRef: []config.ResourceReference{}, 237 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 238 Type: "int", 239 Mandatory: false, 240 Aliases: []config.Alias{}, 241 Default: 30, 242 }, 243 }, 244 }, 245 Outputs: config.StepOutputs{ 246 Resources: []config.StepResources{ 247 { 248 Name: "commonPipelineEnvironment", 249 Type: "piperEnvironment", 250 Parameters: []map[string]interface{}{ 251 {"name": "abap/addonDescriptor"}, 252 }, 253 }, 254 }, 255 }, 256 }, 257 } 258 return theMetaData 259 }