github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapAddonAssemblyKitRegisterPackages_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 abapAddonAssemblyKitRegisterPackagesOptions 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  }
    26  
    27  type abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment struct {
    28  	abap struct {
    29  		addonDescriptor string
    30  	}
    31  }
    32  
    33  func (p *abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment) persist(path, resourceName string) {
    34  	content := []struct {
    35  		category string
    36  		name     string
    37  		value    interface{}
    38  	}{
    39  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    40  	}
    41  
    42  	errCount := 0
    43  	for _, param := range content {
    44  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    45  		if err != nil {
    46  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    47  			errCount++
    48  		}
    49  	}
    50  	if errCount > 0 {
    51  		log.Entry().Error("failed to persist Piper environment")
    52  	}
    53  }
    54  
    55  // AbapAddonAssemblyKitRegisterPackagesCommand This step uploads the SAR archives and creates physical Delivery Packages to AAKaaS.
    56  func AbapAddonAssemblyKitRegisterPackagesCommand() *cobra.Command {
    57  	const STEP_NAME = "abapAddonAssemblyKitRegisterPackages"
    58  
    59  	metadata := abapAddonAssemblyKitRegisterPackagesMetadata()
    60  	var stepConfig abapAddonAssemblyKitRegisterPackagesOptions
    61  	var startTime time.Time
    62  	var commonPipelineEnvironment abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment
    63  	var logCollector *log.CollectorHook
    64  	var splunkClient *splunk.Splunk
    65  	telemetryClient := &telemetry.Telemetry{}
    66  
    67  	var createAbapAddonAssemblyKitRegisterPackagesCmd = &cobra.Command{
    68  		Use:   STEP_NAME,
    69  		Short: "This step uploads the SAR archives and creates physical Delivery Packages to AAKaaS.",
    70  		Long: `This step takes the list of Software Component Versions from the addonDescriptor in the commonPipelineEnvironment.
    71  For Packages in status "P" = planned it uploads the SAR archive with the data file and metadata XML of the Delivery Packages composed and exported in the build system
    72  and creates physical Delivery Package in AAKaaS.
    73  The new status "L" = locked is written back to the addonDescriptor in the commonPipelineEnvironment.
    74  <br />
    75  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    76  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    77  			startTime = time.Now()
    78  			log.SetStepName(STEP_NAME)
    79  			log.SetVerbose(GeneralConfig.Verbose)
    80  
    81  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    82  
    83  			path, _ := os.Getwd()
    84  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    85  			log.RegisterHook(fatalHook)
    86  
    87  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    88  			if err != nil {
    89  				log.SetErrorCategory(log.ErrorConfiguration)
    90  				return err
    91  			}
    92  			log.RegisterSecret(stepConfig.Username)
    93  			log.RegisterSecret(stepConfig.Password)
    94  
    95  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    96  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    97  				log.RegisterHook(&sentryHook)
    98  			}
    99  
   100  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   101  				splunkClient = &splunk.Splunk{}
   102  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   103  				log.RegisterHook(logCollector)
   104  			}
   105  
   106  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   107  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   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.Initialize(GeneralConfig.CorrelationID,
   134  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   135  						GeneralConfig.HookConfig.SplunkConfig.Token,
   136  						GeneralConfig.HookConfig.SplunkConfig.Index,
   137  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   138  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   139  				}
   140  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   141  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   142  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   143  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   144  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   145  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   146  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   147  				}
   148  			}
   149  			log.DeferExitHandler(handler)
   150  			defer handler()
   151  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   152  			abapAddonAssemblyKitRegisterPackages(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   153  			stepTelemetryData.ErrorCode = "0"
   154  			log.Entry().Info("SUCCESS")
   155  		},
   156  	}
   157  
   158  	addAbapAddonAssemblyKitRegisterPackagesFlags(createAbapAddonAssemblyKitRegisterPackagesCmd, &stepConfig)
   159  	return createAbapAddonAssemblyKitRegisterPackagesCmd
   160  }
   161  
   162  func addAbapAddonAssemblyKitRegisterPackagesFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitRegisterPackagesOptions) {
   163  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   164  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   165  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   166  	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")
   167  
   168  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   169  	cmd.MarkFlagRequired("username")
   170  	cmd.MarkFlagRequired("password")
   171  	cmd.MarkFlagRequired("addonDescriptor")
   172  }
   173  
   174  // retrieve step metadata
   175  func abapAddonAssemblyKitRegisterPackagesMetadata() config.StepData {
   176  	var theMetaData = config.StepData{
   177  		Metadata: config.StepMetadata{
   178  			Name:        "abapAddonAssemblyKitRegisterPackages",
   179  			Aliases:     []config.Alias{},
   180  			Description: "This step uploads the SAR archives and creates physical Delivery Packages to AAKaaS.",
   181  		},
   182  		Spec: config.StepSpec{
   183  			Inputs: config.StepInputs{
   184  				Secrets: []config.StepSecrets{
   185  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"},
   186  				},
   187  				Parameters: []config.StepParameters{
   188  					{
   189  						Name:        "abapAddonAssemblyKitEndpoint",
   190  						ResourceRef: []config.ResourceReference{},
   191  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   192  						Type:        "string",
   193  						Mandatory:   true,
   194  						Aliases:     []config.Alias{},
   195  						Default:     `https://apps.support.sap.com`,
   196  					},
   197  					{
   198  						Name:        "username",
   199  						ResourceRef: []config.ResourceReference{},
   200  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   201  						Type:        "string",
   202  						Mandatory:   true,
   203  						Aliases:     []config.Alias{},
   204  						Default:     os.Getenv("PIPER_username"),
   205  					},
   206  					{
   207  						Name:        "password",
   208  						ResourceRef: []config.ResourceReference{},
   209  						Scope:       []string{"PARAMETERS"},
   210  						Type:        "string",
   211  						Mandatory:   true,
   212  						Aliases:     []config.Alias{},
   213  						Default:     os.Getenv("PIPER_password"),
   214  					},
   215  					{
   216  						Name: "addonDescriptor",
   217  						ResourceRef: []config.ResourceReference{
   218  							{
   219  								Name:  "commonPipelineEnvironment",
   220  								Param: "abap/addonDescriptor",
   221  							},
   222  						},
   223  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   224  						Type:      "string",
   225  						Mandatory: true,
   226  						Aliases:   []config.Alias{},
   227  						Default:   os.Getenv("PIPER_addonDescriptor"),
   228  					},
   229  				},
   230  			},
   231  			Outputs: config.StepOutputs{
   232  				Resources: []config.StepResources{
   233  					{
   234  						Name: "commonPipelineEnvironment",
   235  						Type: "piperEnvironment",
   236  						Parameters: []map[string]interface{}{
   237  							{"name": "abap/addonDescriptor"},
   238  						},
   239  					},
   240  				},
   241  			},
   242  		},
   243  	}
   244  	return theMetaData
   245  }