github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/integrationArtifactUpload_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 integrationArtifactUploadOptions struct {
    19  	APIServiceKey       string `json:"apiServiceKey,omitempty"`
    20  	IntegrationFlowID   string `json:"integrationFlowId,omitempty"`
    21  	IntegrationFlowName string `json:"integrationFlowName,omitempty"`
    22  	PackageID           string `json:"packageId,omitempty"`
    23  	FilePath            string `json:"filePath,omitempty"`
    24  }
    25  
    26  // IntegrationArtifactUploadCommand Upload or Update an integration flow designtime artifact
    27  func IntegrationArtifactUploadCommand() *cobra.Command {
    28  	const STEP_NAME = "integrationArtifactUpload"
    29  
    30  	metadata := integrationArtifactUploadMetadata()
    31  	var stepConfig integrationArtifactUploadOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createIntegrationArtifactUploadCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Upload or Update an integration flow designtime artifact",
    40  		Long:  `With this step you can either upload or update a integration flow designtime artifact using the OData API. Learn more about the SAP Cloud Integration remote API for updating an integration flow artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html).`,
    41  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    42  			startTime = time.Now()
    43  			log.SetStepName(STEP_NAME)
    44  			log.SetVerbose(GeneralConfig.Verbose)
    45  
    46  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    47  
    48  			path, _ := os.Getwd()
    49  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    50  			log.RegisterHook(fatalHook)
    51  
    52  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    53  			if err != nil {
    54  				log.SetErrorCategory(log.ErrorConfiguration)
    55  				return err
    56  			}
    57  			log.RegisterSecret(stepConfig.APIServiceKey)
    58  
    59  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    60  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    61  				log.RegisterHook(&sentryHook)
    62  			}
    63  
    64  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    65  				splunkClient = &splunk.Splunk{}
    66  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    67  				log.RegisterHook(logCollector)
    68  			}
    69  
    70  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    71  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    72  			}
    73  
    74  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    75  			if err != nil {
    76  				return err
    77  			}
    78  			if err = validation.ValidateStruct(stepConfig); err != nil {
    79  				log.SetErrorCategory(log.ErrorConfiguration)
    80  				return err
    81  			}
    82  
    83  			return nil
    84  		},
    85  		Run: func(_ *cobra.Command, _ []string) {
    86  			stepTelemetryData := telemetry.CustomData{}
    87  			stepTelemetryData.ErrorCode = "1"
    88  			handler := func() {
    89  				config.RemoveVaultSecretFiles()
    90  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    91  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    92  				stepTelemetryData.PiperCommitHash = GitCommit
    93  				telemetryClient.SetData(&stepTelemetryData)
    94  				telemetryClient.Send()
    95  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    96  					splunkClient.Initialize(GeneralConfig.CorrelationID,
    97  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
    98  						GeneralConfig.HookConfig.SplunkConfig.Token,
    99  						GeneralConfig.HookConfig.SplunkConfig.Index,
   100  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   101  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   102  				}
   103  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   104  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   106  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   107  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   108  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   109  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   110  				}
   111  			}
   112  			log.DeferExitHandler(handler)
   113  			defer handler()
   114  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   115  			integrationArtifactUpload(stepConfig, &stepTelemetryData)
   116  			stepTelemetryData.ErrorCode = "0"
   117  			log.Entry().Info("SUCCESS")
   118  		},
   119  	}
   120  
   121  	addIntegrationArtifactUploadFlags(createIntegrationArtifactUploadCmd, &stepConfig)
   122  	return createIntegrationArtifactUploadCmd
   123  }
   124  
   125  func addIntegrationArtifactUploadFlags(cmd *cobra.Command, stepConfig *integrationArtifactUploadOptions) {
   126  	cmd.Flags().StringVar(&stepConfig.APIServiceKey, "apiServiceKey", os.Getenv("PIPER_apiServiceKey"), "Service key JSON string to access the Process Integration Runtime service instance of plan 'api'")
   127  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   128  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowName, "integrationFlowName", os.Getenv("PIPER_integrationFlowName"), "Specifies the Name of the Integration Flow artifact")
   129  	cmd.Flags().StringVar(&stepConfig.PackageID, "packageId", os.Getenv("PIPER_packageId"), "Specifies the ID of the Integration Package")
   130  	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Specifies integration artifact relative file path.")
   131  
   132  	cmd.MarkFlagRequired("apiServiceKey")
   133  	cmd.MarkFlagRequired("integrationFlowId")
   134  	cmd.MarkFlagRequired("integrationFlowName")
   135  	cmd.MarkFlagRequired("filePath")
   136  }
   137  
   138  // retrieve step metadata
   139  func integrationArtifactUploadMetadata() config.StepData {
   140  	var theMetaData = config.StepData{
   141  		Metadata: config.StepMetadata{
   142  			Name:        "integrationArtifactUpload",
   143  			Aliases:     []config.Alias{},
   144  			Description: "Upload or Update an integration flow designtime artifact",
   145  		},
   146  		Spec: config.StepSpec{
   147  			Inputs: config.StepInputs{
   148  				Secrets: []config.StepSecrets{
   149  					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"},
   150  				},
   151  				Parameters: []config.StepParameters{
   152  					{
   153  						Name: "apiServiceKey",
   154  						ResourceRef: []config.ResourceReference{
   155  							{
   156  								Name:  "cpiApiServiceKeyCredentialsId",
   157  								Param: "apiServiceKey",
   158  								Type:  "secret",
   159  							},
   160  						},
   161  						Scope:     []string{"PARAMETERS"},
   162  						Type:      "string",
   163  						Mandatory: true,
   164  						Aliases:   []config.Alias{},
   165  						Default:   os.Getenv("PIPER_apiServiceKey"),
   166  					},
   167  					{
   168  						Name:        "integrationFlowId",
   169  						ResourceRef: []config.ResourceReference{},
   170  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   171  						Type:        "string",
   172  						Mandatory:   true,
   173  						Aliases:     []config.Alias{},
   174  						Default:     os.Getenv("PIPER_integrationFlowId"),
   175  					},
   176  					{
   177  						Name:        "integrationFlowName",
   178  						ResourceRef: []config.ResourceReference{},
   179  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   180  						Type:        "string",
   181  						Mandatory:   true,
   182  						Aliases:     []config.Alias{},
   183  						Default:     os.Getenv("PIPER_integrationFlowName"),
   184  					},
   185  					{
   186  						Name:        "packageId",
   187  						ResourceRef: []config.ResourceReference{},
   188  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   189  						Type:        "string",
   190  						Mandatory:   false,
   191  						Aliases:     []config.Alias{},
   192  						Default:     os.Getenv("PIPER_packageId"),
   193  					},
   194  					{
   195  						Name:        "filePath",
   196  						ResourceRef: []config.ResourceReference{},
   197  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   198  						Type:        "string",
   199  						Mandatory:   true,
   200  						Aliases:     []config.Alias{},
   201  						Default:     os.Getenv("PIPER_filePath"),
   202  					},
   203  				},
   204  			},
   205  		},
   206  	}
   207  	return theMetaData
   208  }