github.com/SAP/jenkins-library@v1.362.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  	AbapAddonAssemblyKitCertificateFile string `json:"abapAddonAssemblyKitCertificateFile,omitempty"`
    22  	AbapAddonAssemblyKitCertificatePass string `json:"abapAddonAssemblyKitCertificatePass,omitempty"`
    23  	AbapAddonAssemblyKitEndpoint        string `json:"abapAddonAssemblyKitEndpoint,omitempty"`
    24  	Username                            string `json:"username,omitempty"`
    25  	Password                            string `json:"password,omitempty"`
    26  	AddonDescriptor                     string `json:"addonDescriptor,omitempty"`
    27  	MaxRuntimeInMinutes                 int    `json:"maxRuntimeInMinutes,omitempty"`
    28  	PollingIntervalInSeconds            int    `json:"pollingIntervalInSeconds,omitempty"`
    29  	AbapAddonAssemblyKitOriginHash      string `json:"abapAddonAssemblyKitOriginHash,omitempty"`
    30  }
    31  
    32  type abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment struct {
    33  	abap struct {
    34  		addonDescriptor string
    35  	}
    36  }
    37  
    38  func (p *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) persist(path, resourceName string) {
    39  	content := []struct {
    40  		category string
    41  		name     string
    42  		value    interface{}
    43  	}{
    44  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    45  	}
    46  
    47  	errCount := 0
    48  	for _, param := range content {
    49  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    50  		if err != nil {
    51  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    52  			errCount++
    53  		}
    54  	}
    55  	if errCount > 0 {
    56  		log.Entry().Error("failed to persist Piper environment")
    57  	}
    58  }
    59  
    60  // AbapAddonAssemblyKitReserveNextPackagesCommand This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions.
    61  func AbapAddonAssemblyKitReserveNextPackagesCommand() *cobra.Command {
    62  	const STEP_NAME = "abapAddonAssemblyKitReserveNextPackages"
    63  
    64  	metadata := abapAddonAssemblyKitReserveNextPackagesMetadata()
    65  	var stepConfig abapAddonAssemblyKitReserveNextPackagesOptions
    66  	var startTime time.Time
    67  	var commonPipelineEnvironment abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment
    68  	var logCollector *log.CollectorHook
    69  	var splunkClient *splunk.Splunk
    70  	telemetryClient := &telemetry.Telemetry{}
    71  
    72  	var createAbapAddonAssemblyKitReserveNextPackagesCmd = &cobra.Command{
    73  		Use:   STEP_NAME,
    74  		Short: "This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions.",
    75  		Long: `This step takes the list of Software Component Versions from the addonDescriptor in the commonPipelineEnvironment and determines the ABAP delivery packages.
    76  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:
    77  "P": Package was created in the registry; production can be started / continued
    78  "R": Package exists and is already released; production is not needed and will be skipped.
    79  The step waits until the status "P" or "R" is achieved.
    80  The name, type and namespace of each package is written back to the addonDescriptor in the commonPipelineEnvironment.
    81  <br />
    82  For logon you can either provide a credential with basic authorization (username and password) or two secret text credentials containing the technical s-users certificate (see note [2805811](https://me.sap.com/notes/2805811) for download) as base64 encoded string and the password to decrypt the file
    83  <br />
    84  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    85  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    86  			startTime = time.Now()
    87  			log.SetStepName(STEP_NAME)
    88  			log.SetVerbose(GeneralConfig.Verbose)
    89  
    90  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    91  
    92  			path, _ := os.Getwd()
    93  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    94  			log.RegisterHook(fatalHook)
    95  
    96  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    97  			if err != nil {
    98  				log.SetErrorCategory(log.ErrorConfiguration)
    99  				return err
   100  			}
   101  			log.RegisterSecret(stepConfig.AbapAddonAssemblyKitCertificateFile)
   102  			log.RegisterSecret(stepConfig.AbapAddonAssemblyKitCertificatePass)
   103  			log.RegisterSecret(stepConfig.Username)
   104  			log.RegisterSecret(stepConfig.Password)
   105  			log.RegisterSecret(stepConfig.AbapAddonAssemblyKitOriginHash)
   106  
   107  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
   108  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
   109  				log.RegisterHook(&sentryHook)
   110  			}
   111  
   112  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   113  				splunkClient = &splunk.Splunk{}
   114  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   115  				log.RegisterHook(logCollector)
   116  			}
   117  
   118  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   119  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   120  			}
   121  
   122  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   123  			if err != nil {
   124  				return err
   125  			}
   126  			if err = validation.ValidateStruct(stepConfig); err != nil {
   127  				log.SetErrorCategory(log.ErrorConfiguration)
   128  				return err
   129  			}
   130  
   131  			return nil
   132  		},
   133  		Run: func(_ *cobra.Command, _ []string) {
   134  			stepTelemetryData := telemetry.CustomData{}
   135  			stepTelemetryData.ErrorCode = "1"
   136  			handler := func() {
   137  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   138  				config.RemoveVaultSecretFiles()
   139  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   140  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   141  				stepTelemetryData.PiperCommitHash = GitCommit
   142  				telemetryClient.SetData(&stepTelemetryData)
   143  				telemetryClient.Send()
   144  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   145  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   146  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   147  						GeneralConfig.HookConfig.SplunkConfig.Token,
   148  						GeneralConfig.HookConfig.SplunkConfig.Index,
   149  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   150  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   151  				}
   152  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   153  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   154  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   155  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   156  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   157  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   158  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   159  				}
   160  			}
   161  			log.DeferExitHandler(handler)
   162  			defer handler()
   163  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   164  			abapAddonAssemblyKitReserveNextPackages(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   165  			stepTelemetryData.ErrorCode = "0"
   166  			log.Entry().Info("SUCCESS")
   167  		},
   168  	}
   169  
   170  	addAbapAddonAssemblyKitReserveNextPackagesFlags(createAbapAddonAssemblyKitReserveNextPackagesCmd, &stepConfig)
   171  	return createAbapAddonAssemblyKitReserveNextPackagesCmd
   172  }
   173  
   174  func addAbapAddonAssemblyKitReserveNextPackagesFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitReserveNextPackagesOptions) {
   175  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitCertificateFile, "abapAddonAssemblyKitCertificateFile", os.Getenv("PIPER_abapAddonAssemblyKitCertificateFile"), "base64 encoded certificate pfx file (PKCS12 format) see note [2805811](https://me.sap.com/notes/2805811)")
   176  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitCertificatePass, "abapAddonAssemblyKitCertificatePass", os.Getenv("PIPER_abapAddonAssemblyKitCertificatePass"), "password to decrypt the certificate file")
   177  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   178  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   179  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   180  	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")
   181  	cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 5, "Maximum runtime for status polling in minutes")
   182  	cmd.Flags().IntVar(&stepConfig.PollingIntervalInSeconds, "pollingIntervalInSeconds", 30, "Wait time in seconds between polling calls")
   183  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitOriginHash, "abapAddonAssemblyKitOriginHash", os.Getenv("PIPER_abapAddonAssemblyKitOriginHash"), "Origin Hash for restricted AAKaaS scenarios")
   184  
   185  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   186  	cmd.MarkFlagRequired("addonDescriptor")
   187  }
   188  
   189  // retrieve step metadata
   190  func abapAddonAssemblyKitReserveNextPackagesMetadata() config.StepData {
   191  	var theMetaData = config.StepData{
   192  		Metadata: config.StepMetadata{
   193  			Name:        "abapAddonAssemblyKitReserveNextPackages",
   194  			Aliases:     []config.Alias{},
   195  			Description: "This step determines the ABAP delivery packages (name and type), which are needed to deliver Software Component Versions.",
   196  		},
   197  		Spec: config.StepSpec{
   198  			Inputs: config.StepInputs{
   199  				Secrets: []config.StepSecrets{
   200  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"},
   201  					{Name: "abapAddonAssemblyKitCertificateFileCredentialsId", Description: "Jenkins secret text credential ID containing the base64 encoded certificate pfx file (PKCS12 format) see note [2805811](https://me.sap.com/notes/2805811)", Type: "jenkins"},
   202  					{Name: "abapAddonAssemblyKitCertificatePassCredentialsId", Description: "Jenkins secret text credential ID containing the password to decrypt the certificate file stored in abapAddonAssemblyKitCertificateFileCredentialsId", Type: "jenkins"},
   203  				},
   204  				Parameters: []config.StepParameters{
   205  					{
   206  						Name: "abapAddonAssemblyKitCertificateFile",
   207  						ResourceRef: []config.ResourceReference{
   208  							{
   209  								Name:  "abapAddonAssemblyKitCertificateFileCredentialsId",
   210  								Param: "abapAddonAssemblyKitCertificateFile",
   211  								Type:  "secret",
   212  							},
   213  						},
   214  						Scope:     []string{"PARAMETERS"},
   215  						Type:      "string",
   216  						Mandatory: false,
   217  						Aliases:   []config.Alias{},
   218  						Default:   os.Getenv("PIPER_abapAddonAssemblyKitCertificateFile"),
   219  					},
   220  					{
   221  						Name: "abapAddonAssemblyKitCertificatePass",
   222  						ResourceRef: []config.ResourceReference{
   223  							{
   224  								Name:  "abapAddonAssemblyKitCertificatePassCredentialsId",
   225  								Param: "abapAddonAssemblyKitCertificatePass",
   226  								Type:  "secret",
   227  							},
   228  						},
   229  						Scope:     []string{"PARAMETERS"},
   230  						Type:      "string",
   231  						Mandatory: false,
   232  						Aliases:   []config.Alias{},
   233  						Default:   os.Getenv("PIPER_abapAddonAssemblyKitCertificatePass"),
   234  					},
   235  					{
   236  						Name:        "abapAddonAssemblyKitEndpoint",
   237  						ResourceRef: []config.ResourceReference{},
   238  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   239  						Type:        "string",
   240  						Mandatory:   true,
   241  						Aliases:     []config.Alias{},
   242  						Default:     `https://apps.support.sap.com`,
   243  					},
   244  					{
   245  						Name:        "username",
   246  						ResourceRef: []config.ResourceReference{},
   247  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   248  						Type:        "string",
   249  						Mandatory:   false,
   250  						Aliases:     []config.Alias{},
   251  						Default:     os.Getenv("PIPER_username"),
   252  					},
   253  					{
   254  						Name:        "password",
   255  						ResourceRef: []config.ResourceReference{},
   256  						Scope:       []string{"PARAMETERS"},
   257  						Type:        "string",
   258  						Mandatory:   false,
   259  						Aliases:     []config.Alias{},
   260  						Default:     os.Getenv("PIPER_password"),
   261  					},
   262  					{
   263  						Name: "addonDescriptor",
   264  						ResourceRef: []config.ResourceReference{
   265  							{
   266  								Name:  "commonPipelineEnvironment",
   267  								Param: "abap/addonDescriptor",
   268  							},
   269  						},
   270  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   271  						Type:      "string",
   272  						Mandatory: true,
   273  						Aliases:   []config.Alias{},
   274  						Default:   os.Getenv("PIPER_addonDescriptor"),
   275  					},
   276  					{
   277  						Name:        "maxRuntimeInMinutes",
   278  						ResourceRef: []config.ResourceReference{},
   279  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   280  						Type:        "int",
   281  						Mandatory:   false,
   282  						Aliases:     []config.Alias{},
   283  						Default:     5,
   284  					},
   285  					{
   286  						Name:        "pollingIntervalInSeconds",
   287  						ResourceRef: []config.ResourceReference{},
   288  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   289  						Type:        "int",
   290  						Mandatory:   false,
   291  						Aliases:     []config.Alias{},
   292  						Default:     30,
   293  					},
   294  					{
   295  						Name:        "abapAddonAssemblyKitOriginHash",
   296  						ResourceRef: []config.ResourceReference{},
   297  						Scope:       []string{"PARAMETERS"},
   298  						Type:        "string",
   299  						Mandatory:   false,
   300  						Aliases:     []config.Alias{},
   301  						Default:     os.Getenv("PIPER_abapAddonAssemblyKitOriginHash"),
   302  					},
   303  				},
   304  			},
   305  			Outputs: config.StepOutputs{
   306  				Resources: []config.StepResources{
   307  					{
   308  						Name: "commonPipelineEnvironment",
   309  						Type: "piperEnvironment",
   310  						Parameters: []map[string]interface{}{
   311  							{"name": "abap/addonDescriptor"},
   312  						},
   313  					},
   314  				},
   315  			},
   316  		},
   317  	}
   318  	return theMetaData
   319  }