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