github.com/SAP/jenkins-library@v1.362.0/cmd/awsS3Upload_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  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type awsS3UploadOptions struct {
    19  	JSONCredentialsAWS string `json:"jsonCredentialsAWS,omitempty"`
    20  	FilePath           string `json:"filePath,omitempty"`
    21  }
    22  
    23  // AwsS3UploadCommand Uploads a specified file or directory into a given AWS S3 Bucket
    24  func AwsS3UploadCommand() *cobra.Command {
    25  	const STEP_NAME = "awsS3Upload"
    26  
    27  	metadata := awsS3UploadMetadata()
    28  	var stepConfig awsS3UploadOptions
    29  	var startTime time.Time
    30  	var logCollector *log.CollectorHook
    31  	var splunkClient *splunk.Splunk
    32  	telemetryClient := &telemetry.Telemetry{}
    33  
    34  	var createAwsS3UploadCmd = &cobra.Command{
    35  		Use:   STEP_NAME,
    36  		Short: "Uploads a specified file or directory into a given AWS S3 Bucket",
    37  		Long: `Uploads a specified file or directory as S3 Objects into a given AWS S3 Bucket.
    38  In case a file is uploaded that is already contained in the S3 bucket, it will be overwritten with the latest version.`,
    39  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    40  			startTime = time.Now()
    41  			log.SetStepName(STEP_NAME)
    42  			log.SetVerbose(GeneralConfig.Verbose)
    43  
    44  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    45  
    46  			path, _ := os.Getwd()
    47  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    48  			log.RegisterHook(fatalHook)
    49  
    50  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    51  			if err != nil {
    52  				log.SetErrorCategory(log.ErrorConfiguration)
    53  				return err
    54  			}
    55  			log.RegisterSecret(stepConfig.JSONCredentialsAWS)
    56  
    57  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    58  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    59  				log.RegisterHook(&sentryHook)
    60  			}
    61  
    62  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    63  				splunkClient = &splunk.Splunk{}
    64  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    65  				log.RegisterHook(logCollector)
    66  			}
    67  
    68  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    69  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    70  			}
    71  
    72  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    73  			if err != nil {
    74  				return err
    75  			}
    76  			if err = validation.ValidateStruct(stepConfig); err != nil {
    77  				log.SetErrorCategory(log.ErrorConfiguration)
    78  				return err
    79  			}
    80  
    81  			return nil
    82  		},
    83  		Run: func(_ *cobra.Command, _ []string) {
    84  			stepTelemetryData := telemetry.CustomData{}
    85  			stepTelemetryData.ErrorCode = "1"
    86  			handler := func() {
    87  				config.RemoveVaultSecretFiles()
    88  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    89  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    90  				stepTelemetryData.PiperCommitHash = GitCommit
    91  				telemetryClient.SetData(&stepTelemetryData)
    92  				telemetryClient.Send()
    93  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    94  					splunkClient.Initialize(GeneralConfig.CorrelationID,
    95  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
    96  						GeneralConfig.HookConfig.SplunkConfig.Token,
    97  						GeneralConfig.HookConfig.SplunkConfig.Index,
    98  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
    99  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   100  				}
   101  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   102  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   104  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   106  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   108  				}
   109  			}
   110  			log.DeferExitHandler(handler)
   111  			defer handler()
   112  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   113  			awsS3Upload(stepConfig, &stepTelemetryData)
   114  			stepTelemetryData.ErrorCode = "0"
   115  			log.Entry().Info("SUCCESS")
   116  		},
   117  	}
   118  
   119  	addAwsS3UploadFlags(createAwsS3UploadCmd, &stepConfig)
   120  	return createAwsS3UploadCmd
   121  }
   122  
   123  func addAwsS3UploadFlags(cmd *cobra.Command, stepConfig *awsS3UploadOptions) {
   124  	cmd.Flags().StringVar(&stepConfig.JSONCredentialsAWS, "jsonCredentialsAWS", os.Getenv("PIPER_jsonCredentialsAWS"), "JSON String Credentials to access AWS S3 Bucket")
   125  	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Name/Path of the file which should be uploaded")
   126  
   127  	cmd.MarkFlagRequired("jsonCredentialsAWS")
   128  	cmd.MarkFlagRequired("filePath")
   129  }
   130  
   131  // retrieve step metadata
   132  func awsS3UploadMetadata() config.StepData {
   133  	var theMetaData = config.StepData{
   134  		Metadata: config.StepMetadata{
   135  			Name:        "awsS3Upload",
   136  			Aliases:     []config.Alias{},
   137  			Description: "Uploads a specified file or directory into a given AWS S3 Bucket",
   138  		},
   139  		Spec: config.StepSpec{
   140  			Inputs: config.StepInputs{
   141  				Secrets: []config.StepSecrets{
   142  					{Name: "awsCredentialsId", Description: "Jenkins 'Secret Text' credentials ID containing the JSON file to authenticate to the AWS S3 Bucket", Type: "jenkins"},
   143  				},
   144  				Parameters: []config.StepParameters{
   145  					{
   146  						Name: "jsonCredentialsAWS",
   147  						ResourceRef: []config.ResourceReference{
   148  							{
   149  								Name: "awsCredentialsId",
   150  								Type: "secret",
   151  							},
   152  						},
   153  						Scope:     []string{"PARAMETERS"},
   154  						Type:      "string",
   155  						Mandatory: true,
   156  						Aliases:   []config.Alias{},
   157  						Default:   os.Getenv("PIPER_jsonCredentialsAWS"),
   158  					},
   159  					{
   160  						Name: "filePath",
   161  						ResourceRef: []config.ResourceReference{
   162  							{
   163  								Name:  "commonPipelineEnvironment",
   164  								Param: "mtarFilePath",
   165  							},
   166  						},
   167  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   168  						Type:      "string",
   169  						Mandatory: true,
   170  						Aliases:   []config.Alias{},
   171  						Default:   os.Getenv("PIPER_filePath"),
   172  					},
   173  				},
   174  			},
   175  		},
   176  	}
   177  	return theMetaData
   178  }