github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentPushATCSystemConfig_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 abapEnvironmentPushATCSystemConfigOptions struct {
    19  	AtcSystemConfigFilePath string `json:"atcSystemConfigFilePath,omitempty"`
    20  	PatchIfExisting         bool   `json:"patchIfExisting,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  }
    30  
    31  // AbapEnvironmentPushATCSystemConfigCommand Create/Update ATC System Configuration
    32  func AbapEnvironmentPushATCSystemConfigCommand() *cobra.Command {
    33  	const STEP_NAME = "abapEnvironmentPushATCSystemConfig"
    34  
    35  	metadata := abapEnvironmentPushATCSystemConfigMetadata()
    36  	var stepConfig abapEnvironmentPushATCSystemConfigOptions
    37  	var startTime time.Time
    38  	var logCollector *log.CollectorHook
    39  	var splunkClient *splunk.Splunk
    40  	telemetryClient := &telemetry.Telemetry{}
    41  
    42  	var createAbapEnvironmentPushATCSystemConfigCmd = &cobra.Command{
    43  		Use:   STEP_NAME,
    44  		Short: "Create/Update ATC System Configuration",
    45  		Long: `This step is for creating/updating an [ATC](https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/657285a09f7148d894c27bb8e17827cf.html?version=Cloud) system configurationon on an SAP BTP, ABAP Environment system.
    46  Please provide either of the following options:
    47  
    48  * The host and credentials the SAP BTP, ABAP Environment system itself. The credentials must be configured for the Communication Scenario [SAP_COM_0763](https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/657285a09f7148d894c27bb8e17827cf.html?version=Cloud).
    49  * 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_0763.
    50  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.`,
    51  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    52  			startTime = time.Now()
    53  			log.SetStepName(STEP_NAME)
    54  			log.SetVerbose(GeneralConfig.Verbose)
    55  
    56  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    57  
    58  			path, _ := os.Getwd()
    59  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    60  			log.RegisterHook(fatalHook)
    61  
    62  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    63  			if err != nil {
    64  				log.SetErrorCategory(log.ErrorConfiguration)
    65  				return err
    66  			}
    67  			log.RegisterSecret(stepConfig.Username)
    68  			log.RegisterSecret(stepConfig.Password)
    69  
    70  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    71  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    72  				log.RegisterHook(&sentryHook)
    73  			}
    74  
    75  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    76  				splunkClient = &splunk.Splunk{}
    77  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    78  				log.RegisterHook(logCollector)
    79  			}
    80  
    81  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    82  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    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.Initialize(GeneralConfig.CorrelationID,
   108  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   109  						GeneralConfig.HookConfig.SplunkConfig.Token,
   110  						GeneralConfig.HookConfig.SplunkConfig.Index,
   111  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   112  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   113  				}
   114  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   115  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   116  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   117  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   118  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   119  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   120  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   121  				}
   122  			}
   123  			log.DeferExitHandler(handler)
   124  			defer handler()
   125  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   126  			abapEnvironmentPushATCSystemConfig(stepConfig, &stepTelemetryData)
   127  			stepTelemetryData.ErrorCode = "0"
   128  			log.Entry().Info("SUCCESS")
   129  		},
   130  	}
   131  
   132  	addAbapEnvironmentPushATCSystemConfigFlags(createAbapEnvironmentPushATCSystemConfigCmd, &stepConfig)
   133  	return createAbapEnvironmentPushATCSystemConfigCmd
   134  }
   135  
   136  func addAbapEnvironmentPushATCSystemConfigFlags(cmd *cobra.Command, stepConfig *abapEnvironmentPushATCSystemConfigOptions) {
   137  	cmd.Flags().StringVar(&stepConfig.AtcSystemConfigFilePath, "atcSystemConfigFilePath", os.Getenv("PIPER_atcSystemConfigFilePath"), "Path to a JSON file with ATC System Configuration")
   138  	cmd.Flags().BoolVar(&stepConfig.PatchIfExisting, "patchIfExisting", true, "In case an configuration under the given name already exists in the system. Should the step update/patch the existing ATC Systm Configuration from the provided ATC System Configuration file?")
   139  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   140  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "CF org")
   141  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter of ServiceInstance Name to delete CloudFoundry Service")
   142  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter of CloudFoundry Service Key to be created")
   143  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "CF Space")
   144  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0763")
   145  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0763")
   146  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP SAP BTP, ABAP Environment system")
   147  
   148  	cmd.MarkFlagRequired("atcSystemConfigFilePath")
   149  	cmd.MarkFlagRequired("username")
   150  	cmd.MarkFlagRequired("password")
   151  }
   152  
   153  // retrieve step metadata
   154  func abapEnvironmentPushATCSystemConfigMetadata() config.StepData {
   155  	var theMetaData = config.StepData{
   156  		Metadata: config.StepMetadata{
   157  			Name:        "abapEnvironmentPushATCSystemConfig",
   158  			Aliases:     []config.Alias{},
   159  			Description: "Create/Update ATC System Configuration",
   160  		},
   161  		Spec: config.StepSpec{
   162  			Inputs: config.StepInputs{
   163  				Secrets: []config.StepSecrets{
   164  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the SAP BTP, ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}}},
   165  				},
   166  				Parameters: []config.StepParameters{
   167  					{
   168  						Name:        "atcSystemConfigFilePath",
   169  						ResourceRef: []config.ResourceReference{},
   170  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   171  						Type:        "string",
   172  						Mandatory:   true,
   173  						Aliases:     []config.Alias{},
   174  						Default:     os.Getenv("PIPER_atcSystemConfigFilePath"),
   175  					},
   176  					{
   177  						Name:        "patchIfExisting",
   178  						ResourceRef: []config.ResourceReference{},
   179  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   180  						Type:        "bool",
   181  						Mandatory:   false,
   182  						Aliases:     []config.Alias{},
   183  						Default:     true,
   184  					},
   185  					{
   186  						Name:        "cfApiEndpoint",
   187  						ResourceRef: []config.ResourceReference{},
   188  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   189  						Type:        "string",
   190  						Mandatory:   false,
   191  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   192  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   193  					},
   194  					{
   195  						Name:        "cfOrg",
   196  						ResourceRef: []config.ResourceReference{},
   197  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   198  						Type:        "string",
   199  						Mandatory:   false,
   200  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   201  						Default:     os.Getenv("PIPER_cfOrg"),
   202  					},
   203  					{
   204  						Name:        "cfServiceInstance",
   205  						ResourceRef: []config.ResourceReference{},
   206  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   207  						Type:        "string",
   208  						Mandatory:   false,
   209  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   210  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   211  					},
   212  					{
   213  						Name:        "cfServiceKeyName",
   214  						ResourceRef: []config.ResourceReference{},
   215  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   216  						Type:        "string",
   217  						Mandatory:   false,
   218  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   219  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   220  					},
   221  					{
   222  						Name:        "cfSpace",
   223  						ResourceRef: []config.ResourceReference{},
   224  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   225  						Type:        "string",
   226  						Mandatory:   false,
   227  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   228  						Default:     os.Getenv("PIPER_cfSpace"),
   229  					},
   230  					{
   231  						Name: "username",
   232  						ResourceRef: []config.ResourceReference{
   233  							{
   234  								Name:  "abapCredentialsId",
   235  								Param: "username",
   236  								Type:  "secret",
   237  							},
   238  						},
   239  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   240  						Type:      "string",
   241  						Mandatory: true,
   242  						Aliases:   []config.Alias{},
   243  						Default:   os.Getenv("PIPER_username"),
   244  					},
   245  					{
   246  						Name: "password",
   247  						ResourceRef: []config.ResourceReference{
   248  							{
   249  								Name:  "abapCredentialsId",
   250  								Param: "password",
   251  								Type:  "secret",
   252  							},
   253  						},
   254  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   255  						Type:      "string",
   256  						Mandatory: true,
   257  						Aliases:   []config.Alias{},
   258  						Default:   os.Getenv("PIPER_password"),
   259  					},
   260  					{
   261  						Name:        "host",
   262  						ResourceRef: []config.ResourceReference{},
   263  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   264  						Type:        "string",
   265  						Mandatory:   false,
   266  						Aliases:     []config.Alias{},
   267  						Default:     os.Getenv("PIPER_host"),
   268  					},
   269  				},
   270  			},
   271  			Containers: []config.Container{
   272  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   273  			},
   274  		},
   275  	}
   276  	return theMetaData
   277  }