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