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