github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapAddonAssemblyKitReleasePackages_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 abapAddonAssemblyKitReleasePackagesOptions struct {
    21  	AbapAddonAssemblyKitEndpoint string `json:"abapAddonAssemblyKitEndpoint,omitempty"`
    22  	Username                     string `json:"username,omitempty"`
    23  	Password                     string `json:"password,omitempty"`
    24  	AddonDescriptor              string `json:"addonDescriptor,omitempty"`
    25  	MaxRuntimeInMinutes          int    `json:"maxRuntimeInMinutes,omitempty"`
    26  	PollingIntervalInSeconds     int    `json:"pollingIntervalInSeconds,omitempty"`
    27  }
    28  
    29  type abapAddonAssemblyKitReleasePackagesCommonPipelineEnvironment struct {
    30  	abap struct {
    31  		addonDescriptor string
    32  	}
    33  }
    34  
    35  func (p *abapAddonAssemblyKitReleasePackagesCommonPipelineEnvironment) persist(path, resourceName string) {
    36  	content := []struct {
    37  		category string
    38  		name     string
    39  		value    interface{}
    40  	}{
    41  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    42  	}
    43  
    44  	errCount := 0
    45  	for _, param := range content {
    46  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    47  		if err != nil {
    48  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    49  			errCount++
    50  		}
    51  	}
    52  	if errCount > 0 {
    53  		log.Entry().Error("failed to persist Piper environment")
    54  	}
    55  }
    56  
    57  // AbapAddonAssemblyKitReleasePackagesCommand This step releases the physical Delivery Packages
    58  func AbapAddonAssemblyKitReleasePackagesCommand() *cobra.Command {
    59  	const STEP_NAME = "abapAddonAssemblyKitReleasePackages"
    60  
    61  	metadata := abapAddonAssemblyKitReleasePackagesMetadata()
    62  	var stepConfig abapAddonAssemblyKitReleasePackagesOptions
    63  	var startTime time.Time
    64  	var commonPipelineEnvironment abapAddonAssemblyKitReleasePackagesCommonPipelineEnvironment
    65  	var logCollector *log.CollectorHook
    66  	var splunkClient *splunk.Splunk
    67  	telemetryClient := &telemetry.Telemetry{}
    68  
    69  	var createAbapAddonAssemblyKitReleasePackagesCmd = &cobra.Command{
    70  		Use:   STEP_NAME,
    71  		Short: "This step releases the physical Delivery Packages",
    72  		Long: `This step takes the list of Software Component Versions from the addonDescriptor in the commonPipelineEnvironment.
    73  The physical Delivery Packages in status ā€œLā€ are released. The new status "R"eleased is written back to the addonDescriptor in the commonPipelineEnvironment.
    74  <br />
    75  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    76  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    77  			startTime = time.Now()
    78  			log.SetStepName(STEP_NAME)
    79  			log.SetVerbose(GeneralConfig.Verbose)
    80  
    81  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    82  
    83  			path, _ := os.Getwd()
    84  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    85  			log.RegisterHook(fatalHook)
    86  
    87  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    88  			if err != nil {
    89  				log.SetErrorCategory(log.ErrorConfiguration)
    90  				return err
    91  			}
    92  
    93  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    94  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    95  				log.RegisterHook(&sentryHook)
    96  			}
    97  
    98  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    99  				splunkClient = &splunk.Splunk{}
   100  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   101  				log.RegisterHook(logCollector)
   102  			}
   103  
   104  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   105  			if err != nil {
   106  				return err
   107  			}
   108  			if err = validation.ValidateStruct(stepConfig); err != nil {
   109  				log.SetErrorCategory(log.ErrorConfiguration)
   110  				return err
   111  			}
   112  
   113  			return nil
   114  		},
   115  		Run: func(_ *cobra.Command, _ []string) {
   116  			stepTelemetryData := telemetry.CustomData{}
   117  			stepTelemetryData.ErrorCode = "1"
   118  			handler := func() {
   119  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   120  				config.RemoveVaultSecretFiles()
   121  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   122  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   123  				stepTelemetryData.PiperCommitHash = GitCommit
   124  				telemetryClient.SetData(&stepTelemetryData)
   125  				telemetryClient.Send()
   126  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   127  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   128  				}
   129  			}
   130  			log.DeferExitHandler(handler)
   131  			defer handler()
   132  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   133  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   134  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   135  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   136  					GeneralConfig.HookConfig.SplunkConfig.Token,
   137  					GeneralConfig.HookConfig.SplunkConfig.Index,
   138  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   139  			}
   140  			abapAddonAssemblyKitReleasePackages(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   141  			stepTelemetryData.ErrorCode = "0"
   142  			log.Entry().Info("SUCCESS")
   143  		},
   144  	}
   145  
   146  	addAbapAddonAssemblyKitReleasePackagesFlags(createAbapAddonAssemblyKitReleasePackagesCmd, &stepConfig)
   147  	return createAbapAddonAssemblyKitReleasePackagesCmd
   148  }
   149  
   150  func addAbapAddonAssemblyKitReleasePackagesFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitReleasePackagesOptions) {
   151  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   152  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   153  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   154  	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")
   155  	cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 5, "Maximum runtime for status polling in minutes")
   156  	cmd.Flags().IntVar(&stepConfig.PollingIntervalInSeconds, "pollingIntervalInSeconds", 30, "Wait time in seconds between polling calls")
   157  
   158  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   159  	cmd.MarkFlagRequired("username")
   160  	cmd.MarkFlagRequired("password")
   161  	cmd.MarkFlagRequired("addonDescriptor")
   162  }
   163  
   164  // retrieve step metadata
   165  func abapAddonAssemblyKitReleasePackagesMetadata() config.StepData {
   166  	var theMetaData = config.StepData{
   167  		Metadata: config.StepMetadata{
   168  			Name:        "abapAddonAssemblyKitReleasePackages",
   169  			Aliases:     []config.Alias{},
   170  			Description: "This step releases the physical Delivery Packages",
   171  		},
   172  		Spec: config.StepSpec{
   173  			Inputs: config.StepInputs{
   174  				Secrets: []config.StepSecrets{
   175  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential 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: "addonDescriptor",
   207  						ResourceRef: []config.ResourceReference{
   208  							{
   209  								Name:  "commonPipelineEnvironment",
   210  								Param: "abap/addonDescriptor",
   211  							},
   212  						},
   213  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   214  						Type:      "string",
   215  						Mandatory: true,
   216  						Aliases:   []config.Alias{},
   217  						Default:   os.Getenv("PIPER_addonDescriptor"),
   218  					},
   219  					{
   220  						Name:        "maxRuntimeInMinutes",
   221  						ResourceRef: []config.ResourceReference{},
   222  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   223  						Type:        "int",
   224  						Mandatory:   false,
   225  						Aliases:     []config.Alias{},
   226  						Default:     5,
   227  					},
   228  					{
   229  						Name:        "pollingIntervalInSeconds",
   230  						ResourceRef: []config.ResourceReference{},
   231  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   232  						Type:        "int",
   233  						Mandatory:   false,
   234  						Aliases:     []config.Alias{},
   235  						Default:     30,
   236  					},
   237  				},
   238  			},
   239  			Outputs: config.StepOutputs{
   240  				Resources: []config.StepResources{
   241  					{
   242  						Name: "commonPipelineEnvironment",
   243  						Type: "piperEnvironment",
   244  						Parameters: []map[string]interface{}{
   245  							{"name": "abap/addonDescriptor"},
   246  						},
   247  					},
   248  				},
   249  			},
   250  		},
   251  	}
   252  	return theMetaData
   253  }