github.com/jaylevin/jenkins-library@v1.230.4/cmd/integrationArtifactUnDeploy_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 integrationArtifactUnDeployOptions struct {
    19  	APIServiceKey     string `json:"apiServiceKey,omitempty"`
    20  	IntegrationFlowID string `json:"integrationFlowId,omitempty"`
    21  }
    22  
    23  // IntegrationArtifactUnDeployCommand Undeploy a integration flow
    24  func IntegrationArtifactUnDeployCommand() *cobra.Command {
    25  	const STEP_NAME = "integrationArtifactUnDeploy"
    26  
    27  	metadata := integrationArtifactUnDeployMetadata()
    28  	var stepConfig integrationArtifactUnDeployOptions
    29  	var startTime time.Time
    30  	var logCollector *log.CollectorHook
    31  	var splunkClient *splunk.Splunk
    32  	telemetryClient := &telemetry.Telemetry{}
    33  
    34  	var createIntegrationArtifactUnDeployCmd = &cobra.Command{
    35  		Use:   STEP_NAME,
    36  		Short: "Undeploy a integration flow",
    37  		Long:  `With this step you can undeploy a integration flow artifact in SAP Cloud integration runtime using OData API. Learn more about the SAP Cloud Integration remote API for undeploying an integration artifact [here](https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/d1679a80543f46509a7329243b595bdb.html)`,
    38  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    39  			startTime = time.Now()
    40  			log.SetStepName(STEP_NAME)
    41  			log.SetVerbose(GeneralConfig.Verbose)
    42  
    43  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    44  
    45  			path, _ := os.Getwd()
    46  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    47  			log.RegisterHook(fatalHook)
    48  
    49  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    50  			if err != nil {
    51  				log.SetErrorCategory(log.ErrorConfiguration)
    52  				return err
    53  			}
    54  			log.RegisterSecret(stepConfig.APIServiceKey)
    55  
    56  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    57  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    58  				log.RegisterHook(&sentryHook)
    59  			}
    60  
    61  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    62  				splunkClient = &splunk.Splunk{}
    63  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    64  				log.RegisterHook(logCollector)
    65  			}
    66  
    67  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    68  			if err != nil {
    69  				return err
    70  			}
    71  			if err = validation.ValidateStruct(stepConfig); err != nil {
    72  				log.SetErrorCategory(log.ErrorConfiguration)
    73  				return err
    74  			}
    75  
    76  			return nil
    77  		},
    78  		Run: func(_ *cobra.Command, _ []string) {
    79  			stepTelemetryData := telemetry.CustomData{}
    80  			stepTelemetryData.ErrorCode = "1"
    81  			handler := func() {
    82  				config.RemoveVaultSecretFiles()
    83  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    84  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    85  				stepTelemetryData.PiperCommitHash = GitCommit
    86  				telemetryClient.SetData(&stepTelemetryData)
    87  				telemetryClient.Send()
    88  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    89  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    90  				}
    91  			}
    92  			log.DeferExitHandler(handler)
    93  			defer handler()
    94  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    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  			}
   102  			integrationArtifactUnDeploy(stepConfig, &stepTelemetryData)
   103  			stepTelemetryData.ErrorCode = "0"
   104  			log.Entry().Info("SUCCESS")
   105  		},
   106  	}
   107  
   108  	addIntegrationArtifactUnDeployFlags(createIntegrationArtifactUnDeployCmd, &stepConfig)
   109  	return createIntegrationArtifactUnDeployCmd
   110  }
   111  
   112  func addIntegrationArtifactUnDeployFlags(cmd *cobra.Command, stepConfig *integrationArtifactUnDeployOptions) {
   113  	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'")
   114  	cmd.Flags().StringVar(&stepConfig.IntegrationFlowID, "integrationFlowId", os.Getenv("PIPER_integrationFlowId"), "Specifies the ID of the Integration Flow artifact")
   115  
   116  	cmd.MarkFlagRequired("apiServiceKey")
   117  	cmd.MarkFlagRequired("integrationFlowId")
   118  }
   119  
   120  // retrieve step metadata
   121  func integrationArtifactUnDeployMetadata() config.StepData {
   122  	var theMetaData = config.StepData{
   123  		Metadata: config.StepMetadata{
   124  			Name:        "integrationArtifactUnDeploy",
   125  			Aliases:     []config.Alias{},
   126  			Description: "Undeploy a integration flow",
   127  		},
   128  		Spec: config.StepSpec{
   129  			Inputs: config.StepInputs{
   130  				Secrets: []config.StepSecrets{
   131  					{Name: "cpiApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the Process Integration Runtime service instance of plan 'api'", Type: "jenkins"},
   132  				},
   133  				Parameters: []config.StepParameters{
   134  					{
   135  						Name: "apiServiceKey",
   136  						ResourceRef: []config.ResourceReference{
   137  							{
   138  								Name:  "cpiApiServiceKeyCredentialsId",
   139  								Param: "apiServiceKey",
   140  								Type:  "secret",
   141  							},
   142  						},
   143  						Scope:     []string{"PARAMETERS"},
   144  						Type:      "string",
   145  						Mandatory: true,
   146  						Aliases:   []config.Alias{},
   147  						Default:   os.Getenv("PIPER_apiServiceKey"),
   148  					},
   149  					{
   150  						Name:        "integrationFlowId",
   151  						ResourceRef: []config.ResourceReference{},
   152  						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"},
   153  						Type:        "string",
   154  						Mandatory:   true,
   155  						Aliases:     []config.Alias{},
   156  						Default:     os.Getenv("PIPER_integrationFlowId"),
   157  					},
   158  				},
   159  			},
   160  		},
   161  	}
   162  	return theMetaData
   163  }