github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentCheckoutBranch_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 abapEnvironmentCheckoutBranchOptions struct {
    19  	Username          string   `json:"username,omitempty"`
    20  	Password          string   `json:"password,omitempty"`
    21  	RepositoryName    string   `json:"repositoryName,omitempty"`
    22  	BranchName        string   `json:"branchName,omitempty"`
    23  	Host              string   `json:"host,omitempty"`
    24  	Repositories      string   `json:"repositories,omitempty"`
    25  	CfAPIEndpoint     string   `json:"cfApiEndpoint,omitempty"`
    26  	CfOrg             string   `json:"cfOrg,omitempty"`
    27  	CfSpace           string   `json:"cfSpace,omitempty"`
    28  	CfServiceInstance string   `json:"cfServiceInstance,omitempty"`
    29  	CfServiceKeyName  string   `json:"cfServiceKeyName,omitempty"`
    30  	CertificateNames  []string `json:"certificateNames,omitempty"`
    31  }
    32  
    33  // AbapEnvironmentCheckoutBranchCommand Switches between branches of a git repository on a SAP BTP ABAP Environment system
    34  func AbapEnvironmentCheckoutBranchCommand() *cobra.Command {
    35  	const STEP_NAME = "abapEnvironmentCheckoutBranch"
    36  
    37  	metadata := abapEnvironmentCheckoutBranchMetadata()
    38  	var stepConfig abapEnvironmentCheckoutBranchOptions
    39  	var startTime time.Time
    40  	var logCollector *log.CollectorHook
    41  	var splunkClient *splunk.Splunk
    42  	telemetryClient := &telemetry.Telemetry{}
    43  
    44  	var createAbapEnvironmentCheckoutBranchCmd = &cobra.Command{
    45  		Use:   STEP_NAME,
    46  		Short: "Switches between branches of a git repository on a SAP BTP ABAP Environment system",
    47  		Long: `This step switches between branches of a git repository (Software Component) on a SAP BTP ABAP Environment system.
    48  Please provide either of the following options:
    49  
    50  * The host and credentials the BTP ABAP Environment system itself. The credentials must be configured for the Communication Scenario [SAP_COM_0510](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/b04a9ae412894725a2fc539bfb1ca055.html).
    51  * The Cloud Foundry parameters (API endpoint, organization, space), credentials, the service instance for the ABAP service and the service key for the Communication Scenario SAP_COM_0510.
    52  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.`,
    53  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    54  			startTime = time.Now()
    55  			log.SetStepName(STEP_NAME)
    56  			log.SetVerbose(GeneralConfig.Verbose)
    57  
    58  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    59  
    60  			path, _ := os.Getwd()
    61  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    62  			log.RegisterHook(fatalHook)
    63  
    64  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    65  			if err != nil {
    66  				log.SetErrorCategory(log.ErrorConfiguration)
    67  				return err
    68  			}
    69  			log.RegisterSecret(stepConfig.Username)
    70  			log.RegisterSecret(stepConfig.Password)
    71  
    72  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    73  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    74  				log.RegisterHook(&sentryHook)
    75  			}
    76  
    77  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    78  				splunkClient = &splunk.Splunk{}
    79  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    80  				log.RegisterHook(logCollector)
    81  			}
    82  
    83  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    84  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    85  			}
    86  
    87  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    88  			if err != nil {
    89  				return err
    90  			}
    91  			if err = validation.ValidateStruct(stepConfig); err != nil {
    92  				log.SetErrorCategory(log.ErrorConfiguration)
    93  				return err
    94  			}
    95  
    96  			return nil
    97  		},
    98  		Run: func(_ *cobra.Command, _ []string) {
    99  			stepTelemetryData := telemetry.CustomData{}
   100  			stepTelemetryData.ErrorCode = "1"
   101  			handler := func() {
   102  				config.RemoveVaultSecretFiles()
   103  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   104  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   105  				stepTelemetryData.PiperCommitHash = GitCommit
   106  				telemetryClient.SetData(&stepTelemetryData)
   107  				telemetryClient.Send()
   108  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   109  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   110  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   111  						GeneralConfig.HookConfig.SplunkConfig.Token,
   112  						GeneralConfig.HookConfig.SplunkConfig.Index,
   113  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   114  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   115  				}
   116  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   117  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   118  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   119  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   120  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   121  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   122  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   123  				}
   124  			}
   125  			log.DeferExitHandler(handler)
   126  			defer handler()
   127  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   128  			abapEnvironmentCheckoutBranch(stepConfig, &stepTelemetryData)
   129  			stepTelemetryData.ErrorCode = "0"
   130  			log.Entry().Info("SUCCESS")
   131  		},
   132  	}
   133  
   134  	addAbapEnvironmentCheckoutBranchFlags(createAbapEnvironmentCheckoutBranchCmd, &stepConfig)
   135  	return createAbapEnvironmentCheckoutBranchCmd
   136  }
   137  
   138  func addAbapEnvironmentCheckoutBranchFlags(cmd *cobra.Command, stepConfig *abapEnvironmentCheckoutBranchOptions) {
   139  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
   140  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
   141  	cmd.Flags().StringVar(&stepConfig.RepositoryName, "repositoryName", os.Getenv("PIPER_repositoryName"), "Specifies a Repository (Software Component) on the SAP BTP ABAP Environment system")
   142  	cmd.Flags().StringVar(&stepConfig.BranchName, "branchName", os.Getenv("PIPER_branchName"), "Specifies a Branch of a Repository (Software Component) on the SAP BTP ABAP Environment system")
   143  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP ABAP Environment system")
   144  	cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
   145  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API Enpoint")
   146  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization")
   147  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry target space")
   148  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Cloud Foundry Service Instance")
   149  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Cloud Foundry Service Key")
   150  	cmd.Flags().StringSliceVar(&stepConfig.CertificateNames, "certificateNames", []string{}, "file names of trusted (self-signed) server certificates - need to be stored in .pipeline/trustStore")
   151  
   152  	cmd.MarkFlagRequired("username")
   153  	cmd.MarkFlagRequired("password")
   154  }
   155  
   156  // retrieve step metadata
   157  func abapEnvironmentCheckoutBranchMetadata() config.StepData {
   158  	var theMetaData = config.StepData{
   159  		Metadata: config.StepMetadata{
   160  			Name:        "abapEnvironmentCheckoutBranch",
   161  			Aliases:     []config.Alias{},
   162  			Description: "Switches between branches of a git repository on a SAP BTP ABAP Environment system",
   163  		},
   164  		Spec: config.StepSpec{
   165  			Inputs: config.StepInputs{
   166  				Secrets: []config.StepSecrets{
   167  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the BTP ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}, {Name: "credentialsId", Deprecated: false}}},
   168  				},
   169  				Parameters: []config.StepParameters{
   170  					{
   171  						Name: "username",
   172  						ResourceRef: []config.ResourceReference{
   173  							{
   174  								Name:  "abapCredentialsId",
   175  								Param: "username",
   176  								Type:  "secret",
   177  							},
   178  						},
   179  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   180  						Type:      "string",
   181  						Mandatory: true,
   182  						Aliases:   []config.Alias{},
   183  						Default:   os.Getenv("PIPER_username"),
   184  					},
   185  					{
   186  						Name: "password",
   187  						ResourceRef: []config.ResourceReference{
   188  							{
   189  								Name:  "abapCredentialsId",
   190  								Param: "password",
   191  								Type:  "secret",
   192  							},
   193  						},
   194  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   195  						Type:      "string",
   196  						Mandatory: true,
   197  						Aliases:   []config.Alias{},
   198  						Default:   os.Getenv("PIPER_password"),
   199  					},
   200  					{
   201  						Name:        "repositoryName",
   202  						ResourceRef: []config.ResourceReference{},
   203  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   204  						Type:        "string",
   205  						Mandatory:   false,
   206  						Aliases:     []config.Alias{},
   207  						Default:     os.Getenv("PIPER_repositoryName"),
   208  					},
   209  					{
   210  						Name:        "branchName",
   211  						ResourceRef: []config.ResourceReference{},
   212  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   213  						Type:        "string",
   214  						Mandatory:   false,
   215  						Aliases:     []config.Alias{},
   216  						Default:     os.Getenv("PIPER_branchName"),
   217  					},
   218  					{
   219  						Name:        "host",
   220  						ResourceRef: []config.ResourceReference{},
   221  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   222  						Type:        "string",
   223  						Mandatory:   false,
   224  						Aliases:     []config.Alias{},
   225  						Default:     os.Getenv("PIPER_host"),
   226  					},
   227  					{
   228  						Name:        "repositories",
   229  						ResourceRef: []config.ResourceReference{},
   230  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   231  						Type:        "string",
   232  						Mandatory:   false,
   233  						Aliases:     []config.Alias{},
   234  						Default:     os.Getenv("PIPER_repositories"),
   235  					},
   236  					{
   237  						Name:        "cfApiEndpoint",
   238  						ResourceRef: []config.ResourceReference{},
   239  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   240  						Type:        "string",
   241  						Mandatory:   false,
   242  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   243  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   244  					},
   245  					{
   246  						Name:        "cfOrg",
   247  						ResourceRef: []config.ResourceReference{},
   248  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   249  						Type:        "string",
   250  						Mandatory:   false,
   251  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   252  						Default:     os.Getenv("PIPER_cfOrg"),
   253  					},
   254  					{
   255  						Name:        "cfSpace",
   256  						ResourceRef: []config.ResourceReference{},
   257  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   258  						Type:        "string",
   259  						Mandatory:   false,
   260  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   261  						Default:     os.Getenv("PIPER_cfSpace"),
   262  					},
   263  					{
   264  						Name:        "cfServiceInstance",
   265  						ResourceRef: []config.ResourceReference{},
   266  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   267  						Type:        "string",
   268  						Mandatory:   false,
   269  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   270  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   271  					},
   272  					{
   273  						Name:        "cfServiceKeyName",
   274  						ResourceRef: []config.ResourceReference{},
   275  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   276  						Type:        "string",
   277  						Mandatory:   false,
   278  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKeyName"}},
   279  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   280  					},
   281  					{
   282  						Name:        "certificateNames",
   283  						ResourceRef: []config.ResourceReference{},
   284  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   285  						Type:        "[]string",
   286  						Mandatory:   false,
   287  						Aliases:     []config.Alias{},
   288  						Default:     []string{},
   289  					},
   290  				},
   291  			},
   292  			Containers: []config.Container{
   293  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   294  			},
   295  		},
   296  	}
   297  	return theMetaData
   298  }