github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/cloudFoundryCreateServiceKey_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 cloudFoundryCreateServiceKeyOptions struct {
    19  	CfAPIEndpoint      string `json:"cfApiEndpoint,omitempty"`
    20  	Username           string `json:"username,omitempty"`
    21  	Password           string `json:"password,omitempty"`
    22  	CfOrg              string `json:"cfOrg,omitempty"`
    23  	CfSpace            string `json:"cfSpace,omitempty"`
    24  	CfServiceInstance  string `json:"cfServiceInstance,omitempty"`
    25  	CfServiceKeyName   string `json:"cfServiceKeyName,omitempty"`
    26  	CfServiceKeyConfig string `json:"cfServiceKeyConfig,omitempty"`
    27  	CfAsync            bool   `json:"cfAsync,omitempty"`
    28  }
    29  
    30  // CloudFoundryCreateServiceKeyCommand cloudFoundryCreateServiceKey
    31  func CloudFoundryCreateServiceKeyCommand() *cobra.Command {
    32  	const STEP_NAME = "cloudFoundryCreateServiceKey"
    33  
    34  	metadata := cloudFoundryCreateServiceKeyMetadata()
    35  	var stepConfig cloudFoundryCreateServiceKeyOptions
    36  	var startTime time.Time
    37  	var logCollector *log.CollectorHook
    38  	var splunkClient *splunk.Splunk
    39  	telemetryClient := &telemetry.Telemetry{}
    40  
    41  	var createCloudFoundryCreateServiceKeyCmd = &cobra.Command{
    42  		Use:   STEP_NAME,
    43  		Short: "cloudFoundryCreateServiceKey",
    44  		Long:  `Create CloudFoundryServiceKey`,
    45  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    46  			startTime = time.Now()
    47  			log.SetStepName(STEP_NAME)
    48  			log.SetVerbose(GeneralConfig.Verbose)
    49  
    50  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    51  
    52  			path, _ := os.Getwd()
    53  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    54  			log.RegisterHook(fatalHook)
    55  
    56  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    57  			if err != nil {
    58  				log.SetErrorCategory(log.ErrorConfiguration)
    59  				return err
    60  			}
    61  			log.RegisterSecret(stepConfig.Username)
    62  			log.RegisterSecret(stepConfig.Password)
    63  
    64  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    65  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    66  				log.RegisterHook(&sentryHook)
    67  			}
    68  
    69  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    70  				splunkClient = &splunk.Splunk{}
    71  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    72  				log.RegisterHook(logCollector)
    73  			}
    74  
    75  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    76  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    77  			}
    78  
    79  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    80  			if err != nil {
    81  				return err
    82  			}
    83  			if err = validation.ValidateStruct(stepConfig); err != nil {
    84  				log.SetErrorCategory(log.ErrorConfiguration)
    85  				return err
    86  			}
    87  
    88  			return nil
    89  		},
    90  		Run: func(_ *cobra.Command, _ []string) {
    91  			stepTelemetryData := telemetry.CustomData{}
    92  			stepTelemetryData.ErrorCode = "1"
    93  			handler := func() {
    94  				config.RemoveVaultSecretFiles()
    95  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    96  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    97  				stepTelemetryData.PiperCommitHash = GitCommit
    98  				telemetryClient.SetData(&stepTelemetryData)
    99  				telemetryClient.Send()
   100  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   101  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   102  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   103  						GeneralConfig.HookConfig.SplunkConfig.Token,
   104  						GeneralConfig.HookConfig.SplunkConfig.Index,
   105  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   106  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   107  				}
   108  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   109  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   110  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   111  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   112  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   113  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   114  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   115  				}
   116  			}
   117  			log.DeferExitHandler(handler)
   118  			defer handler()
   119  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   120  			cloudFoundryCreateServiceKey(stepConfig, &stepTelemetryData)
   121  			stepTelemetryData.ErrorCode = "0"
   122  			log.Entry().Info("SUCCESS")
   123  		},
   124  	}
   125  
   126  	addCloudFoundryCreateServiceKeyFlags(createCloudFoundryCreateServiceKeyCmd, &stepConfig)
   127  	return createCloudFoundryCreateServiceKeyCmd
   128  }
   129  
   130  func addCloudFoundryCreateServiceKeyFlags(cmd *cobra.Command, stepConfig *cloudFoundryCreateServiceKeyOptions) {
   131  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   132  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF")
   133  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "User Password for CF User")
   134  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "CF org")
   135  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "CF Space")
   136  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter for CloudFoundry Service Instance Name")
   137  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter for Service Key name for CloudFoundry Service Key to be created")
   138  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyConfig, "cfServiceKeyConfig", os.Getenv("PIPER_cfServiceKeyConfig"), "Path to JSON config file path or JSON in-line string for Cloud Foundry Service Key creation")
   139  	cmd.Flags().BoolVar(&stepConfig.CfAsync, "cfAsync", true, "Decides if the service key creation runs asynchronously")
   140  
   141  	cmd.MarkFlagRequired("cfApiEndpoint")
   142  	cmd.MarkFlagRequired("username")
   143  	cmd.MarkFlagRequired("password")
   144  	cmd.MarkFlagRequired("cfOrg")
   145  	cmd.MarkFlagRequired("cfSpace")
   146  	cmd.MarkFlagRequired("cfServiceInstance")
   147  	cmd.MarkFlagRequired("cfServiceKeyName")
   148  }
   149  
   150  // retrieve step metadata
   151  func cloudFoundryCreateServiceKeyMetadata() config.StepData {
   152  	var theMetaData = config.StepData{
   153  		Metadata: config.StepMetadata{
   154  			Name:        "cloudFoundryCreateServiceKey",
   155  			Aliases:     []config.Alias{},
   156  			Description: "cloudFoundryCreateServiceKey",
   157  		},
   158  		Spec: config.StepSpec{
   159  			Inputs: config.StepInputs{
   160  				Secrets: []config.StepSecrets{
   161  					{Name: "cfCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate to the Cloud Foundry API.", Type: "jenkins"},
   162  				},
   163  				Parameters: []config.StepParameters{
   164  					{
   165  						Name:        "cfApiEndpoint",
   166  						ResourceRef: []config.ResourceReference{},
   167  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   168  						Type:        "string",
   169  						Mandatory:   true,
   170  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   171  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   172  					},
   173  					{
   174  						Name: "username",
   175  						ResourceRef: []config.ResourceReference{
   176  							{
   177  								Name:  "cfCredentialsId",
   178  								Param: "username",
   179  								Type:  "secret",
   180  							},
   181  
   182  							{
   183  								Name:    "cloudfoundryVaultSecretName",
   184  								Type:    "vaultSecret",
   185  								Default: "cloudfoundry-$(org)-$(space)",
   186  							},
   187  						},
   188  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   189  						Type:      "string",
   190  						Mandatory: true,
   191  						Aliases:   []config.Alias{},
   192  						Default:   os.Getenv("PIPER_username"),
   193  					},
   194  					{
   195  						Name: "password",
   196  						ResourceRef: []config.ResourceReference{
   197  							{
   198  								Name:  "cfCredentialsId",
   199  								Param: "password",
   200  								Type:  "secret",
   201  							},
   202  
   203  							{
   204  								Name:    "cloudfoundryVaultSecretName",
   205  								Type:    "vaultSecret",
   206  								Default: "cloudfoundry-$(org)-$(space)",
   207  							},
   208  						},
   209  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   210  						Type:      "string",
   211  						Mandatory: true,
   212  						Aliases:   []config.Alias{},
   213  						Default:   os.Getenv("PIPER_password"),
   214  					},
   215  					{
   216  						Name:        "cfOrg",
   217  						ResourceRef: []config.ResourceReference{},
   218  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   219  						Type:        "string",
   220  						Mandatory:   true,
   221  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   222  						Default:     os.Getenv("PIPER_cfOrg"),
   223  					},
   224  					{
   225  						Name:        "cfSpace",
   226  						ResourceRef: []config.ResourceReference{},
   227  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   228  						Type:        "string",
   229  						Mandatory:   true,
   230  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   231  						Default:     os.Getenv("PIPER_cfSpace"),
   232  					},
   233  					{
   234  						Name:        "cfServiceInstance",
   235  						ResourceRef: []config.ResourceReference{},
   236  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   237  						Type:        "string",
   238  						Mandatory:   true,
   239  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   240  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   241  					},
   242  					{
   243  						Name:        "cfServiceKeyName",
   244  						ResourceRef: []config.ResourceReference{},
   245  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   246  						Type:        "string",
   247  						Mandatory:   true,
   248  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   249  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   250  					},
   251  					{
   252  						Name:        "cfServiceKeyConfig",
   253  						ResourceRef: []config.ResourceReference{},
   254  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   255  						Type:        "string",
   256  						Mandatory:   false,
   257  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKeyConfig"}},
   258  						Default:     os.Getenv("PIPER_cfServiceKeyConfig"),
   259  					},
   260  					{
   261  						Name:        "cfAsync",
   262  						ResourceRef: []config.ResourceReference{},
   263  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   264  						Type:        "bool",
   265  						Mandatory:   false,
   266  						Aliases:     []config.Alias{},
   267  						Default:     true,
   268  					},
   269  				},
   270  			},
   271  			Containers: []config.Container{
   272  				{Name: "cf", Image: "ppiper/cf-cli:latest"},
   273  			},
   274  		},
   275  	}
   276  	return theMetaData
   277  }