github.com/xgoffin/jenkins-library@v1.154.0/cmd/integrationArtifactTriggerIntegrationTest_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 integrationArtifactTriggerIntegrationTestOptions struct {
    19  	IntegrationFlowServiceKey         string `json:"integrationFlowServiceKey,omitempty"`
    20  	IntegrationFlowID                 string `json:"integrationFlowId,omitempty"`
    21  	IntegrationFlowServiceEndpointURL string `json:"integrationFlowServiceEndpointUrl,omitempty"`
    22  	ContentType                       string `json:"contentType,omitempty"`
    23  	MessageBodyPath                   string `json:"messageBodyPath,omitempty"`
    24  }
    25  
    26  // IntegrationArtifactTriggerIntegrationTestCommand Test the service endpoint of your iFlow
    27  func IntegrationArtifactTriggerIntegrationTestCommand() *cobra.Command {
    28  	const STEP_NAME = "integrationArtifactTriggerIntegrationTest"
    29  
    30  	metadata := integrationArtifactTriggerIntegrationTestMetadata()
    31  	var stepConfig integrationArtifactTriggerIntegrationTestOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createIntegrationArtifactTriggerIntegrationTestCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Test the service endpoint of your iFlow",
    40  		Long:  `With this step you can test your intergration flow  exposed by SAP Cloud Platform Integration on a tenant using OData API.Learn more about the SAP Cloud Integration remote API for getting service endpoint of deployed integration 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.IntegrationFlowServiceKey)
    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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    71  			if err != nil {
    72  				return err
    73  			}
    74  			if err = validation.ValidateStruct(stepConfig); err != nil {
    75  				log.SetErrorCategory(log.ErrorConfiguration)
    76  				return err
    77  			}
    78  
    79  			return nil
    80  		},
    81  		Run: func(_ *cobra.Command, _ []string) {
    82  			stepTelemetryData := telemetry.CustomData{}
    83  			stepTelemetryData.ErrorCode = "1"
    84  			handler := func() {
    85  				config.RemoveVaultSecretFiles()
    86  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    87  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    88  				stepTelemetryData.PiperCommitHash = GitCommit
    89  				telemetryClient.SetData(&stepTelemetryData)
    90  				telemetryClient.Send()
    91  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    92  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    93  				}
    94  			}
    95  			log.DeferExitHandler(handler)
    96  			defer handler()
    97  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    98  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    99  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   100  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   101  					GeneralConfig.HookConfig.SplunkConfig.Token,
   102  					GeneralConfig.HookConfig.SplunkConfig.Index,
   103  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   104  			}
   105  			integrationArtifactTriggerIntegrationTest(stepConfig, &stepTelemetryData)
   106  			stepTelemetryData.ErrorCode = "0"
   107  			log.Entry().Info("SUCCESS")
   108  		},
   109  	}
   110  
   111  	addIntegrationArtifactTriggerIntegrationTestFlags(createIntegrationArtifactTriggerIntegrationTestCmd, &stepConfig)
   112  	return createIntegrationArtifactTriggerIntegrationTestCmd
   113  }
   114  
   115  func addIntegrationArtifactTriggerIntegrationTestFlags(cmd *cobra.Command, stepConfig *integrationArtifactTriggerIntegrationTestOptions) {
   116  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowServiceKey, "integrationFlowServiceKey", os.Getenv("PIPER_integrationFlowServiceKey"), "Service key JSON string to access the Process Integration Runtime service instance of plan 'integration-flow'")
   117  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   118  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowServiceEndpointURL, "integrationFlowServiceEndpointUrl", os.Getenv("PIPER_integrationFlowServiceEndpointUrl"), "Specifies the URL endpoint of the iFlow. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
   119  	cmd.Flags().StringVar(&stepConfig.ContentType, "contentType", os.Getenv("PIPER_contentType"), "Specifies the content type of the file defined in messageBodyPath e.g. application/json")
   120  	cmd.Flags().StringVar(&stepConfig.MessageBodyPath, "messageBodyPath", os.Getenv("PIPER_messageBodyPath"), "Speficfies the relative file path to the message body.")
   121  
   122  	cmd.MarkFlagRequired("integrationFlowServiceKey")
   123  	cmd.MarkFlagRequired("integrationFlowId")
   124  	cmd.MarkFlagRequired("integrationFlowServiceEndpointUrl")
   125  }
   126  
   127  // retrieve step metadata
   128  func integrationArtifactTriggerIntegrationTestMetadata() config.StepData {
   129  	var theMetaData = config.StepData{
   130  		Metadata: config.StepMetadata{
   131  			Name:        "integrationArtifactTriggerIntegrationTest",
   132  			Aliases:     []config.Alias{},
   133  			Description: "Test the service endpoint of your iFlow",
   134  		},
   135  		Spec: config.StepSpec{
   136  			Inputs: config.StepInputs{
   137  				Secrets: []config.StepSecrets{
   138  					{Name: "integrationFlowServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'integration-flow'", Type: "jenkins"},
   139  				},
   140  				Parameters: []config.StepParameters{
   141  					{
   142  						Name: "integrationFlowServiceKey",
   143  						ResourceRef: []config.ResourceReference{
   144  							{
   145  								Name:  "integrationFlowServiceKeyCredentialsId",
   146  								Param: "integrationFlowServiceKey",
   147  								Type:  "secret",
   148  							},
   149  						},
   150  						Scope:     []string{"PARAMETERS"},
   151  						Type:      "string",
   152  						Mandatory: true,
   153  						Aliases:   []config.Alias{},
   154  						Default:   os.Getenv("PIPER_integrationFlowServiceKey"),
   155  					},
   156  					{
   157  						Name:        "integrationFlowId",
   158  						ResourceRef: []config.ResourceReference{},
   159  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   160  						Type:        "string",
   161  						Mandatory:   true,
   162  						Aliases:     []config.Alias{},
   163  						Default:     os.Getenv("PIPER_integrationFlowId"),
   164  					},
   165  					{
   166  						Name: "integrationFlowServiceEndpointUrl",
   167  						ResourceRef: []config.ResourceReference{
   168  							{
   169  								Name:  "commonPipelineEnvironment",
   170  								Param: "custom/integrationFlowServiceEndpoint",
   171  							},
   172  						},
   173  						Scope:     []string{"PARAMETERS"},
   174  						Type:      "string",
   175  						Mandatory: true,
   176  						Aliases:   []config.Alias{},
   177  						Default:   os.Getenv("PIPER_integrationFlowServiceEndpointUrl"),
   178  					},
   179  					{
   180  						Name:        "contentType",
   181  						ResourceRef: []config.ResourceReference{},
   182  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   183  						Type:        "string",
   184  						Mandatory:   false,
   185  						Aliases:     []config.Alias{},
   186  						Default:     os.Getenv("PIPER_contentType"),
   187  					},
   188  					{
   189  						Name:        "messageBodyPath",
   190  						ResourceRef: []config.ResourceReference{},
   191  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   192  						Type:        "string",
   193  						Mandatory:   false,
   194  						Aliases:     []config.Alias{},
   195  						Default:     os.Getenv("PIPER_messageBodyPath"),
   196  					},
   197  				},
   198  			},
   199  		},
   200  	}
   201  	return theMetaData
   202  }