github.com/SAP/jenkins-library@v1.362.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  	FailOnSeverity     string `json:"failOnSeverity,omitempty"`
    32  }
    33  
    34  // AbapEnvironmentRunATCCheckCommand Runs an ATC Check
    35  func AbapEnvironmentRunATCCheckCommand() *cobra.Command {
    36  	const STEP_NAME = "abapEnvironmentRunATCCheck"
    37  
    38  	metadata := abapEnvironmentRunATCCheckMetadata()
    39  	var stepConfig abapEnvironmentRunATCCheckOptions
    40  	var startTime time.Time
    41  	var logCollector *log.CollectorHook
    42  	var splunkClient *splunk.Splunk
    43  	telemetryClient := &telemetry.Telemetry{}
    44  
    45  	var createAbapEnvironmentRunATCCheckCmd = &cobra.Command{
    46  		Use:   STEP_NAME,
    47  		Short: "Runs an ATC Check",
    48  		Long: `This step is for triggering an [ATC](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/d8cec788fc104ff9ad9c3757b4dd13d4.html) test run on an SAP BTP ABAP Environment system.
    49  Please provide either of the following options:
    50  
    51  * The host and credentials the Cloud Platform ABAP Environment system itself. The credentials must be configured for the Communication Scenario [SAP_COM_0901](https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/d8cec788fc104ff9ad9c3757b4dd13d4.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_0901.
    53  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.
    54  
    55  Regardless of the option you chose, please make sure to provide the configuration the object set (e.g. with Software Components and Packages) that you want to be checked analog to the examples listed on this page.`,
    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 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 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, GeneralConfig.HookConfig.PendoConfig.Token)
   131  			abapEnvironmentRunATCCheck(stepConfig, &stepTelemetryData)
   132  			stepTelemetryData.ErrorCode = "0"
   133  			log.Entry().Info("SUCCESS")
   134  		},
   135  	}
   136  
   137  	addAbapEnvironmentRunATCCheckFlags(createAbapEnvironmentRunATCCheckCmd, &stepConfig)
   138  	return createAbapEnvironmentRunATCCheckCmd
   139  }
   140  
   141  func addAbapEnvironmentRunATCCheckFlags(cmd *cobra.Command, stepConfig *abapEnvironmentRunATCCheckOptions) {
   142  	cmd.Flags().StringVar(&stepConfig.AtcConfig, "atcConfig", os.Getenv("PIPER_atcConfig"), "Path to a YAML configuration file for the object set to be checked during ATC run")
   143  	cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
   144  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   145  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "CF org")
   146  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter of ServiceInstance Name to delete CloudFoundry Service")
   147  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter of CloudFoundry Service Key to be created")
   148  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "CF Space")
   149  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0901")
   150  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0901")
   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.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")
   153  	cmd.Flags().BoolVar(&stepConfig.GenerateHTML, "generateHTML", false, "Specifies whether the ATC results should also be generated as an HTML document")
   154  	cmd.Flags().StringVar(&stepConfig.FailOnSeverity, "failOnSeverity", os.Getenv("PIPER_failOnSeverity"), "Specifies the severity level, for which the ATC step should fail if at least one message with this severity (or \"higher\") level is returned by the ATC Check Run (possible values - error, warning, info). Initial value is default behavior and ATC findings of any severity do not fail the step")
   155  
   156  	cmd.MarkFlagRequired("username")
   157  	cmd.MarkFlagRequired("password")
   158  }
   159  
   160  // retrieve step metadata
   161  func abapEnvironmentRunATCCheckMetadata() config.StepData {
   162  	var theMetaData = config.StepData{
   163  		Metadata: config.StepMetadata{
   164  			Name:        "abapEnvironmentRunATCCheck",
   165  			Aliases:     []config.Alias{},
   166  			Description: "Runs an ATC Check",
   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 Cloud Platform ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}}},
   172  				},
   173  				Parameters: []config.StepParameters{
   174  					{
   175  						Name:        "atcConfig",
   176  						ResourceRef: []config.ResourceReference{},
   177  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   178  						Type:        "string",
   179  						Mandatory:   false,
   180  						Aliases:     []config.Alias{},
   181  						Default:     os.Getenv("PIPER_atcConfig"),
   182  					},
   183  					{
   184  						Name:        "repositories",
   185  						ResourceRef: []config.ResourceReference{},
   186  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   187  						Type:        "string",
   188  						Mandatory:   false,
   189  						Aliases:     []config.Alias{},
   190  						Default:     os.Getenv("PIPER_repositories"),
   191  					},
   192  					{
   193  						Name:        "cfApiEndpoint",
   194  						ResourceRef: []config.ResourceReference{},
   195  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   196  						Type:        "string",
   197  						Mandatory:   false,
   198  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   199  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   200  					},
   201  					{
   202  						Name:        "cfOrg",
   203  						ResourceRef: []config.ResourceReference{},
   204  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   205  						Type:        "string",
   206  						Mandatory:   false,
   207  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   208  						Default:     os.Getenv("PIPER_cfOrg"),
   209  					},
   210  					{
   211  						Name:        "cfServiceInstance",
   212  						ResourceRef: []config.ResourceReference{},
   213  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   214  						Type:        "string",
   215  						Mandatory:   false,
   216  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   217  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   218  					},
   219  					{
   220  						Name:        "cfServiceKeyName",
   221  						ResourceRef: []config.ResourceReference{},
   222  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   223  						Type:        "string",
   224  						Mandatory:   false,
   225  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   226  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   227  					},
   228  					{
   229  						Name:        "cfSpace",
   230  						ResourceRef: []config.ResourceReference{},
   231  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   232  						Type:        "string",
   233  						Mandatory:   false,
   234  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   235  						Default:     os.Getenv("PIPER_cfSpace"),
   236  					},
   237  					{
   238  						Name: "username",
   239  						ResourceRef: []config.ResourceReference{
   240  							{
   241  								Name:  "abapCredentialsId",
   242  								Param: "username",
   243  								Type:  "secret",
   244  							},
   245  						},
   246  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   247  						Type:      "string",
   248  						Mandatory: true,
   249  						Aliases:   []config.Alias{},
   250  						Default:   os.Getenv("PIPER_username"),
   251  					},
   252  					{
   253  						Name: "password",
   254  						ResourceRef: []config.ResourceReference{
   255  							{
   256  								Name:  "abapCredentialsId",
   257  								Param: "password",
   258  								Type:  "secret",
   259  							},
   260  						},
   261  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   262  						Type:      "string",
   263  						Mandatory: true,
   264  						Aliases:   []config.Alias{},
   265  						Default:   os.Getenv("PIPER_password"),
   266  					},
   267  					{
   268  						Name:        "host",
   269  						ResourceRef: []config.ResourceReference{},
   270  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   271  						Type:        "string",
   272  						Mandatory:   false,
   273  						Aliases:     []config.Alias{},
   274  						Default:     os.Getenv("PIPER_host"),
   275  					},
   276  					{
   277  						Name:        "atcResultsFileName",
   278  						ResourceRef: []config.ResourceReference{},
   279  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   280  						Type:        "string",
   281  						Mandatory:   false,
   282  						Aliases:     []config.Alias{},
   283  						Default:     `ATCResults.xml`,
   284  					},
   285  					{
   286  						Name:        "generateHTML",
   287  						ResourceRef: []config.ResourceReference{},
   288  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   289  						Type:        "bool",
   290  						Mandatory:   false,
   291  						Aliases:     []config.Alias{},
   292  						Default:     false,
   293  					},
   294  					{
   295  						Name:        "failOnSeverity",
   296  						ResourceRef: []config.ResourceReference{},
   297  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   298  						Type:        "string",
   299  						Mandatory:   false,
   300  						Aliases:     []config.Alias{},
   301  						Default:     os.Getenv("PIPER_failOnSeverity"),
   302  					},
   303  				},
   304  			},
   305  			Containers: []config.Container{
   306  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   307  			},
   308  		},
   309  	}
   310  	return theMetaData
   311  }