github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentAssemblePackages_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 abapEnvironmentAssemblePackagesOptions struct {
    21  	CfAPIEndpoint               string   `json:"cfApiEndpoint,omitempty"`
    22  	CfOrg                       string   `json:"cfOrg,omitempty"`
    23  	CfSpace                     string   `json:"cfSpace,omitempty"`
    24  	CfServiceInstance           string   `json:"cfServiceInstance,omitempty"`
    25  	CfServiceKeyName            string   `json:"cfServiceKeyName,omitempty"`
    26  	Host                        string   `json:"host,omitempty"`
    27  	Username                    string   `json:"username,omitempty"`
    28  	Password                    string   `json:"password,omitempty"`
    29  	AddonDescriptor             string   `json:"addonDescriptor,omitempty"`
    30  	MaxRuntimeInMinutes         int      `json:"maxRuntimeInMinutes,omitempty"`
    31  	PollIntervalsInMilliseconds int      `json:"pollIntervalsInMilliseconds,omitempty"`
    32  	CertificateNames            []string `json:"certificateNames,omitempty"`
    33  }
    34  
    35  type abapEnvironmentAssemblePackagesCommonPipelineEnvironment struct {
    36  	abap struct {
    37  		addonDescriptor string
    38  	}
    39  }
    40  
    41  func (p *abapEnvironmentAssemblePackagesCommonPipelineEnvironment) persist(path, resourceName string) {
    42  	content := []struct {
    43  		category string
    44  		name     string
    45  		value    interface{}
    46  	}{
    47  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    48  	}
    49  
    50  	errCount := 0
    51  	for _, param := range content {
    52  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    53  		if err != nil {
    54  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    55  			errCount++
    56  		}
    57  	}
    58  	if errCount > 0 {
    59  		log.Entry().Error("failed to persist Piper environment")
    60  	}
    61  }
    62  
    63  // AbapEnvironmentAssemblePackagesCommand Assembly of installation, support package or patch in SAP BTP ABAP Environment system
    64  func AbapEnvironmentAssemblePackagesCommand() *cobra.Command {
    65  	const STEP_NAME = "abapEnvironmentAssemblePackages"
    66  
    67  	metadata := abapEnvironmentAssemblePackagesMetadata()
    68  	var stepConfig abapEnvironmentAssemblePackagesOptions
    69  	var startTime time.Time
    70  	var commonPipelineEnvironment abapEnvironmentAssemblePackagesCommonPipelineEnvironment
    71  	var logCollector *log.CollectorHook
    72  	var splunkClient *splunk.Splunk
    73  	telemetryClient := &telemetry.Telemetry{}
    74  
    75  	var createAbapEnvironmentAssemblePackagesCmd = &cobra.Command{
    76  		Use:   STEP_NAME,
    77  		Short: "Assembly of installation, support package or patch in SAP BTP ABAP Environment system",
    78  		Long: `This step runs the assembly of a list of provided [installations, support packages or patches](https://help.sap.com/viewer/9043aa5d2f834ad385e1cdfdadc06b6f/LATEST/en-US/9a81f55473568c77e10000000a174cb4.html) in SAP Cloud
    79  Platform ABAP Environment system and saves the corresponding [SAR archive](https://launchpad.support.sap.com/#/notes/212876) to the filesystem.`,
    80  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    81  			startTime = time.Now()
    82  			log.SetStepName(STEP_NAME)
    83  			log.SetVerbose(GeneralConfig.Verbose)
    84  
    85  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    86  
    87  			path, _ := os.Getwd()
    88  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    89  			log.RegisterHook(fatalHook)
    90  
    91  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    92  			if err != nil {
    93  				log.SetErrorCategory(log.ErrorConfiguration)
    94  				return err
    95  			}
    96  			log.RegisterSecret(stepConfig.Username)
    97  			log.RegisterSecret(stepConfig.Password)
    98  
    99  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
   100  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
   101  				log.RegisterHook(&sentryHook)
   102  			}
   103  
   104  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   105  				splunkClient = &splunk.Splunk{}
   106  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   107  				log.RegisterHook(logCollector)
   108  			}
   109  
   110  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   111  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   112  			}
   113  
   114  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   115  			if err != nil {
   116  				return err
   117  			}
   118  			if err = validation.ValidateStruct(stepConfig); err != nil {
   119  				log.SetErrorCategory(log.ErrorConfiguration)
   120  				return err
   121  			}
   122  
   123  			return nil
   124  		},
   125  		Run: func(_ *cobra.Command, _ []string) {
   126  			stepTelemetryData := telemetry.CustomData{}
   127  			stepTelemetryData.ErrorCode = "1"
   128  			handler := func() {
   129  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   130  				config.RemoveVaultSecretFiles()
   131  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   132  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   133  				stepTelemetryData.PiperCommitHash = GitCommit
   134  				telemetryClient.SetData(&stepTelemetryData)
   135  				telemetryClient.Send()
   136  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   137  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   138  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   139  						GeneralConfig.HookConfig.SplunkConfig.Token,
   140  						GeneralConfig.HookConfig.SplunkConfig.Index,
   141  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   142  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   143  				}
   144  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   145  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   146  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   147  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   148  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   149  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   150  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   151  				}
   152  			}
   153  			log.DeferExitHandler(handler)
   154  			defer handler()
   155  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   156  			abapEnvironmentAssemblePackages(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   157  			stepTelemetryData.ErrorCode = "0"
   158  			log.Entry().Info("SUCCESS")
   159  		},
   160  	}
   161  
   162  	addAbapEnvironmentAssemblePackagesFlags(createAbapEnvironmentAssemblePackagesCmd, &stepConfig)
   163  	return createAbapEnvironmentAssemblePackagesCmd
   164  }
   165  
   166  func addAbapEnvironmentAssemblePackagesFlags(cmd *cobra.Command, stepConfig *abapEnvironmentAssemblePackagesOptions) {
   167  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   168  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization")
   169  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry target space")
   170  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Cloud Foundry Service Instance")
   171  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Cloud Foundry Service Key")
   172  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP ABAP Environment system")
   173  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0582")
   174  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0582")
   175  	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")
   176  	cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 360, "maximal runtime of the step in minutes")
   177  	cmd.Flags().IntVar(&stepConfig.PollIntervalsInMilliseconds, "pollIntervalsInMilliseconds", 60000, "wait time in milliseconds till next status request in the backend system")
   178  	cmd.Flags().StringSliceVar(&stepConfig.CertificateNames, "certificateNames", []string{}, "file names of trusted (self-signed) server certificates - need to be stored in .pipeline/trustStore")
   179  
   180  	cmd.MarkFlagRequired("username")
   181  	cmd.MarkFlagRequired("password")
   182  	cmd.MarkFlagRequired("addonDescriptor")
   183  	cmd.MarkFlagRequired("maxRuntimeInMinutes")
   184  	cmd.MarkFlagRequired("pollIntervalsInMilliseconds")
   185  }
   186  
   187  // retrieve step metadata
   188  func abapEnvironmentAssemblePackagesMetadata() config.StepData {
   189  	var theMetaData = config.StepData{
   190  		Metadata: config.StepMetadata{
   191  			Name:        "abapEnvironmentAssemblePackages",
   192  			Aliases:     []config.Alias{},
   193  			Description: "Assembly of installation, support package or patch in SAP BTP ABAP Environment system",
   194  		},
   195  		Spec: config.StepSpec{
   196  			Inputs: config.StepInputs{
   197  				Secrets: []config.StepSecrets{
   198  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the Cloud Platform ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}, {Name: "credentialsId", Deprecated: false}}},
   199  				},
   200  				Parameters: []config.StepParameters{
   201  					{
   202  						Name:        "cfApiEndpoint",
   203  						ResourceRef: []config.ResourceReference{},
   204  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   205  						Type:        "string",
   206  						Mandatory:   false,
   207  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   208  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   209  					},
   210  					{
   211  						Name:        "cfOrg",
   212  						ResourceRef: []config.ResourceReference{},
   213  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   214  						Type:        "string",
   215  						Mandatory:   false,
   216  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   217  						Default:     os.Getenv("PIPER_cfOrg"),
   218  					},
   219  					{
   220  						Name:        "cfSpace",
   221  						ResourceRef: []config.ResourceReference{},
   222  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   223  						Type:        "string",
   224  						Mandatory:   false,
   225  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   226  						Default:     os.Getenv("PIPER_cfSpace"),
   227  					},
   228  					{
   229  						Name:        "cfServiceInstance",
   230  						ResourceRef: []config.ResourceReference{},
   231  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   232  						Type:        "string",
   233  						Mandatory:   false,
   234  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   235  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   236  					},
   237  					{
   238  						Name:        "cfServiceKeyName",
   239  						ResourceRef: []config.ResourceReference{},
   240  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   241  						Type:        "string",
   242  						Mandatory:   false,
   243  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   244  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   245  					},
   246  					{
   247  						Name:        "host",
   248  						ResourceRef: []config.ResourceReference{},
   249  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   250  						Type:        "string",
   251  						Mandatory:   false,
   252  						Aliases:     []config.Alias{},
   253  						Default:     os.Getenv("PIPER_host"),
   254  					},
   255  					{
   256  						Name:        "username",
   257  						ResourceRef: []config.ResourceReference{},
   258  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   259  						Type:        "string",
   260  						Mandatory:   true,
   261  						Aliases:     []config.Alias{},
   262  						Default:     os.Getenv("PIPER_username"),
   263  					},
   264  					{
   265  						Name:        "password",
   266  						ResourceRef: []config.ResourceReference{},
   267  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   268  						Type:        "string",
   269  						Mandatory:   true,
   270  						Aliases:     []config.Alias{},
   271  						Default:     os.Getenv("PIPER_password"),
   272  					},
   273  					{
   274  						Name: "addonDescriptor",
   275  						ResourceRef: []config.ResourceReference{
   276  							{
   277  								Name:  "commonPipelineEnvironment",
   278  								Param: "abap/addonDescriptor",
   279  							},
   280  						},
   281  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   282  						Type:      "string",
   283  						Mandatory: true,
   284  						Aliases:   []config.Alias{},
   285  						Default:   os.Getenv("PIPER_addonDescriptor"),
   286  					},
   287  					{
   288  						Name:        "maxRuntimeInMinutes",
   289  						ResourceRef: []config.ResourceReference{},
   290  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   291  						Type:        "int",
   292  						Mandatory:   true,
   293  						Aliases:     []config.Alias{},
   294  						Default:     360,
   295  					},
   296  					{
   297  						Name:        "pollIntervalsInMilliseconds",
   298  						ResourceRef: []config.ResourceReference{},
   299  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   300  						Type:        "int",
   301  						Mandatory:   true,
   302  						Aliases:     []config.Alias{},
   303  						Default:     60000,
   304  					},
   305  					{
   306  						Name:        "certificateNames",
   307  						ResourceRef: []config.ResourceReference{},
   308  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   309  						Type:        "[]string",
   310  						Mandatory:   false,
   311  						Aliases:     []config.Alias{},
   312  						Default:     []string{},
   313  					},
   314  				},
   315  			},
   316  			Containers: []config.Container{
   317  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   318  			},
   319  			Outputs: config.StepOutputs{
   320  				Resources: []config.StepResources{
   321  					{
   322  						Name: "commonPipelineEnvironment",
   323  						Type: "piperEnvironment",
   324  						Parameters: []map[string]interface{}{
   325  							{"name": "abap/addonDescriptor"},
   326  						},
   327  					},
   328  				},
   329  			},
   330  		},
   331  	}
   332  	return theMetaData
   333  }