github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapAddonAssemblyKitCreateTargetVector_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  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/SAP/jenkins-library/pkg/config"
    12  	"github.com/SAP/jenkins-library/pkg/log"
    13  	"github.com/SAP/jenkins-library/pkg/piperenv"
    14  	"github.com/SAP/jenkins-library/pkg/splunk"
    15  	"github.com/SAP/jenkins-library/pkg/telemetry"
    16  	"github.com/SAP/jenkins-library/pkg/validation"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  type abapAddonAssemblyKitCreateTargetVectorOptions struct {
    21  	AbapAddonAssemblyKitEndpoint string `json:"abapAddonAssemblyKitEndpoint,omitempty"`
    22  	Username                     string `json:"username,omitempty"`
    23  	Password                     string `json:"password,omitempty"`
    24  	AddonDescriptor              string `json:"addonDescriptor,omitempty"`
    25  }
    26  
    27  type abapAddonAssemblyKitCreateTargetVectorCommonPipelineEnvironment struct {
    28  	abap struct {
    29  		addonDescriptor string
    30  	}
    31  }
    32  
    33  func (p *abapAddonAssemblyKitCreateTargetVectorCommonPipelineEnvironment) persist(path, resourceName string) {
    34  	content := []struct {
    35  		category string
    36  		name     string
    37  		value    interface{}
    38  	}{
    39  		{category: "abap", name: "addonDescriptor", value: p.abap.addonDescriptor},
    40  	}
    41  
    42  	errCount := 0
    43  	for _, param := range content {
    44  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    45  		if err != nil {
    46  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    47  			errCount++
    48  		}
    49  	}
    50  	if errCount > 0 {
    51  		log.Entry().Error("failed to persist Piper environment")
    52  	}
    53  }
    54  
    55  // AbapAddonAssemblyKitCreateTargetVectorCommand This step creates a Target Vector for software lifecycle operations
    56  func AbapAddonAssemblyKitCreateTargetVectorCommand() *cobra.Command {
    57  	const STEP_NAME = "abapAddonAssemblyKitCreateTargetVector"
    58  
    59  	metadata := abapAddonAssemblyKitCreateTargetVectorMetadata()
    60  	var stepConfig abapAddonAssemblyKitCreateTargetVectorOptions
    61  	var startTime time.Time
    62  	var commonPipelineEnvironment abapAddonAssemblyKitCreateTargetVectorCommonPipelineEnvironment
    63  	var logCollector *log.CollectorHook
    64  	var splunkClient *splunk.Splunk
    65  	telemetryClient := &telemetry.Telemetry{}
    66  
    67  	var createAbapAddonAssemblyKitCreateTargetVectorCmd = &cobra.Command{
    68  		Use:   STEP_NAME,
    69  		Short: "This step creates a Target Vector for software lifecycle operations",
    70  		Long: `This step takes the Product Version and the corresponding list of Software Component Versions from the addonDescriptor in the commonPipelineEnvironment.
    71  With these it creates a Target Vector, which is necessary for executing software lifecylce operations in ABAP Cloud Platform systems.
    72  The Target Vector describes the software state, which shall be reached in the managed ABAP Cloud Platform system.
    73  <br />
    74  For Terminology refer to the [Scenario Description](https://www.project-piper.io/scenarios/abapEnvironmentAddons/).`,
    75  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    76  			startTime = time.Now()
    77  			log.SetStepName(STEP_NAME)
    78  			log.SetVerbose(GeneralConfig.Verbose)
    79  
    80  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    81  
    82  			path, _ := os.Getwd()
    83  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    84  			log.RegisterHook(fatalHook)
    85  
    86  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    87  			if err != nil {
    88  				log.SetErrorCategory(log.ErrorConfiguration)
    89  				return err
    90  			}
    91  			log.RegisterSecret(stepConfig.Username)
    92  			log.RegisterSecret(stepConfig.Password)
    93  
    94  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    95  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    96  				log.RegisterHook(&sentryHook)
    97  			}
    98  
    99  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   100  				splunkClient = &splunk.Splunk{}
   101  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   102  				log.RegisterHook(logCollector)
   103  			}
   104  
   105  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   106  			if err != nil {
   107  				return err
   108  			}
   109  			if err = validation.ValidateStruct(stepConfig); err != nil {
   110  				log.SetErrorCategory(log.ErrorConfiguration)
   111  				return err
   112  			}
   113  
   114  			return nil
   115  		},
   116  		Run: func(_ *cobra.Command, _ []string) {
   117  			stepTelemetryData := telemetry.CustomData{}
   118  			stepTelemetryData.ErrorCode = "1"
   119  			handler := func() {
   120  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   121  				config.RemoveVaultSecretFiles()
   122  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   123  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   124  				stepTelemetryData.PiperCommitHash = GitCommit
   125  				telemetryClient.SetData(&stepTelemetryData)
   126  				telemetryClient.Send()
   127  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   128  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   129  				}
   130  			}
   131  			log.DeferExitHandler(handler)
   132  			defer handler()
   133  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   134  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   135  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   136  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   137  					GeneralConfig.HookConfig.SplunkConfig.Token,
   138  					GeneralConfig.HookConfig.SplunkConfig.Index,
   139  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   140  			}
   141  			abapAddonAssemblyKitCreateTargetVector(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   142  			stepTelemetryData.ErrorCode = "0"
   143  			log.Entry().Info("SUCCESS")
   144  		},
   145  	}
   146  
   147  	addAbapAddonAssemblyKitCreateTargetVectorFlags(createAbapAddonAssemblyKitCreateTargetVectorCmd, &stepConfig)
   148  	return createAbapAddonAssemblyKitCreateTargetVectorCmd
   149  }
   150  
   151  func addAbapAddonAssemblyKitCreateTargetVectorFlags(cmd *cobra.Command, stepConfig *abapAddonAssemblyKitCreateTargetVectorOptions) {
   152  	cmd.Flags().StringVar(&stepConfig.AbapAddonAssemblyKitEndpoint, "abapAddonAssemblyKitEndpoint", `https://apps.support.sap.com`, "Base URL to the Addon Assembly Kit as a Service (AAKaaS) system")
   153  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for the Addon Assembly Kit as a Service (AAKaaS) system")
   154  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for the Addon Assembly Kit as a Service (AAKaaS) system")
   155  	cmd.Flags().StringVar(&stepConfig.AddonDescriptor, "addonDescriptor", os.Getenv("PIPER_addonDescriptor"), "Structure in the commonPipelineEnvironment containing information about the Product Version and corresponding Software Component Versions")
   156  
   157  	cmd.MarkFlagRequired("abapAddonAssemblyKitEndpoint")
   158  	cmd.MarkFlagRequired("username")
   159  	cmd.MarkFlagRequired("password")
   160  	cmd.MarkFlagRequired("addonDescriptor")
   161  }
   162  
   163  // retrieve step metadata
   164  func abapAddonAssemblyKitCreateTargetVectorMetadata() config.StepData {
   165  	var theMetaData = config.StepData{
   166  		Metadata: config.StepMetadata{
   167  			Name:        "abapAddonAssemblyKitCreateTargetVector",
   168  			Aliases:     []config.Alias{},
   169  			Description: "This step creates a Target Vector for software lifecycle operations",
   170  		},
   171  		Spec: config.StepSpec{
   172  			Inputs: config.StepInputs{
   173  				Secrets: []config.StepSecrets{
   174  					{Name: "abapAddonAssemblyKitCredentialsId", Description: "Credential stored in Jenkins for the Addon Assembly Kit as a Service (AAKaaS) system", Type: "jenkins"},
   175  				},
   176  				Parameters: []config.StepParameters{
   177  					{
   178  						Name:        "abapAddonAssemblyKitEndpoint",
   179  						ResourceRef: []config.ResourceReference{},
   180  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   181  						Type:        "string",
   182  						Mandatory:   true,
   183  						Aliases:     []config.Alias{},
   184  						Default:     `https://apps.support.sap.com`,
   185  					},
   186  					{
   187  						Name:        "username",
   188  						ResourceRef: []config.ResourceReference{},
   189  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   190  						Type:        "string",
   191  						Mandatory:   true,
   192  						Aliases:     []config.Alias{},
   193  						Default:     os.Getenv("PIPER_username"),
   194  					},
   195  					{
   196  						Name:        "password",
   197  						ResourceRef: []config.ResourceReference{},
   198  						Scope:       []string{"PARAMETERS"},
   199  						Type:        "string",
   200  						Mandatory:   true,
   201  						Aliases:     []config.Alias{},
   202  						Default:     os.Getenv("PIPER_password"),
   203  					},
   204  					{
   205  						Name: "addonDescriptor",
   206  						ResourceRef: []config.ResourceReference{
   207  							{
   208  								Name:  "commonPipelineEnvironment",
   209  								Param: "abap/addonDescriptor",
   210  							},
   211  						},
   212  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   213  						Type:      "string",
   214  						Mandatory: true,
   215  						Aliases:   []config.Alias{},
   216  						Default:   os.Getenv("PIPER_addonDescriptor"),
   217  					},
   218  				},
   219  			},
   220  			Outputs: config.StepOutputs{
   221  				Resources: []config.StepResources{
   222  					{
   223  						Name: "commonPipelineEnvironment",
   224  						Type: "piperEnvironment",
   225  						Parameters: []map[string]interface{}{
   226  							{"name": "abap/addonDescriptor"},
   227  						},
   228  					},
   229  				},
   230  			},
   231  		},
   232  	}
   233  	return theMetaData
   234  }