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