github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapEnvironmentRunATCCheck_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 abapEnvironmentRunATCCheckOptions struct {
    19  	AtcConfig          string `json:"atcConfig,omitempty"`
    20  	Repositories       string `json:"repositories,omitempty"`
    21  	CfAPIEndpoint      string `json:"cfApiEndpoint,omitempty"`
    22  	CfOrg              string `json:"cfOrg,omitempty"`
    23  	CfServiceInstance  string `json:"cfServiceInstance,omitempty"`
    24  	CfServiceKeyName   string `json:"cfServiceKeyName,omitempty"`
    25  	CfSpace            string `json:"cfSpace,omitempty"`
    26  	Username           string `json:"username,omitempty"`
    27  	Password           string `json:"password,omitempty"`
    28  	Host               string `json:"host,omitempty"`
    29  	AtcResultsFileName string `json:"atcResultsFileName,omitempty"`
    30  	GenerateHTML       bool   `json:"generateHTML,omitempty"`
    31  }
    32  
    33  // AbapEnvironmentRunATCCheckCommand Runs an ATC Check
    34  func AbapEnvironmentRunATCCheckCommand() *cobra.Command {
    35  	const STEP_NAME = "abapEnvironmentRunATCCheck"
    36  
    37  	metadata := abapEnvironmentRunATCCheckMetadata()
    38  	var stepConfig abapEnvironmentRunATCCheckOptions
    39  	var startTime time.Time
    40  	var logCollector *log.CollectorHook
    41  	var splunkClient *splunk.Splunk
    42  	telemetryClient := &telemetry.Telemetry{}
    43  
    44  	var createAbapEnvironmentRunATCCheckCmd = &cobra.Command{
    45  		Use:   STEP_NAME,
    46  		Short: "Runs an ATC Check",
    47  		Long: `This step is for triggering an [ATC](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/d8cec788fc104ff9ad9c3757b4dd13d4.html) test run on an SAP Cloud Platform ABAP Environment system.
    48  Please provide either of the following options:
    49  
    50  * The host and credentials the Cloud Platform 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).
    51  * 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.
    52  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.
    53  
    54  Regardless of the option you chose, please make sure to provide the configuration for Software Components and Packages that you want to be checked analog to the examples listed on this page.`,
    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 {
    80  				splunkClient = &splunk.Splunk{}
    81  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    82  				log.RegisterHook(logCollector)
    83  			}
    84  
    85  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    86  			if err != nil {
    87  				return err
    88  			}
    89  			if err = validation.ValidateStruct(stepConfig); err != nil {
    90  				log.SetErrorCategory(log.ErrorConfiguration)
    91  				return err
    92  			}
    93  
    94  			return nil
    95  		},
    96  		Run: func(_ *cobra.Command, _ []string) {
    97  			stepTelemetryData := telemetry.CustomData{}
    98  			stepTelemetryData.ErrorCode = "1"
    99  			handler := func() {
   100  				config.RemoveVaultSecretFiles()
   101  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   102  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   103  				stepTelemetryData.PiperCommitHash = GitCommit
   104  				telemetryClient.SetData(&stepTelemetryData)
   105  				telemetryClient.Send()
   106  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   107  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   108  				}
   109  			}
   110  			log.DeferExitHandler(handler)
   111  			defer handler()
   112  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   113  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   114  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   115  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   116  					GeneralConfig.HookConfig.SplunkConfig.Token,
   117  					GeneralConfig.HookConfig.SplunkConfig.Index,
   118  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   119  			}
   120  			abapEnvironmentRunATCCheck(stepConfig, &stepTelemetryData)
   121  			stepTelemetryData.ErrorCode = "0"
   122  			log.Entry().Info("SUCCESS")
   123  		},
   124  	}
   125  
   126  	addAbapEnvironmentRunATCCheckFlags(createAbapEnvironmentRunATCCheckCmd, &stepConfig)
   127  	return createAbapEnvironmentRunATCCheckCmd
   128  }
   129  
   130  func addAbapEnvironmentRunATCCheckFlags(cmd *cobra.Command, stepConfig *abapEnvironmentRunATCCheckOptions) {
   131  	cmd.Flags().StringVar(&stepConfig.AtcConfig, "atcConfig", os.Getenv("PIPER_atcConfig"), "Path to a YAML configuration file for Packages and/or Software Components to be checked during ATC run")
   132  	cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
   133  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   134  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "CF org")
   135  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter of ServiceInstance Name to delete CloudFoundry Service")
   136  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter of CloudFoundry Service Key to be created")
   137  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "CF Space")
   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.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP Cloud Platform ABAP Environment system")
   141  	cmd.Flags().StringVar(&stepConfig.AtcResultsFileName, "atcResultsFileName", `ATCResults.xml`, "Specifies output file name for the results from the ATC run. This file name will also be used for generating the HTML file")
   142  	cmd.Flags().BoolVar(&stepConfig.GenerateHTML, "generateHTML", false, "Specifies whether the ATC results should also be generated as an HTML document")
   143  
   144  	cmd.MarkFlagRequired("username")
   145  	cmd.MarkFlagRequired("password")
   146  }
   147  
   148  // retrieve step metadata
   149  func abapEnvironmentRunATCCheckMetadata() config.StepData {
   150  	var theMetaData = config.StepData{
   151  		Metadata: config.StepMetadata{
   152  			Name:        "abapEnvironmentRunATCCheck",
   153  			Aliases:     []config.Alias{},
   154  			Description: "Runs an ATC Check",
   155  		},
   156  		Spec: config.StepSpec{
   157  			Inputs: config.StepInputs{
   158  				Secrets: []config.StepSecrets{
   159  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the Cloud Platform ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}}},
   160  				},
   161  				Parameters: []config.StepParameters{
   162  					{
   163  						Name:        "atcConfig",
   164  						ResourceRef: []config.ResourceReference{},
   165  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   166  						Type:        "string",
   167  						Mandatory:   false,
   168  						Aliases:     []config.Alias{},
   169  						Default:     os.Getenv("PIPER_atcConfig"),
   170  					},
   171  					{
   172  						Name:        "repositories",
   173  						ResourceRef: []config.ResourceReference{},
   174  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   175  						Type:        "string",
   176  						Mandatory:   false,
   177  						Aliases:     []config.Alias{},
   178  						Default:     os.Getenv("PIPER_repositories"),
   179  					},
   180  					{
   181  						Name:        "cfApiEndpoint",
   182  						ResourceRef: []config.ResourceReference{},
   183  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   184  						Type:        "string",
   185  						Mandatory:   false,
   186  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   187  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   188  					},
   189  					{
   190  						Name:        "cfOrg",
   191  						ResourceRef: []config.ResourceReference{},
   192  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   193  						Type:        "string",
   194  						Mandatory:   false,
   195  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   196  						Default:     os.Getenv("PIPER_cfOrg"),
   197  					},
   198  					{
   199  						Name:        "cfServiceInstance",
   200  						ResourceRef: []config.ResourceReference{},
   201  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   202  						Type:        "string",
   203  						Mandatory:   false,
   204  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   205  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   206  					},
   207  					{
   208  						Name:        "cfServiceKeyName",
   209  						ResourceRef: []config.ResourceReference{},
   210  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   211  						Type:        "string",
   212  						Mandatory:   false,
   213  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   214  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   215  					},
   216  					{
   217  						Name:        "cfSpace",
   218  						ResourceRef: []config.ResourceReference{},
   219  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   220  						Type:        "string",
   221  						Mandatory:   false,
   222  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   223  						Default:     os.Getenv("PIPER_cfSpace"),
   224  					},
   225  					{
   226  						Name: "username",
   227  						ResourceRef: []config.ResourceReference{
   228  							{
   229  								Name:  "abapCredentialsId",
   230  								Param: "username",
   231  								Type:  "secret",
   232  							},
   233  						},
   234  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   235  						Type:      "string",
   236  						Mandatory: true,
   237  						Aliases:   []config.Alias{},
   238  						Default:   os.Getenv("PIPER_username"),
   239  					},
   240  					{
   241  						Name: "password",
   242  						ResourceRef: []config.ResourceReference{
   243  							{
   244  								Name:  "abapCredentialsId",
   245  								Param: "password",
   246  								Type:  "secret",
   247  							},
   248  						},
   249  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   250  						Type:      "string",
   251  						Mandatory: true,
   252  						Aliases:   []config.Alias{},
   253  						Default:   os.Getenv("PIPER_password"),
   254  					},
   255  					{
   256  						Name:        "host",
   257  						ResourceRef: []config.ResourceReference{},
   258  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   259  						Type:        "string",
   260  						Mandatory:   false,
   261  						Aliases:     []config.Alias{},
   262  						Default:     os.Getenv("PIPER_host"),
   263  					},
   264  					{
   265  						Name:        "atcResultsFileName",
   266  						ResourceRef: []config.ResourceReference{},
   267  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   268  						Type:        "string",
   269  						Mandatory:   false,
   270  						Aliases:     []config.Alias{},
   271  						Default:     `ATCResults.xml`,
   272  					},
   273  					{
   274  						Name:        "generateHTML",
   275  						ResourceRef: []config.ResourceReference{},
   276  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   277  						Type:        "bool",
   278  						Mandatory:   false,
   279  						Aliases:     []config.Alias{},
   280  						Default:     false,
   281  					},
   282  				},
   283  			},
   284  			Containers: []config.Container{
   285  				{Name: "cf", Image: "ppiper/cf-cli:7"},
   286  			},
   287  		},
   288  	}
   289  	return theMetaData
   290  }