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