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