github.com/xgoffin/jenkins-library@v1.154.0/cmd/integrationArtifactGetMplStatus_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  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/SAP/jenkins-library/pkg/config"
    12  	"github.com/SAP/jenkins-library/pkg/log"
    13  	"github.com/SAP/jenkins-library/pkg/piperenv"
    14  	"github.com/SAP/jenkins-library/pkg/splunk"
    15  	"github.com/SAP/jenkins-library/pkg/telemetry"
    16  	"github.com/SAP/jenkins-library/pkg/validation"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  type integrationArtifactGetMplStatusOptions struct {
    21  	APIServiceKey     string `json:"apiServiceKey,omitempty"`
    22  	IntegrationFlowID string `json:"integrationFlowId,omitempty"`
    23  }
    24  
    25  type integrationArtifactGetMplStatusCommonPipelineEnvironment struct {
    26  	custom struct {
    27  		integrationFlowMplStatus string
    28  		integrationFlowMplError  string
    29  	}
    30  }
    31  
    32  func (p *integrationArtifactGetMplStatusCommonPipelineEnvironment) persist(path, resourceName string) {
    33  	content := []struct {
    34  		category string
    35  		name     string
    36  		value    interface{}
    37  	}{
    38  		{category: "custom", name: "integrationFlowMplStatus", value: p.custom.integrationFlowMplStatus},
    39  		{category: "custom", name: "integrationFlowMplError", value: p.custom.integrationFlowMplError},
    40  	}
    41  
    42  	errCount := 0
    43  	for _, param := range content {
    44  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    45  		if err != nil {
    46  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    47  			errCount++
    48  		}
    49  	}
    50  	if errCount > 0 {
    51  		log.Entry().Error("failed to persist Piper environment")
    52  	}
    53  }
    54  
    55  // IntegrationArtifactGetMplStatusCommand Get the MPL status of an integration flow
    56  func IntegrationArtifactGetMplStatusCommand() *cobra.Command {
    57  	const STEP_NAME = "integrationArtifactGetMplStatus"
    58  
    59  	metadata := integrationArtifactGetMplStatusMetadata()
    60  	var stepConfig integrationArtifactGetMplStatusOptions
    61  	var startTime time.Time
    62  	var commonPipelineEnvironment integrationArtifactGetMplStatusCommonPipelineEnvironment
    63  	var logCollector *log.CollectorHook
    64  	var splunkClient *splunk.Splunk
    65  	telemetryClient := &telemetry.Telemetry{}
    66  
    67  	var createIntegrationArtifactGetMplStatusCmd = &cobra.Command{
    68  		Use:   STEP_NAME,
    69  		Short: "Get the MPL status of an integration flow",
    70  		Long:  `With this step you can obtain information about the Message Processing Log (MPL) status of integration flow using OData API. Learn more about the SAP Cloud Integration remote API for getting MPL status messages processed of an deployed integration artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html).`,
    71  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    72  			startTime = time.Now()
    73  			log.SetStepName(STEP_NAME)
    74  			log.SetVerbose(GeneralConfig.Verbose)
    75  
    76  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    77  
    78  			path, _ := os.Getwd()
    79  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    80  			log.RegisterHook(fatalHook)
    81  
    82  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    83  			if err != nil {
    84  				log.SetErrorCategory(log.ErrorConfiguration)
    85  				return err
    86  			}
    87  			log.RegisterSecret(stepConfig.APIServiceKey)
    88  
    89  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    90  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    91  				log.RegisterHook(&sentryHook)
    92  			}
    93  
    94  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  				splunkClient = &splunk.Splunk{}
    96  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    97  				log.RegisterHook(logCollector)
    98  			}
    99  
   100  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   101  			if err != nil {
   102  				return err
   103  			}
   104  			if err = validation.ValidateStruct(stepConfig); err != nil {
   105  				log.SetErrorCategory(log.ErrorConfiguration)
   106  				return err
   107  			}
   108  
   109  			return nil
   110  		},
   111  		Run: func(_ *cobra.Command, _ []string) {
   112  			stepTelemetryData := telemetry.CustomData{}
   113  			stepTelemetryData.ErrorCode = "1"
   114  			handler := func() {
   115  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   116  				config.RemoveVaultSecretFiles()
   117  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   118  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   119  				stepTelemetryData.PiperCommitHash = GitCommit
   120  				telemetryClient.SetData(&stepTelemetryData)
   121  				telemetryClient.Send()
   122  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   123  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   124  				}
   125  			}
   126  			log.DeferExitHandler(handler)
   127  			defer handler()
   128  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   129  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   130  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   131  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   132  					GeneralConfig.HookConfig.SplunkConfig.Token,
   133  					GeneralConfig.HookConfig.SplunkConfig.Index,
   134  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   135  			}
   136  			integrationArtifactGetMplStatus(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   137  			stepTelemetryData.ErrorCode = "0"
   138  			log.Entry().Info("SUCCESS")
   139  		},
   140  	}
   141  
   142  	addIntegrationArtifactGetMplStatusFlags(createIntegrationArtifactGetMplStatusCmd, &stepConfig)
   143  	return createIntegrationArtifactGetMplStatusCmd
   144  }
   145  
   146  func addIntegrationArtifactGetMplStatusFlags(cmd *cobra.Command, stepConfig *integrationArtifactGetMplStatusOptions) {
   147  	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'")
   148  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   149  
   150  	cmd.MarkFlagRequired("apiServiceKey")
   151  	cmd.MarkFlagRequired("integrationFlowId")
   152  }
   153  
   154  // retrieve step metadata
   155  func integrationArtifactGetMplStatusMetadata() config.StepData {
   156  	var theMetaData = config.StepData{
   157  		Metadata: config.StepMetadata{
   158  			Name:        "integrationArtifactGetMplStatus",
   159  			Aliases:     []config.Alias{},
   160  			Description: "Get the MPL status of an integration flow",
   161  		},
   162  		Spec: config.StepSpec{
   163  			Inputs: config.StepInputs{
   164  				Secrets: []config.StepSecrets{
   165  					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"},
   166  				},
   167  				Parameters: []config.StepParameters{
   168  					{
   169  						Name: "apiServiceKey",
   170  						ResourceRef: []config.ResourceReference{
   171  							{
   172  								Name:  "cpiApiServiceKeyCredentialsId",
   173  								Param: "apiServiceKey",
   174  								Type:  "secret",
   175  							},
   176  						},
   177  						Scope:     []string{"PARAMETERS"},
   178  						Type:      "string",
   179  						Mandatory: true,
   180  						Aliases:   []config.Alias{},
   181  						Default:   os.Getenv("PIPER_apiServiceKey"),
   182  					},
   183  					{
   184  						Name:        "integrationFlowId",
   185  						ResourceRef: []config.ResourceReference{},
   186  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   187  						Type:        "string",
   188  						Mandatory:   true,
   189  						Aliases:     []config.Alias{},
   190  						Default:     os.Getenv("PIPER_integrationFlowId"),
   191  					},
   192  				},
   193  			},
   194  			Outputs: config.StepOutputs{
   195  				Resources: []config.StepResources{
   196  					{
   197  						Name: "commonPipelineEnvironment",
   198  						Type: "piperEnvironment",
   199  						Parameters: []map[string]interface{}{
   200  							{"name": "custom/integrationFlowMplStatus"},
   201  							{"name": "custom/integrationFlowMplError"},
   202  						},
   203  					},
   204  				},
   205  			},
   206  		},
   207  	}
   208  	return theMetaData
   209  }