github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/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  	AbapAddonAssemblyKitEndpoint string `json:"abapAddonAssemblyKitEndpoint,omitempty"`
    22  	Username                     string `json:"username,omitempty"`
    23  	Password                     string `json:"password,omitempty"`
    24  	AddonDescriptorFileName      string `json:"addonDescriptorFileName,omitempty"`
    25  	AddonDescriptor              string `json:"addonDescriptor,omitempty"`
    26  }
    27  
    28  type abapAddonAssemblyKitCheckPVCommonPipelineEnvironment struct {
    29  	abap struct {
    30  		addonDescriptor string
    31  	}
    32  }
    33  
    34  func (p *abapAddonAssemblyKitCheckPVCommonPipelineEnvironment) persist(path, resourceName string) {
    35  	content := []struct {
    36  		category string
    37  		name     string
    38  		value    interface{}
    39  	}{
    40  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    41  	}
    42  
    43  	errCount := 0
    44  	for _, param := range content {
    45  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    46  		if err != nil {
    47  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    48  			errCount++
    49  		}
    50  	}
    51  	if errCount > 0 {
    52  		log.Entry().Error("failed to persist Piper environment")
    53  	}
    54  }
    55  
    56  // AbapAddonAssemblyKitCheckPVCommand This step checks the validity of a Addon Product Version.
    57  func AbapAddonAssemblyKitCheckPVCommand() *cobra.Command {
    58  	const STEP_NAME = "abapAddonAssemblyKitCheckPV"
    59  
    60  	metadata := abapAddonAssemblyKitCheckPVMetadata()
    61  	var stepConfig abapAddonAssemblyKitCheckPVOptions
    62  	var startTime time.Time
    63  	var commonPipelineEnvironment abapAddonAssemblyKitCheckPVCommonPipelineEnvironment
    64  	var logCollector *log.CollectorHook
    65  	var splunkClient *splunk.Splunk
    66  	telemetryClient := &telemetry.Telemetry{}
    67  
    68  	var createAbapAddonAssemblyKitCheckPVCmd = &cobra.Command{
    69  		Use:   STEP_NAME,
    70  		Short: "This step checks the validity of a Addon Product Version.",
    71  		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.
    72  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.
    73  <br />
    74  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    75  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    76  			startTime = time.Now()
    77  			log.SetStepName(STEP_NAME)
    78  			log.SetVerbose(GeneralConfig.Verbose)
    79  
    80  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    81  
    82  			path, _ := os.Getwd()
    83  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    84  			log.RegisterHook(fatalHook)
    85  
    86  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    87  			if err != nil {
    88  				log.SetErrorCategory(log.ErrorConfiguration)
    89  				return err
    90  			}
    91  			log.RegisterSecret(stepConfig.Username)
    92  			log.RegisterSecret(stepConfig.Password)
    93  
    94  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    95  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    96  				log.RegisterHook(&sentryHook)
    97  			}
    98  
    99  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   100  				splunkClient = &splunk.Splunk{}
   101  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   102  				log.RegisterHook(logCollector)
   103  			}
   104  
   105  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   106  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   107  			}
   108  
   109  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   110  			if err != nil {
   111  				return err
   112  			}
   113  			if err = validation.ValidateStruct(stepConfig); err != nil {
   114  				log.SetErrorCategory(log.ErrorConfiguration)
   115  				return err
   116  			}
   117  
   118  			return nil
   119  		},
   120  		Run: func(_ *cobra.Command, _ []string) {
   121  			stepTelemetryData := telemetry.CustomData{}
   122  			stepTelemetryData.ErrorCode = "1"
   123  			handler := func() {
   124  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   125  				config.RemoveVaultSecretFiles()
   126  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   127  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   128  				stepTelemetryData.PiperCommitHash = GitCommit
   129  				telemetryClient.SetData(&stepTelemetryData)
   130  				telemetryClient.Send()
   131  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   132  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   133  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   134  						GeneralConfig.HookConfig.SplunkConfig.Token,
   135  						GeneralConfig.HookConfig.SplunkConfig.Index,
   136  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   137  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   138  				}
   139  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   140  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   141  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   142  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   143  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   144  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   145  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   146  				}
   147  			}
   148  			log.DeferExitHandler(handler)
   149  			defer handler()
   150  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   151  			abapAddonAssemblyKitCheckPV(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   152  			stepTelemetryData.ErrorCode = "0"
   153  			log.Entry().Info("SUCCESS")
   154  		},
   155  	}
   156  
   157  	addAbapAddonAssemblyKitCheckPVFlags(createAbapAddonAssemblyKitCheckPVCmd, &stepConfig)
   158  	return createAbapAddonAssemblyKitCheckPVCmd
   159  }
   160  
   161  func addAbapAddonAssemblyKitCheckPVFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitCheckPVOptions) {
   162  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   163  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   164  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   165  	cmd.Flags().StringVar(&stepConfig.AddonDescriptorFileName, "addonDescriptorFileName", `addon.yml`, "File name of the YAML file which describes the Product Version and corresponding Software Component Versions")
   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("addonDescriptorFileName")
   172  }
   173  
   174  // retrieve step metadata
   175  func abapAddonAssemblyKitCheckPVMetadata() config.StepData {
   176  	var theMetaData = config.StepData{
   177  		Metadata: config.StepMetadata{
   178  			Name:        "abapAddonAssemblyKitCheckPV",
   179  			Aliases:     []config.Alias{},
   180  			Description: "This step checks the validity of a Addon Product Version.",
   181  		},
   182  		Spec: config.StepSpec{
   183  			Inputs: config.StepInputs{
   184  				Secrets: []config.StepSecrets{
   185  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "CredentialsId 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:        "addonDescriptorFileName",
   217  						ResourceRef: []config.ResourceReference{},
   218  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   219  						Type:        "string",
   220  						Mandatory:   true,
   221  						Aliases:     []config.Alias{},
   222  						Default:     `addon.yml`,
   223  					},
   224  					{
   225  						Name: "addonDescriptor",
   226  						ResourceRef: []config.ResourceReference{
   227  							{
   228  								Name:  "commonPipelineEnvironment",
   229  								Param: "abap/addonDescriptor",
   230  							},
   231  						},
   232  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   233  						Type:      "string",
   234  						Mandatory: false,
   235  						Aliases:   []config.Alias{},
   236  						Default:   os.Getenv("PIPER_addonDescriptor"),
   237  					},
   238  				},
   239  			},
   240  			Outputs: config.StepOutputs{
   241  				Resources: []config.StepResources{
   242  					{
   243  						Name: "commonPipelineEnvironment",
   244  						Type: "piperEnvironment",
   245  						Parameters: []map[string]interface{}{
   246  							{"name": "abap/addonDescriptor"},
   247  						},
   248  					},
   249  				},
   250  			},
   251  		},
   252  	}
   253  	return theMetaData
   254  }