github.com/SAP/jenkins-library@v1.362.0/cmd/cloudFoundryDeleteService_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 cloudFoundryDeleteServiceOptions 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  	CfDeleteServiceKeys bool   `json:"cfDeleteServiceKeys,omitempty"`
    26  }
    27  
    28  // CloudFoundryDeleteServiceCommand DeleteCloudFoundryService
    29  func CloudFoundryDeleteServiceCommand() *cobra.Command {
    30  	const STEP_NAME = "cloudFoundryDeleteService"
    31  
    32  	metadata := cloudFoundryDeleteServiceMetadata()
    33  	var stepConfig cloudFoundryDeleteServiceOptions
    34  	var startTime time.Time
    35  	var logCollector *log.CollectorHook
    36  	var splunkClient *splunk.Splunk
    37  	telemetryClient := &telemetry.Telemetry{}
    38  
    39  	var createCloudFoundryDeleteServiceCmd = &cobra.Command{
    40  		Use:   STEP_NAME,
    41  		Short: "DeleteCloudFoundryService",
    42  		Long:  `Delete CloudFoundryService`,
    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 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    68  				splunkClient = &splunk.Splunk{}
    69  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    70  				log.RegisterHook(logCollector)
    71  			}
    72  
    73  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    74  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    75  			}
    76  
    77  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    78  			if err != nil {
    79  				return err
    80  			}
    81  			if err = validation.ValidateStruct(stepConfig); err != nil {
    82  				log.SetErrorCategory(log.ErrorConfiguration)
    83  				return err
    84  			}
    85  
    86  			return nil
    87  		},
    88  		Run: func(_ *cobra.Command, _ []string) {
    89  			stepTelemetryData := telemetry.CustomData{}
    90  			stepTelemetryData.ErrorCode = "1"
    91  			handler := func() {
    92  				config.RemoveVaultSecretFiles()
    93  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    94  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    95  				stepTelemetryData.PiperCommitHash = GitCommit
    96  				telemetryClient.SetData(&stepTelemetryData)
    97  				telemetryClient.Send()
    98  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    99  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   100  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   101  						GeneralConfig.HookConfig.SplunkConfig.Token,
   102  						GeneralConfig.HookConfig.SplunkConfig.Index,
   103  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   104  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   105  				}
   106  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   107  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   108  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   109  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   110  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   111  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   112  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   113  				}
   114  			}
   115  			log.DeferExitHandler(handler)
   116  			defer handler()
   117  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   118  			cloudFoundryDeleteService(stepConfig, &stepTelemetryData)
   119  			stepTelemetryData.ErrorCode = "0"
   120  			log.Entry().Info("SUCCESS")
   121  		},
   122  	}
   123  
   124  	addCloudFoundryDeleteServiceFlags(createCloudFoundryDeleteServiceCmd, &stepConfig)
   125  	return createCloudFoundryDeleteServiceCmd
   126  }
   127  
   128  func addCloudFoundryDeleteServiceFlags(cmd *cobra.Command, stepConfig *cloudFoundryDeleteServiceOptions) {
   129  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   130  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF")
   131  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "User Password for CF User")
   132  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "CF org")
   133  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "CF Space")
   134  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter of ServiceInstance Name to delete CloudFoundry Service")
   135  	cmd.Flags().BoolVar(&stepConfig.CfDeleteServiceKeys, "cfDeleteServiceKeys", false, "Parameter to force deletion of Cloud Foundry Service Keys")
   136  
   137  	cmd.MarkFlagRequired("cfApiEndpoint")
   138  	cmd.MarkFlagRequired("username")
   139  	cmd.MarkFlagRequired("password")
   140  	cmd.MarkFlagRequired("cfOrg")
   141  	cmd.MarkFlagRequired("cfSpace")
   142  	cmd.MarkFlagRequired("cfServiceInstance")
   143  }
   144  
   145  // retrieve step metadata
   146  func cloudFoundryDeleteServiceMetadata() config.StepData {
   147  	var theMetaData = config.StepData{
   148  		Metadata: config.StepMetadata{
   149  			Name:        "cloudFoundryDeleteService",
   150  			Aliases:     []config.Alias{},
   151  			Description: "DeleteCloudFoundryService",
   152  		},
   153  		Spec: config.StepSpec{
   154  			Inputs: config.StepInputs{
   155  				Secrets: []config.StepSecrets{
   156  					{Name: "cfCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate to the Cloud Foundry API.", Type: "jenkins"},
   157  				},
   158  				Parameters: []config.StepParameters{
   159  					{
   160  						Name:        "cfApiEndpoint",
   161  						ResourceRef: []config.ResourceReference{},
   162  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   163  						Type:        "string",
   164  						Mandatory:   true,
   165  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   166  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   167  					},
   168  					{
   169  						Name: "username",
   170  						ResourceRef: []config.ResourceReference{
   171  							{
   172  								Name:  "cfCredentialsId",
   173  								Param: "username",
   174  								Type:  "secret",
   175  							},
   176  
   177  							{
   178  								Name:    "cloudfoundryVaultSecretName",
   179  								Type:    "vaultSecret",
   180  								Default: "cloudfoundry-$(org)-$(space)",
   181  							},
   182  						},
   183  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   184  						Type:      "string",
   185  						Mandatory: true,
   186  						Aliases:   []config.Alias{},
   187  						Default:   os.Getenv("PIPER_username"),
   188  					},
   189  					{
   190  						Name: "password",
   191  						ResourceRef: []config.ResourceReference{
   192  							{
   193  								Name:  "cfCredentialsId",
   194  								Param: "password",
   195  								Type:  "secret",
   196  							},
   197  
   198  							{
   199  								Name:    "cloudfoundryVaultSecretName",
   200  								Type:    "vaultSecret",
   201  								Default: "cloudfoundry-$(org)-$(space)",
   202  							},
   203  						},
   204  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   205  						Type:      "string",
   206  						Mandatory: true,
   207  						Aliases:   []config.Alias{},
   208  						Default:   os.Getenv("PIPER_password"),
   209  					},
   210  					{
   211  						Name:        "cfOrg",
   212  						ResourceRef: []config.ResourceReference{},
   213  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   214  						Type:        "string",
   215  						Mandatory:   true,
   216  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   217  						Default:     os.Getenv("PIPER_cfOrg"),
   218  					},
   219  					{
   220  						Name:        "cfSpace",
   221  						ResourceRef: []config.ResourceReference{},
   222  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   223  						Type:        "string",
   224  						Mandatory:   true,
   225  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   226  						Default:     os.Getenv("PIPER_cfSpace"),
   227  					},
   228  					{
   229  						Name:        "cfServiceInstance",
   230  						ResourceRef: []config.ResourceReference{},
   231  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   232  						Type:        "string",
   233  						Mandatory:   true,
   234  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   235  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   236  					},
   237  					{
   238  						Name:        "cfDeleteServiceKeys",
   239  						ResourceRef: []config.ResourceReference{},
   240  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   241  						Type:        "bool",
   242  						Mandatory:   false,
   243  						Aliases:     []config.Alias{{Name: "cloudFoundry/cfDeleteServiceKeys"}},
   244  						Default:     false,
   245  					},
   246  				},
   247  			},
   248  			Containers: []config.Container{
   249  				{Name: "cf", Image: "ppiper/cf-cli:latest", WorkingDir: "/home/piper"},
   250  			},
   251  		},
   252  	}
   253  	return theMetaData
   254  }