github.com/jaylevin/jenkins-library@v1.230.4/cmd/transportRequestUploadSOLMAN_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 transportRequestUploadSOLMANOptions struct {
    21  	Endpoint           string   `json:"endpoint,omitempty"`
    22  	Username           string   `json:"username,omitempty"`
    23  	Password           string   `json:"password,omitempty"`
    24  	ApplicationID      string   `json:"applicationId,omitempty"`
    25  	ChangeDocumentID   string   `json:"changeDocumentId,omitempty"`
    26  	TransportRequestID string   `json:"transportRequestId,omitempty"`
    27  	FilePath           string   `json:"filePath,omitempty"`
    28  	CmClientOpts       []string `json:"cmClientOpts,omitempty"`
    29  }
    30  
    31  type transportRequestUploadSOLMANCommonPipelineEnvironment struct {
    32  	custom struct {
    33  		changeDocumentID   string
    34  		transportRequestID string
    35  	}
    36  }
    37  
    38  func (p *transportRequestUploadSOLMANCommonPipelineEnvironment) persist(path, resourceName string) {
    39  	content := []struct {
    40  		category string
    41  		name     string
    42  		value    interface{}
    43  	}{
    44  		{category: "custom", name: "changeDocumentId", value: p.custom.changeDocumentID},
    45  		{category: "custom", name: "transportRequestId", value: p.custom.transportRequestID},
    46  	}
    47  
    48  	errCount := 0
    49  	for _, param := range content {
    50  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    51  		if err != nil {
    52  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    53  			errCount++
    54  		}
    55  	}
    56  	if errCount > 0 {
    57  		log.Entry().Error("failed to persist Piper environment")
    58  	}
    59  }
    60  
    61  // TransportRequestUploadSOLMANCommand Uploads a specified file into a given transport via Solution Manager
    62  func TransportRequestUploadSOLMANCommand() *cobra.Command {
    63  	const STEP_NAME = "transportRequestUploadSOLMAN"
    64  
    65  	metadata := transportRequestUploadSOLMANMetadata()
    66  	var stepConfig transportRequestUploadSOLMANOptions
    67  	var startTime time.Time
    68  	var commonPipelineEnvironment transportRequestUploadSOLMANCommonPipelineEnvironment
    69  	var logCollector *log.CollectorHook
    70  	var splunkClient *splunk.Splunk
    71  	telemetryClient := &telemetry.Telemetry{}
    72  
    73  	var createTransportRequestUploadSOLMANCmd = &cobra.Command{
    74  		Use:   STEP_NAME,
    75  		Short: "Uploads a specified file into a given transport via Solution Manager",
    76  		Long: `Uploads the specified file into the given transport request via Solution Manager.
    77  The mandatory change document ID points to the associate change document item.
    78  The application ID specifies how the file needs to be handled on server side.`,
    79  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    80  			startTime = time.Now()
    81  			log.SetStepName(STEP_NAME)
    82  			log.SetVerbose(GeneralConfig.Verbose)
    83  
    84  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    85  
    86  			path, _ := os.Getwd()
    87  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    88  			log.RegisterHook(fatalHook)
    89  
    90  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    91  			if err != nil {
    92  				log.SetErrorCategory(log.ErrorConfiguration)
    93  				return err
    94  			}
    95  			log.RegisterSecret(stepConfig.Username)
    96  			log.RegisterSecret(stepConfig.Password)
    97  
    98  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    99  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
   100  				log.RegisterHook(&sentryHook)
   101  			}
   102  
   103  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   104  				splunkClient = &splunk.Splunk{}
   105  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   106  				log.RegisterHook(logCollector)
   107  			}
   108  
   109  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   110  			if err != nil {
   111  				return err
   112  			}
   113  			if err = validation.ValidateStruct(stepConfig); err != nil {
   114  				log.SetErrorCategory(log.ErrorConfiguration)
   115  				return err
   116  			}
   117  
   118  			return nil
   119  		},
   120  		Run: func(_ *cobra.Command, _ []string) {
   121  			stepTelemetryData := telemetry.CustomData{}
   122  			stepTelemetryData.ErrorCode = "1"
   123  			handler := func() {
   124  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   125  				config.RemoveVaultSecretFiles()
   126  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   127  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   128  				stepTelemetryData.PiperCommitHash = GitCommit
   129  				telemetryClient.SetData(&stepTelemetryData)
   130  				telemetryClient.Send()
   131  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   132  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   133  				}
   134  			}
   135  			log.DeferExitHandler(handler)
   136  			defer handler()
   137  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   138  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   139  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   140  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   141  					GeneralConfig.HookConfig.SplunkConfig.Token,
   142  					GeneralConfig.HookConfig.SplunkConfig.Index,
   143  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   144  			}
   145  			transportRequestUploadSOLMAN(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   146  			stepTelemetryData.ErrorCode = "0"
   147  			log.Entry().Info("SUCCESS")
   148  		},
   149  	}
   150  
   151  	addTransportRequestUploadSOLMANFlags(createTransportRequestUploadSOLMANCmd, &stepConfig)
   152  	return createTransportRequestUploadSOLMANCmd
   153  }
   154  
   155  func addTransportRequestUploadSOLMANFlags(cmd *cobra.Command, stepConfig *transportRequestUploadSOLMANOptions) {
   156  	cmd.Flags().StringVar(&stepConfig.Endpoint, "endpoint", os.Getenv("PIPER_endpoint"), "Service endpoint")
   157  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "Service user for uploading to the Solution Manager")
   158  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Service user password for uploading to the Solution Manager")
   159  	cmd.Flags().StringVar(&stepConfig.ApplicationID, "applicationId", os.Getenv("PIPER_applicationId"), "Id of the application. Specifies how the file needs to be handled on server side")
   160  	cmd.Flags().StringVar(&stepConfig.ChangeDocumentID, "changeDocumentId", os.Getenv("PIPER_changeDocumentId"), "ID of the change document to which the file is uploaded")
   161  	cmd.Flags().StringVar(&stepConfig.TransportRequestID, "transportRequestId", os.Getenv("PIPER_transportRequestId"), "ID of the transport request to which the file is uploaded")
   162  	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Name/Path of the file which should be uploaded")
   163  	cmd.Flags().StringSliceVar(&stepConfig.CmClientOpts, "cmClientOpts", []string{}, "Additional options handed over to the cm client")
   164  
   165  	cmd.MarkFlagRequired("endpoint")
   166  	cmd.MarkFlagRequired("username")
   167  	cmd.MarkFlagRequired("password")
   168  	cmd.MarkFlagRequired("applicationId")
   169  	cmd.MarkFlagRequired("changeDocumentId")
   170  	cmd.MarkFlagRequired("transportRequestId")
   171  	cmd.MarkFlagRequired("filePath")
   172  	cmd.MarkFlagRequired("cmClientOpts")
   173  }
   174  
   175  // retrieve step metadata
   176  func transportRequestUploadSOLMANMetadata() config.StepData {
   177  	var theMetaData = config.StepData{
   178  		Metadata: config.StepMetadata{
   179  			Name:        "transportRequestUploadSOLMAN",
   180  			Aliases:     []config.Alias{{Name: "transportRequestUploadFile", Deprecated: false}},
   181  			Description: "Uploads a specified file into a given transport via Solution Manager",
   182  		},
   183  		Spec: config.StepSpec{
   184  			Inputs: config.StepInputs{
   185  				Secrets: []config.StepSecrets{
   186  					{Name: "uploadCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate against the ABAP backend", Type: "jenkins", Aliases: []config.Alias{{Name: "changeManagement/credentialsId", Deprecated: false}}},
   187  				},
   188  				Parameters: []config.StepParameters{
   189  					{
   190  						Name:        "endpoint",
   191  						ResourceRef: []config.ResourceReference{},
   192  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   193  						Type:        "string",
   194  						Mandatory:   true,
   195  						Aliases:     []config.Alias{{Name: "changeManagement/endpoint"}},
   196  						Default:     os.Getenv("PIPER_endpoint"),
   197  					},
   198  					{
   199  						Name: "username",
   200  						ResourceRef: []config.ResourceReference{
   201  							{
   202  								Name:  "uploadCredentialsId",
   203  								Param: "username",
   204  								Type:  "secret",
   205  							},
   206  						},
   207  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   208  						Type:      "string",
   209  						Mandatory: true,
   210  						Aliases:   []config.Alias{},
   211  						Default:   os.Getenv("PIPER_username"),
   212  					},
   213  					{
   214  						Name: "password",
   215  						ResourceRef: []config.ResourceReference{
   216  							{
   217  								Name:  "uploadCredentialsId",
   218  								Param: "password",
   219  								Type:  "secret",
   220  							},
   221  						},
   222  						Scope:     []string{"PARAMETERS"},
   223  						Type:      "string",
   224  						Mandatory: true,
   225  						Aliases:   []config.Alias{},
   226  						Default:   os.Getenv("PIPER_password"),
   227  					},
   228  					{
   229  						Name:        "applicationId",
   230  						ResourceRef: []config.ResourceReference{},
   231  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   232  						Type:        "string",
   233  						Mandatory:   true,
   234  						Aliases:     []config.Alias{},
   235  						Default:     os.Getenv("PIPER_applicationId"),
   236  					},
   237  					{
   238  						Name: "changeDocumentId",
   239  						ResourceRef: []config.ResourceReference{
   240  							{
   241  								Name:  "commonPipelineEnvironment",
   242  								Param: "custom/changeDocumentId",
   243  							},
   244  						},
   245  						Scope:     []string{"PARAMETERS"},
   246  						Type:      "string",
   247  						Mandatory: true,
   248  						Aliases:   []config.Alias{},
   249  						Default:   os.Getenv("PIPER_changeDocumentId"),
   250  					},
   251  					{
   252  						Name: "transportRequestId",
   253  						ResourceRef: []config.ResourceReference{
   254  							{
   255  								Name:  "commonPipelineEnvironment",
   256  								Param: "custom/transportRequestId",
   257  							},
   258  						},
   259  						Scope:     []string{"PARAMETERS"},
   260  						Type:      "string",
   261  						Mandatory: true,
   262  						Aliases:   []config.Alias{},
   263  						Default:   os.Getenv("PIPER_transportRequestId"),
   264  					},
   265  					{
   266  						Name: "filePath",
   267  						ResourceRef: []config.ResourceReference{
   268  							{
   269  								Name:  "commonPipelineEnvironment",
   270  								Param: "mtarFilePath",
   271  							},
   272  						},
   273  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   274  						Type:      "string",
   275  						Mandatory: true,
   276  						Aliases:   []config.Alias{},
   277  						Default:   os.Getenv("PIPER_filePath"),
   278  					},
   279  					{
   280  						Name:        "cmClientOpts",
   281  						ResourceRef: []config.ResourceReference{},
   282  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   283  						Type:        "[]string",
   284  						Mandatory:   true,
   285  						Aliases:     []config.Alias{{Name: "clientOpts"}, {Name: "changeManagement/clientOpts"}},
   286  						Default:     []string{},
   287  					},
   288  				},
   289  			},
   290  			Containers: []config.Container{
   291  				{Name: "cmclient", Image: "ppiper/cm-client:3.0.0.0"},
   292  			},
   293  			Outputs: config.StepOutputs{
   294  				Resources: []config.StepResources{
   295  					{
   296  						Name: "commonPipelineEnvironment",
   297  						Type: "piperEnvironment",
   298  						Parameters: []map[string]interface{}{
   299  							{"name": "custom/changeDocumentId"},
   300  							{"name": "custom/transportRequestId"},
   301  						},
   302  					},
   303  				},
   304  			},
   305  		},
   306  	}
   307  	return theMetaData
   308  }