github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapAddonAssemblyKitCheckCVs_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 abapAddonAssemblyKitCheckCVsOptions 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 abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment struct {
    29  	abap struct {
    30  		addonDescriptor string
    31  	}
    32  }
    33  
    34  func (p *abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment) 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  // AbapAddonAssemblyKitCheckCVsCommand This step checks the validity of ABAP Software Component Versions.
    57  func AbapAddonAssemblyKitCheckCVsCommand() *cobra.Command {
    58  	const STEP_NAME = "abapAddonAssemblyKitCheckCVs"
    59  
    60  	metadata := abapAddonAssemblyKitCheckCVsMetadata()
    61  	var stepConfig abapAddonAssemblyKitCheckCVsOptions
    62  	var startTime time.Time
    63  	var commonPipelineEnvironment abapAddonAssemblyKitCheckCVsCommonPipelineEnvironment
    64  	var logCollector *log.CollectorHook
    65  	var splunkClient *splunk.Splunk
    66  	telemetryClient := &telemetry.Telemetry{}
    67  
    68  	var createAbapAddonAssemblyKitCheckCVsCmd = &cobra.Command{
    69  		Use:   STEP_NAME,
    70  		Short: "This step checks the validity of ABAP Software Component Versions.",
    71  		Long: `This steps takes the list of ABAP Software Component Versions(repositories) from the addonDescriptor configuration file specified via addonDescriptorFileName (e.g. addon.yml) and checks by calling AAKaaS whether they exist or are a valid successor of an existing Software Component Version.
    72  It resolves the dotted version string into version, support package 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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   106  			if err != nil {
   107  				return err
   108  			}
   109  			if err = validation.ValidateStruct(stepConfig); err != nil {
   110  				log.SetErrorCategory(log.ErrorConfiguration)
   111  				return err
   112  			}
   113  
   114  			return nil
   115  		},
   116  		Run: func(_ *cobra.Command, _ []string) {
   117  			stepTelemetryData := telemetry.CustomData{}
   118  			stepTelemetryData.ErrorCode = "1"
   119  			handler := func() {
   120  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   121  				config.RemoveVaultSecretFiles()
   122  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   123  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   124  				stepTelemetryData.PiperCommitHash = GitCommit
   125  				telemetryClient.SetData(&stepTelemetryData)
   126  				telemetryClient.Send()
   127  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   128  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   129  				}
   130  			}
   131  			log.DeferExitHandler(handler)
   132  			defer handler()
   133  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   134  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   135  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   136  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   137  					GeneralConfig.HookConfig.SplunkConfig.Token,
   138  					GeneralConfig.HookConfig.SplunkConfig.Index,
   139  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   140  			}
   141  			abapAddonAssemblyKitCheckCVs(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   142  			stepTelemetryData.ErrorCode = "0"
   143  			log.Entry().Info("SUCCESS")
   144  		},
   145  	}
   146  
   147  	addAbapAddonAssemblyKitCheckCVsFlags(createAbapAddonAssemblyKitCheckCVsCmd, &stepConfig)
   148  	return createAbapAddonAssemblyKitCheckCVsCmd
   149  }
   150  
   151  func addAbapAddonAssemblyKitCheckCVsFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitCheckCVsOptions) {
   152  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   153  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   154  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   155  	cmd.Flags().StringVar(&stepConfig.AddonDescriptorFileName, "addonDescriptorFileName", `addon.yml`, "File name of the YAML file which describes the Product Version and corresponding Software Component Versions")
   156  	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")
   157  
   158  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   159  	cmd.MarkFlagRequired("username")
   160  	cmd.MarkFlagRequired("password")
   161  	cmd.MarkFlagRequired("addonDescriptorFileName")
   162  }
   163  
   164  // retrieve step metadata
   165  func abapAddonAssemblyKitCheckCVsMetadata() config.StepData {
   166  	var theMetaData = config.StepData{
   167  		Metadata: config.StepMetadata{
   168  			Name:        "abapAddonAssemblyKitCheckCVs",
   169  			Aliases:     []config.Alias{},
   170  			Description: "This step checks the validity of ABAP Software Component Versions.",
   171  		},
   172  		Spec: config.StepSpec{
   173  			Inputs: config.StepInputs{
   174  				Secrets: []config.StepSecrets{
   175  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "CredentialsId stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"},
   176  				},
   177  				Parameters: []config.StepParameters{
   178  					{
   179  						Name:        "abapAddonAssemblyKitEndpoint",
   180  						ResourceRef: []config.ResourceReference{},
   181  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   182  						Type:        "string",
   183  						Mandatory:   true,
   184  						Aliases:     []config.Alias{},
   185  						Default:     `https://apps.support.sap.com`,
   186  					},
   187  					{
   188  						Name:        "username",
   189  						ResourceRef: []config.ResourceReference{},
   190  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   191  						Type:        "string",
   192  						Mandatory:   true,
   193  						Aliases:     []config.Alias{},
   194  						Default:     os.Getenv("PIPER_username"),
   195  					},
   196  					{
   197  						Name:        "password",
   198  						ResourceRef: []config.ResourceReference{},
   199  						Scope:       []string{"PARAMETERS"},
   200  						Type:        "string",
   201  						Mandatory:   true,
   202  						Aliases:     []config.Alias{},
   203  						Default:     os.Getenv("PIPER_password"),
   204  					},
   205  					{
   206  						Name:        "addonDescriptorFileName",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   209  						Type:        "string",
   210  						Mandatory:   true,
   211  						Aliases:     []config.Alias{},
   212  						Default:     `addon.yml`,
   213  					},
   214  					{
   215  						Name: "addonDescriptor",
   216  						ResourceRef: []config.ResourceReference{
   217  							{
   218  								Name:  "commonPipelineEnvironment",
   219  								Param: "abap/addonDescriptor",
   220  							},
   221  						},
   222  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   223  						Type:      "string",
   224  						Mandatory: false,
   225  						Aliases:   []config.Alias{},
   226  						Default:   os.Getenv("PIPER_addonDescriptor"),
   227  					},
   228  				},
   229  			},
   230  			Outputs: config.StepOutputs{
   231  				Resources: []config.StepResources{
   232  					{
   233  						Name: "commonPipelineEnvironment",
   234  						Type: "piperEnvironment",
   235  						Parameters: []map[string]interface{}{
   236  							{"name": "abap/addonDescriptor"},
   237  						},
   238  					},
   239  				},
   240  			},
   241  		},
   242  	}
   243  	return theMetaData
   244  }