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