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