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