github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/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 BTP 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 BTP ABAP Environment system",
    77  		Long:  `This step confirms the assemblies of provided [installations, support packages or patches] in SAP BTP 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  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   109  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   110  			}
   111  
   112  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   113  			if err != nil {
   114  				return err
   115  			}
   116  			if err = validation.ValidateStruct(stepConfig); err != nil {
   117  				log.SetErrorCategory(log.ErrorConfiguration)
   118  				return err
   119  			}
   120  
   121  			return nil
   122  		},
   123  		Run: func(_ *cobra.Command, _ []string) {
   124  			stepTelemetryData := telemetry.CustomData{}
   125  			stepTelemetryData.ErrorCode = "1"
   126  			handler := func() {
   127  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   128  				config.RemoveVaultSecretFiles()
   129  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   130  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   131  				stepTelemetryData.PiperCommitHash = GitCommit
   132  				telemetryClient.SetData(&stepTelemetryData)
   133  				telemetryClient.Send()
   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  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   141  				}
   142  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   143  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   144  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   145  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   146  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   147  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   148  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   149  				}
   150  			}
   151  			log.DeferExitHandler(handler)
   152  			defer handler()
   153  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   154  			abapEnvironmentAssembleConfirm(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   155  			stepTelemetryData.ErrorCode = "0"
   156  			log.Entry().Info("SUCCESS")
   157  		},
   158  	}
   159  
   160  	addAbapEnvironmentAssembleConfirmFlags(createAbapEnvironmentAssembleConfirmCmd, &stepConfig)
   161  	return createAbapEnvironmentAssembleConfirmCmd
   162  }
   163  
   164  func addAbapEnvironmentAssembleConfirmFlags(cmd *cobra.Command, stepConfig *abapEnvironmentAssembleConfirmOptions) {
   165  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   166  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization")
   167  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry target space")
   168  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Cloud Foundry Service Instance")
   169  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Cloud Foundry Service Key")
   170  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP ABAP Environment system")
   171  	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")
   172  	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")
   173  	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")
   174  	cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 360, "maximal runtime of the step")
   175  	cmd.Flags().StringSliceVar(&stepConfig.CertificateNames, "certificateNames", []string{}, "certificates for the backend system, this certificates needs to be stored in .pipeline/trustStore")
   176  
   177  	cmd.MarkFlagRequired("username")
   178  	cmd.MarkFlagRequired("password")
   179  	cmd.MarkFlagRequired("addonDescriptor")
   180  	cmd.MarkFlagRequired("maxRuntimeInMinutes")
   181  }
   182  
   183  // retrieve step metadata
   184  func abapEnvironmentAssembleConfirmMetadata() config.StepData {
   185  	var theMetaData = config.StepData{
   186  		Metadata: config.StepMetadata{
   187  			Name:        "abapEnvironmentAssembleConfirm",
   188  			Aliases:     []config.Alias{},
   189  			Description: "Confirm the Delivery of Assembly for installation, support package or patch in SAP BTP ABAP Environment system",
   190  		},
   191  		Spec: config.StepSpec{
   192  			Inputs: config.StepInputs{
   193  				Secrets: []config.StepSecrets{
   194  					{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}}},
   195  				},
   196  				Parameters: []config.StepParameters{
   197  					{
   198  						Name:        "cfApiEndpoint",
   199  						ResourceRef: []config.ResourceReference{},
   200  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   201  						Type:        "string",
   202  						Mandatory:   false,
   203  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   204  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   205  					},
   206  					{
   207  						Name:        "cfOrg",
   208  						ResourceRef: []config.ResourceReference{},
   209  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   210  						Type:        "string",
   211  						Mandatory:   false,
   212  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   213  						Default:     os.Getenv("PIPER_cfOrg"),
   214  					},
   215  					{
   216  						Name:        "cfSpace",
   217  						ResourceRef: []config.ResourceReference{},
   218  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   219  						Type:        "string",
   220  						Mandatory:   false,
   221  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   222  						Default:     os.Getenv("PIPER_cfSpace"),
   223  					},
   224  					{
   225  						Name:        "cfServiceInstance",
   226  						ResourceRef: []config.ResourceReference{},
   227  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   228  						Type:        "string",
   229  						Mandatory:   false,
   230  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   231  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   232  					},
   233  					{
   234  						Name:        "cfServiceKeyName",
   235  						ResourceRef: []config.ResourceReference{},
   236  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   237  						Type:        "string",
   238  						Mandatory:   false,
   239  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   240  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   241  					},
   242  					{
   243  						Name:        "host",
   244  						ResourceRef: []config.ResourceReference{},
   245  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   246  						Type:        "string",
   247  						Mandatory:   false,
   248  						Aliases:     []config.Alias{},
   249  						Default:     os.Getenv("PIPER_host"),
   250  					},
   251  					{
   252  						Name:        "username",
   253  						ResourceRef: []config.ResourceReference{},
   254  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   255  						Type:        "string",
   256  						Mandatory:   true,
   257  						Aliases:     []config.Alias{},
   258  						Default:     os.Getenv("PIPER_username"),
   259  					},
   260  					{
   261  						Name:        "password",
   262  						ResourceRef: []config.ResourceReference{},
   263  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   264  						Type:        "string",
   265  						Mandatory:   true,
   266  						Aliases:     []config.Alias{},
   267  						Default:     os.Getenv("PIPER_password"),
   268  					},
   269  					{
   270  						Name: "addonDescriptor",
   271  						ResourceRef: []config.ResourceReference{
   272  							{
   273  								Name:  "commonPipelineEnvironment",
   274  								Param: "abap/addonDescriptor",
   275  							},
   276  						},
   277  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   278  						Type:      "string",
   279  						Mandatory: true,
   280  						Aliases:   []config.Alias{},
   281  						Default:   os.Getenv("PIPER_addonDescriptor"),
   282  					},
   283  					{
   284  						Name:        "maxRuntimeInMinutes",
   285  						ResourceRef: []config.ResourceReference{},
   286  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   287  						Type:        "int",
   288  						Mandatory:   true,
   289  						Aliases:     []config.Alias{},
   290  						Default:     360,
   291  					},
   292  					{
   293  						Name:        "certificateNames",
   294  						ResourceRef: []config.ResourceReference{},
   295  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   296  						Type:        "[]string",
   297  						Mandatory:   false,
   298  						Aliases:     []config.Alias{},
   299  						Default:     []string{},
   300  					},
   301  				},
   302  			},
   303  			Containers: []config.Container{
   304  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   305  			},
   306  			Outputs: config.StepOutputs{
   307  				Resources: []config.StepResources{
   308  					{
   309  						Name: "commonPipelineEnvironment",
   310  						Type: "piperEnvironment",
   311  						Parameters: []map[string]interface{}{
   312  							{"name": "abap/addonDescriptor"},
   313  						},
   314  					},
   315  				},
   316  			},
   317  		},
   318  	}
   319  	return theMetaData
   320  }