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