github.com/jaylevin/jenkins-library@v1.230.4/cmd/integrationArtifactDownload_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 integrationArtifactDownloadOptions struct {
    19  	APIServiceKey          string `json:"apiServiceKey,omitempty"`
    20  	IntegrationFlowID      string `json:"integrationFlowId,omitempty"`
    21  	IntegrationFlowVersion string `json:"integrationFlowVersion,omitempty"`
    22  	DownloadPath           string `json:"downloadPath,omitempty"`
    23  }
    24  
    25  // IntegrationArtifactDownloadCommand Download integration flow runtime artefact
    26  func IntegrationArtifactDownloadCommand() *cobra.Command {
    27  	const STEP_NAME = "integrationArtifactDownload"
    28  
    29  	metadata := integrationArtifactDownloadMetadata()
    30  	var stepConfig integrationArtifactDownloadOptions
    31  	var startTime time.Time
    32  	var logCollector *log.CollectorHook
    33  	var splunkClient *splunk.Splunk
    34  	telemetryClient := &telemetry.Telemetry{}
    35  
    36  	var createIntegrationArtifactDownloadCmd = &cobra.Command{
    37  		Use:   STEP_NAME,
    38  		Short: "Download integration flow runtime artefact",
    39  		Long:  `With this step you can download a integration flow runtime artifact, which returns a zip file with the integration flow contents in to current workspace using the OData API. Learn more about the SAP Cloud Integration remote API for downloading an 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 {
    64  				splunkClient = &splunk.Splunk{}
    65  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    66  				log.RegisterHook(logCollector)
    67  			}
    68  
    69  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    70  			if err != nil {
    71  				return err
    72  			}
    73  			if err = validation.ValidateStruct(stepConfig); err != nil {
    74  				log.SetErrorCategory(log.ErrorConfiguration)
    75  				return err
    76  			}
    77  
    78  			return nil
    79  		},
    80  		Run: func(_ *cobra.Command, _ []string) {
    81  			stepTelemetryData := telemetry.CustomData{}
    82  			stepTelemetryData.ErrorCode = "1"
    83  			handler := func() {
    84  				config.RemoveVaultSecretFiles()
    85  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    86  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    87  				stepTelemetryData.PiperCommitHash = GitCommit
    88  				telemetryClient.SetData(&stepTelemetryData)
    89  				telemetryClient.Send()
    90  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    91  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    92  				}
    93  			}
    94  			log.DeferExitHandler(handler)
    95  			defer handler()
    96  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    97  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    98  				splunkClient.Initialize(GeneralConfig.CorrelationID,
    99  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   100  					GeneralConfig.HookConfig.SplunkConfig.Token,
   101  					GeneralConfig.HookConfig.SplunkConfig.Index,
   102  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   103  			}
   104  			integrationArtifactDownload(stepConfig, &stepTelemetryData)
   105  			stepTelemetryData.ErrorCode = "0"
   106  			log.Entry().Info("SUCCESS")
   107  		},
   108  	}
   109  
   110  	addIntegrationArtifactDownloadFlags(createIntegrationArtifactDownloadCmd, &stepConfig)
   111  	return createIntegrationArtifactDownloadCmd
   112  }
   113  
   114  func addIntegrationArtifactDownloadFlags(cmd *cobra.Command, stepConfig *integrationArtifactDownloadOptions) {
   115  	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'")
   116  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   117  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowVersion, "integrationFlowVersion", os.Getenv("PIPER_integrationFlowVersion"), "Specifies the version of the Integration Flow artifact")
   118  	cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies integration artifact download location.")
   119  
   120  	cmd.MarkFlagRequired("apiServiceKey")
   121  	cmd.MarkFlagRequired("integrationFlowId")
   122  	cmd.MarkFlagRequired("integrationFlowVersion")
   123  	cmd.MarkFlagRequired("downloadPath")
   124  }
   125  
   126  // retrieve step metadata
   127  func integrationArtifactDownloadMetadata() config.StepData {
   128  	var theMetaData = config.StepData{
   129  		Metadata: config.StepMetadata{
   130  			Name:        "integrationArtifactDownload",
   131  			Aliases:     []config.Alias{},
   132  			Description: "Download integration flow runtime artefact",
   133  		},
   134  		Spec: config.StepSpec{
   135  			Inputs: config.StepInputs{
   136  				Secrets: []config.StepSecrets{
   137  					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"},
   138  				},
   139  				Parameters: []config.StepParameters{
   140  					{
   141  						Name: "apiServiceKey",
   142  						ResourceRef: []config.ResourceReference{
   143  							{
   144  								Name:  "cpiApiServiceKeyCredentialsId",
   145  								Param: "apiServiceKey",
   146  								Type:  "secret",
   147  							},
   148  						},
   149  						Scope:     []string{"PARAMETERS"},
   150  						Type:      "string",
   151  						Mandatory: true,
   152  						Aliases:   []config.Alias{},
   153  						Default:   os.Getenv("PIPER_apiServiceKey"),
   154  					},
   155  					{
   156  						Name:        "integrationFlowId",
   157  						ResourceRef: []config.ResourceReference{},
   158  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   159  						Type:        "string",
   160  						Mandatory:   true,
   161  						Aliases:     []config.Alias{},
   162  						Default:     os.Getenv("PIPER_integrationFlowId"),
   163  					},
   164  					{
   165  						Name:        "integrationFlowVersion",
   166  						ResourceRef: []config.ResourceReference{},
   167  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   168  						Type:        "string",
   169  						Mandatory:   true,
   170  						Aliases:     []config.Alias{},
   171  						Default:     os.Getenv("PIPER_integrationFlowVersion"),
   172  					},
   173  					{
   174  						Name:        "downloadPath",
   175  						ResourceRef: []config.ResourceReference{},
   176  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   177  						Type:        "string",
   178  						Mandatory:   true,
   179  						Aliases:     []config.Alias{},
   180  						Default:     os.Getenv("PIPER_downloadPath"),
   181  					},
   182  				},
   183  			},
   184  		},
   185  	}
   186  	return theMetaData
   187  }