github.com/SAP/jenkins-library@v1.362.0/cmd/integrationArtifactTransport_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 integrationArtifactTransportOptions struct {
    19  	CasServiceKey        string `json:"casServiceKey,omitempty"`
    20  	IntegrationPackageID string `json:"integrationPackageId,omitempty"`
    21  	ResourceID           string `json:"resourceID,omitempty"`
    22  	Name                 string `json:"name,omitempty"`
    23  	Version              string `json:"version,omitempty"`
    24  }
    25  
    26  // IntegrationArtifactTransportCommand Integration Package transport using the SAP Content Agent Service
    27  func IntegrationArtifactTransportCommand() *cobra.Command {
    28  	const STEP_NAME = "integrationArtifactTransport"
    29  
    30  	metadata := integrationArtifactTransportMetadata()
    31  	var stepConfig integrationArtifactTransportOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createIntegrationArtifactTransportCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Integration Package transport using the SAP Content Agent Service",
    40  		Long:  `With this step you can trigger an Integration Package transport from SAP Integration Suite using SAP Content Agent Service and SAP Cloud Transport Management Service. For more information about doing an Integration Package transport using SAP Content Agent Service see the documentation [here](https://help.sap.com/docs/CONTENT_AGENT_SERVICE/ae1a4f2d150d468d9ff56e13f9898e07/8e274fdd41da45a69ff919c0af8c6127.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.CasServiceKey)
    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 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    65  				splunkClient = &splunk.Splunk{}
    66  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    67  				log.RegisterHook(logCollector)
    68  			}
    69  
    70  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    71  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    72  			}
    73  
    74  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    75  			if err != nil {
    76  				return err
    77  			}
    78  			if err = validation.ValidateStruct(stepConfig); err != nil {
    79  				log.SetErrorCategory(log.ErrorConfiguration)
    80  				return err
    81  			}
    82  
    83  			return nil
    84  		},
    85  		Run: func(_ *cobra.Command, _ []string) {
    86  			stepTelemetryData := telemetry.CustomData{}
    87  			stepTelemetryData.ErrorCode = "1"
    88  			handler := func() {
    89  				config.RemoveVaultSecretFiles()
    90  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    91  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    92  				stepTelemetryData.PiperCommitHash = GitCommit
    93  				telemetryClient.SetData(&stepTelemetryData)
    94  				telemetryClient.Send()
    95  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    96  					splunkClient.Initialize(GeneralConfig.CorrelationID,
    97  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
    98  						GeneralConfig.HookConfig.SplunkConfig.Token,
    99  						GeneralConfig.HookConfig.SplunkConfig.Index,
   100  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   101  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   102  				}
   103  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   104  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   106  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   107  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   108  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   109  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   110  				}
   111  			}
   112  			log.DeferExitHandler(handler)
   113  			defer handler()
   114  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   115  			integrationArtifactTransport(stepConfig, &stepTelemetryData)
   116  			stepTelemetryData.ErrorCode = "0"
   117  			log.Entry().Info("SUCCESS")
   118  		},
   119  	}
   120  
   121  	addIntegrationArtifactTransportFlags(createIntegrationArtifactTransportCmd, &stepConfig)
   122  	return createIntegrationArtifactTransportCmd
   123  }
   124  
   125  func addIntegrationArtifactTransportFlags(cmd *cobra.Command, stepConfig *integrationArtifactTransportOptions) {
   126  	cmd.Flags().StringVar(&stepConfig.CasServiceKey, "casServiceKey", os.Getenv("PIPER_casServiceKey"), "Service key JSON string to access the CAS service instance")
   127  	cmd.Flags().StringVar(&stepConfig.IntegrationPackageID, "integrationPackageId", os.Getenv("PIPER_integrationPackageId"), "Specifies the ID of the integration package artifact.")
   128  	cmd.Flags().StringVar(&stepConfig.ResourceID, "resourceID", os.Getenv("PIPER_resourceID"), "Specifies the technical ID of the integration package artifact.")
   129  	cmd.Flags().StringVar(&stepConfig.Name, "name", os.Getenv("PIPER_name"), "Specifies the name of the integration package artifact.")
   130  	cmd.Flags().StringVar(&stepConfig.Version, "version", os.Getenv("PIPER_version"), "Specifies the version of the Integration Package artifact.")
   131  
   132  	cmd.MarkFlagRequired("casServiceKey")
   133  	cmd.MarkFlagRequired("integrationPackageId")
   134  	cmd.MarkFlagRequired("resourceID")
   135  	cmd.MarkFlagRequired("name")
   136  	cmd.MarkFlagRequired("version")
   137  }
   138  
   139  // retrieve step metadata
   140  func integrationArtifactTransportMetadata() config.StepData {
   141  	var theMetaData = config.StepData{
   142  		Metadata: config.StepMetadata{
   143  			Name:        "integrationArtifactTransport",
   144  			Aliases:     []config.Alias{},
   145  			Description: "Integration Package transport using the SAP Content Agent Service",
   146  		},
   147  		Spec: config.StepSpec{
   148  			Inputs: config.StepInputs{
   149  				Secrets: []config.StepSecrets{
   150  					{Name: "casApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the CAS service instance", Type: "jenkins"},
   151  				},
   152  				Parameters: []config.StepParameters{
   153  					{
   154  						Name: "casServiceKey",
   155  						ResourceRef: []config.ResourceReference{
   156  							{
   157  								Name:  "casApiServiceKeyCredentialsId",
   158  								Param: "casServiceKey",
   159  								Type:  "secret",
   160  							},
   161  						},
   162  						Scope:     []string{"PARAMETERS"},
   163  						Type:      "string",
   164  						Mandatory: true,
   165  						Aliases:   []config.Alias{},
   166  						Default:   os.Getenv("PIPER_casServiceKey"),
   167  					},
   168  					{
   169  						Name:        "integrationPackageId",
   170  						ResourceRef: []config.ResourceReference{},
   171  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   172  						Type:        "string",
   173  						Mandatory:   true,
   174  						Aliases:     []config.Alias{},
   175  						Default:     os.Getenv("PIPER_integrationPackageId"),
   176  					},
   177  					{
   178  						Name:        "resourceID",
   179  						ResourceRef: []config.ResourceReference{},
   180  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   181  						Type:        "string",
   182  						Mandatory:   true,
   183  						Aliases:     []config.Alias{},
   184  						Default:     os.Getenv("PIPER_resourceID"),
   185  					},
   186  					{
   187  						Name:        "name",
   188  						ResourceRef: []config.ResourceReference{},
   189  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   190  						Type:        "string",
   191  						Mandatory:   true,
   192  						Aliases:     []config.Alias{},
   193  						Default:     os.Getenv("PIPER_name"),
   194  					},
   195  					{
   196  						Name:        "version",
   197  						ResourceRef: []config.ResourceReference{},
   198  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   199  						Type:        "string",
   200  						Mandatory:   true,
   201  						Aliases:     []config.Alias{},
   202  						Default:     os.Getenv("PIPER_version"),
   203  					},
   204  				},
   205  			},
   206  		},
   207  	}
   208  	return theMetaData
   209  }