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