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