github.com/SAP/jenkins-library@v1.362.0/cmd/abapAddonAssemblyKitCheckPV_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 abapAddonAssemblyKitCheckPVOptions 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  	AddonDescriptorFileName             string `json:"addonDescriptorFileName,omitempty"`
    27  	AddonDescriptor                     string `json:"addonDescriptor,omitempty"`
    28  }
    29  
    30  type abapAddonAssemblyKitCheckPVCommonPipelineEnvironment struct {
    31  	abap struct {
    32  		addonDescriptor string
    33  	}
    34  }
    35  
    36  func (p *abapAddonAssemblyKitCheckPVCommonPipelineEnvironment) persist(path, resourceName string) {
    37  	content := []struct {
    38  		category string
    39  		name     string
    40  		value    interface{}
    41  	}{
    42  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    43  	}
    44  
    45  	errCount := 0
    46  	for _, param := range content {
    47  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    48  		if err != nil {
    49  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    50  			errCount++
    51  		}
    52  	}
    53  	if errCount > 0 {
    54  		log.Entry().Error("failed to persist Piper environment")
    55  	}
    56  }
    57  
    58  // AbapAddonAssemblyKitCheckPVCommand This step checks the validity of a Addon Product Version.
    59  func AbapAddonAssemblyKitCheckPVCommand() *cobra.Command {
    60  	const STEP_NAME = "abapAddonAssemblyKitCheckPV"
    61  
    62  	metadata := abapAddonAssemblyKitCheckPVMetadata()
    63  	var stepConfig abapAddonAssemblyKitCheckPVOptions
    64  	var startTime time.Time
    65  	var commonPipelineEnvironment abapAddonAssemblyKitCheckPVCommonPipelineEnvironment
    66  	var logCollector *log.CollectorHook
    67  	var splunkClient *splunk.Splunk
    68  	telemetryClient := &telemetry.Telemetry{}
    69  
    70  	var createAbapAddonAssemblyKitCheckPVCmd = &cobra.Command{
    71  		Use:   STEP_NAME,
    72  		Short: "This step checks the validity of a Addon Product Version.",
    73  		Long: `This step checks by calling AAKaaS whether the Addon Product Version in the addonDescriptor configuration file specified via addonDescriptorFileName (e.g. addon.yml) does exist or is a valid successor of an existing Product Version.
    74  It resolves the dotted version string into version, support package stack level and patch level and writes it to the addonDescriptor structure in the Piper commonPipelineEnvironment for usage of subsequent pipeline steps.
    75  <br />
    76  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
    77  <br />
    78  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    79  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    80  			startTime = time.Now()
    81  			log.SetStepName(STEP_NAME)
    82  			log.SetVerbose(GeneralConfig.Verbose)
    83  
    84  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    85  
    86  			path, _ := os.Getwd()
    87  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    88  			log.RegisterHook(fatalHook)
    89  
    90  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    91  			if err != nil {
    92  				log.SetErrorCategory(log.ErrorConfiguration)
    93  				return err
    94  			}
    95  			log.RegisterSecret(stepConfig.AbapAddonAssemblyKitCertificateFile)
    96  			log.RegisterSecret(stepConfig.AbapAddonAssemblyKitCertificatePass)
    97  			log.RegisterSecret(stepConfig.Username)
    98  			log.RegisterSecret(stepConfig.Password)
    99  
   100  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
   101  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
   102  				log.RegisterHook(&sentryHook)
   103  			}
   104  
   105  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   106  				splunkClient = &splunk.Splunk{}
   107  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   108  				log.RegisterHook(logCollector)
   109  			}
   110  
   111  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   112  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   113  			}
   114  
   115  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   116  			if err != nil {
   117  				return err
   118  			}
   119  			if err = validation.ValidateStruct(stepConfig); err != nil {
   120  				log.SetErrorCategory(log.ErrorConfiguration)
   121  				return err
   122  			}
   123  
   124  			return nil
   125  		},
   126  		Run: func(_ *cobra.Command, _ []string) {
   127  			stepTelemetryData := telemetry.CustomData{}
   128  			stepTelemetryData.ErrorCode = "1"
   129  			handler := func() {
   130  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   131  				config.RemoveVaultSecretFiles()
   132  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   133  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   134  				stepTelemetryData.PiperCommitHash = GitCommit
   135  				telemetryClient.SetData(&stepTelemetryData)
   136  				telemetryClient.Send()
   137  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   138  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   139  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   140  						GeneralConfig.HookConfig.SplunkConfig.Token,
   141  						GeneralConfig.HookConfig.SplunkConfig.Index,
   142  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   143  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   144  				}
   145  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   146  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   147  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   148  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   149  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   150  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   151  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   152  				}
   153  			}
   154  			log.DeferExitHandler(handler)
   155  			defer handler()
   156  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   157  			abapAddonAssemblyKitCheckPV(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   158  			stepTelemetryData.ErrorCode = "0"
   159  			log.Entry().Info("SUCCESS")
   160  		},
   161  	}
   162  
   163  	addAbapAddonAssemblyKitCheckPVFlags(createAbapAddonAssemblyKitCheckPVCmd, &stepConfig)
   164  	return createAbapAddonAssemblyKitCheckPVCmd
   165  }
   166  
   167  func addAbapAddonAssemblyKitCheckPVFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitCheckPVOptions) {
   168  	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)")
   169  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitCertificatePass, "abapAddonAssemblyKitCertificatePass", os.Getenv("PIPER_abapAddonAssemblyKitCertificatePass"), "password to decrypt the certificate file")
   170  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   171  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   172  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   173  	cmd.Flags().StringVar(&stepConfig.AddonDescriptorFileName, "addonDescriptorFileName", `addon.yml`, "File name of the YAML file which describes the Product Version and corresponding Software Component Versions")
   174  	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")
   175  
   176  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   177  	cmd.MarkFlagRequired("addonDescriptorFileName")
   178  }
   179  
   180  // retrieve step metadata
   181  func abapAddonAssemblyKitCheckPVMetadata() config.StepData {
   182  	var theMetaData = config.StepData{
   183  		Metadata: config.StepMetadata{
   184  			Name:        "abapAddonAssemblyKitCheckPV",
   185  			Aliases:     []config.Alias{},
   186  			Description: "This step checks the validity of a Addon Product Version.",
   187  		},
   188  		Spec: config.StepSpec{
   189  			Inputs: config.StepInputs{
   190  				Secrets: []config.StepSecrets{
   191  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "CredentialsId stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"},
   192  					{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"},
   193  					{Name: "abapAddonAssemblyKitCertificatePassCredentialsId", Description: "Jenkins secret text credential ID containing the password to decrypt the certificate file stored in abapAddonAssemblyKitCertificateFileCredentialsId", Type: "jenkins"},
   194  				},
   195  				Parameters: []config.StepParameters{
   196  					{
   197  						Name: "abapAddonAssemblyKitCertificateFile",
   198  						ResourceRef: []config.ResourceReference{
   199  							{
   200  								Name:  "abapAddonAssemblyKitCertificateFileCredentialsId",
   201  								Param: "abapAddonAssemblyKitCertificateFile",
   202  								Type:  "secret",
   203  							},
   204  						},
   205  						Scope:     []string{"PARAMETERS"},
   206  						Type:      "string",
   207  						Mandatory: false,
   208  						Aliases:   []config.Alias{},
   209  						Default:   os.Getenv("PIPER_abapAddonAssemblyKitCertificateFile"),
   210  					},
   211  					{
   212  						Name: "abapAddonAssemblyKitCertificatePass",
   213  						ResourceRef: []config.ResourceReference{
   214  							{
   215  								Name:  "abapAddonAssemblyKitCertificatePassCredentialsId",
   216  								Param: "abapAddonAssemblyKitCertificatePass",
   217  								Type:  "secret",
   218  							},
   219  						},
   220  						Scope:     []string{"PARAMETERS"},
   221  						Type:      "string",
   222  						Mandatory: false,
   223  						Aliases:   []config.Alias{},
   224  						Default:   os.Getenv("PIPER_abapAddonAssemblyKitCertificatePass"),
   225  					},
   226  					{
   227  						Name:        "abapAddonAssemblyKitEndpoint",
   228  						ResourceRef: []config.ResourceReference{},
   229  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   230  						Type:        "string",
   231  						Mandatory:   true,
   232  						Aliases:     []config.Alias{},
   233  						Default:     `https://apps.support.sap.com`,
   234  					},
   235  					{
   236  						Name:        "username",
   237  						ResourceRef: []config.ResourceReference{},
   238  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   239  						Type:        "string",
   240  						Mandatory:   false,
   241  						Aliases:     []config.Alias{},
   242  						Default:     os.Getenv("PIPER_username"),
   243  					},
   244  					{
   245  						Name:        "password",
   246  						ResourceRef: []config.ResourceReference{},
   247  						Scope:       []string{"PARAMETERS"},
   248  						Type:        "string",
   249  						Mandatory:   false,
   250  						Aliases:     []config.Alias{},
   251  						Default:     os.Getenv("PIPER_password"),
   252  					},
   253  					{
   254  						Name:        "addonDescriptorFileName",
   255  						ResourceRef: []config.ResourceReference{},
   256  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   257  						Type:        "string",
   258  						Mandatory:   true,
   259  						Aliases:     []config.Alias{},
   260  						Default:     `addon.yml`,
   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: false,
   273  						Aliases:   []config.Alias{},
   274  						Default:   os.Getenv("PIPER_addonDescriptor"),
   275  					},
   276  				},
   277  			},
   278  			Outputs: config.StepOutputs{
   279  				Resources: []config.StepResources{
   280  					{
   281  						Name: "commonPipelineEnvironment",
   282  						Type: "piperEnvironment",
   283  						Parameters: []map[string]interface{}{
   284  							{"name": "abap/addonDescriptor"},
   285  						},
   286  					},
   287  				},
   288  			},
   289  		},
   290  	}
   291  	return theMetaData
   292  }