github.com/jaylevin/jenkins-library@v1.230.4/cmd/cloudFoundryDeleteSpace_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 cloudFoundryDeleteSpaceOptions 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  }
    25  
    26  // CloudFoundryDeleteSpaceCommand Deletes a space in Cloud Foundry
    27  func CloudFoundryDeleteSpaceCommand() *cobra.Command {
    28  	const STEP_NAME = "cloudFoundryDeleteSpace"
    29  
    30  	metadata := cloudFoundryDeleteSpaceMetadata()
    31  	var stepConfig cloudFoundryDeleteSpaceOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createCloudFoundryDeleteSpaceCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Deletes a space in Cloud Foundry",
    40  		Long: `Deletes a space in Cloud Foundry
    41  Mandatory:
    42  * Cloud Foundry API endpoint, Organization, Space name`,
    43  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    44  			startTime = time.Now()
    45  			log.SetStepName(STEP_NAME)
    46  			log.SetVerbose(GeneralConfig.Verbose)
    47  
    48  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    49  
    50  			path, _ := os.Getwd()
    51  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    52  			log.RegisterHook(fatalHook)
    53  
    54  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    55  			if err != nil {
    56  				log.SetErrorCategory(log.ErrorConfiguration)
    57  				return err
    58  			}
    59  			log.RegisterSecret(stepConfig.Username)
    60  			log.RegisterSecret(stepConfig.Password)
    61  
    62  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    63  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    64  				log.RegisterHook(&sentryHook)
    65  			}
    66  
    67  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    68  				splunkClient = &splunk.Splunk{}
    69  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    70  				log.RegisterHook(logCollector)
    71  			}
    72  
    73  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    74  			if err != nil {
    75  				return err
    76  			}
    77  			if err = validation.ValidateStruct(stepConfig); err != nil {
    78  				log.SetErrorCategory(log.ErrorConfiguration)
    79  				return err
    80  			}
    81  
    82  			return nil
    83  		},
    84  		Run: func(_ *cobra.Command, _ []string) {
    85  			stepTelemetryData := telemetry.CustomData{}
    86  			stepTelemetryData.ErrorCode = "1"
    87  			handler := func() {
    88  				config.RemoveVaultSecretFiles()
    89  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    90  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    91  				stepTelemetryData.PiperCommitHash = GitCommit
    92  				telemetryClient.SetData(&stepTelemetryData)
    93  				telemetryClient.Send()
    94  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    96  				}
    97  			}
    98  			log.DeferExitHandler(handler)
    99  			defer handler()
   100  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   101  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   102  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   104  					GeneralConfig.HookConfig.SplunkConfig.Token,
   105  					GeneralConfig.HookConfig.SplunkConfig.Index,
   106  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  			}
   108  			cloudFoundryDeleteSpace(stepConfig, &stepTelemetryData)
   109  			stepTelemetryData.ErrorCode = "0"
   110  			log.Entry().Info("SUCCESS")
   111  		},
   112  	}
   113  
   114  	addCloudFoundryDeleteSpaceFlags(createCloudFoundryDeleteSpaceCmd, &stepConfig)
   115  	return createCloudFoundryDeleteSpaceCmd
   116  }
   117  
   118  func addCloudFoundryDeleteSpaceFlags(cmd *cobra.Command, stepConfig *cloudFoundryDeleteSpaceOptions) {
   119  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", `https://api.cf.eu10.hana.ondemand.com`, "Cloud Foundry API endpoint")
   120  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF")
   121  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for Cloud Foundry User")
   122  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org")
   123  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "The name of the Cloud Foundry Space to be deleted")
   124  
   125  	cmd.MarkFlagRequired("cfApiEndpoint")
   126  	cmd.MarkFlagRequired("username")
   127  	cmd.MarkFlagRequired("password")
   128  	cmd.MarkFlagRequired("cfOrg")
   129  	cmd.MarkFlagRequired("cfSpace")
   130  }
   131  
   132  // retrieve step metadata
   133  func cloudFoundryDeleteSpaceMetadata() config.StepData {
   134  	var theMetaData = config.StepData{
   135  		Metadata: config.StepMetadata{
   136  			Name:        "cloudFoundryDeleteSpace",
   137  			Aliases:     []config.Alias{},
   138  			Description: "Deletes a space in Cloud Foundry",
   139  		},
   140  		Spec: config.StepSpec{
   141  			Inputs: config.StepInputs{
   142  				Secrets: []config.StepSecrets{
   143  					{Name: "cfCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cloudFoundry/credentialsId", Deprecated: false}}},
   144  				},
   145  				Resources: []config.StepResources{
   146  					{Name: "deployDescriptor", Type: "stash"},
   147  				},
   148  				Parameters: []config.StepParameters{
   149  					{
   150  						Name:        "cfApiEndpoint",
   151  						ResourceRef: []config.ResourceReference{},
   152  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   153  						Type:        "string",
   154  						Mandatory:   true,
   155  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   156  						Default:     `https://api.cf.eu10.hana.ondemand.com`,
   157  					},
   158  					{
   159  						Name: "username",
   160  						ResourceRef: []config.ResourceReference{
   161  							{
   162  								Name:  "cfCredentialsId",
   163  								Param: "username",
   164  								Type:  "secret",
   165  							},
   166  						},
   167  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   168  						Type:      "string",
   169  						Mandatory: true,
   170  						Aliases:   []config.Alias{},
   171  						Default:   os.Getenv("PIPER_username"),
   172  					},
   173  					{
   174  						Name: "password",
   175  						ResourceRef: []config.ResourceReference{
   176  							{
   177  								Name:  "cfCredentialsId",
   178  								Param: "password",
   179  								Type:  "secret",
   180  							},
   181  						},
   182  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   183  						Type:      "string",
   184  						Mandatory: true,
   185  						Aliases:   []config.Alias{},
   186  						Default:   os.Getenv("PIPER_password"),
   187  					},
   188  					{
   189  						Name:        "cfOrg",
   190  						ResourceRef: []config.ResourceReference{},
   191  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   192  						Type:        "string",
   193  						Mandatory:   true,
   194  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   195  						Default:     os.Getenv("PIPER_cfOrg"),
   196  					},
   197  					{
   198  						Name:        "cfSpace",
   199  						ResourceRef: []config.ResourceReference{},
   200  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   201  						Type:        "string",
   202  						Mandatory:   true,
   203  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   204  						Default:     os.Getenv("PIPER_cfSpace"),
   205  					},
   206  				},
   207  			},
   208  			Containers: []config.Container{
   209  				{Name: "cf", Image: "ppiper/cf-cli:latest"},
   210  			},
   211  		},
   212  	}
   213  	return theMetaData
   214  }