You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Add steps for cf space creation and deletion (#2049)
* [wip] Add steps for cf spaces creation and deletion * Add tests * Add cf space creation groovy file * Fix shell script * Fix shell script * Add cloudFoundryCreateSpace command to command line controller * Fix format * Add cloudFoundryDeleteSpace command to command line controller * Refactor cloudFoundryDeleteSpace step * Add cloudFoundryDeleteSpace groovy file * Add missing test * Fix tests * Add docu for cloudFoundryCreateSpace step * Add docu for cloudFoundryDeleteSpace step * Code review changes Co-authored-by: Marcus Holl <marcus.holl@sap.com>
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							55bba0ebbb
						
					
				
				
					commit
					75e696ad7b
				
			
							
								
								
									
										60
									
								
								cmd/cloudFoundryCreateSpace.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								cmd/cloudFoundryCreateSpace.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/cloudfoundry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/command" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| ) | ||||
|  | ||||
| func cloudFoundryCreateSpace(config cloudFoundryCreateSpaceOptions, telemetryData *telemetry.CustomData) { | ||||
|  | ||||
| 	c := command.Command{} | ||||
| 	cf := cloudfoundry.CFUtils{Exec: &command.Command{}} | ||||
|  | ||||
| 	// reroute command output to logging framework | ||||
| 	c.Stdout(log.Writer()) | ||||
| 	c.Stderr(log.Writer()) | ||||
|  | ||||
| 	err := runCloudFoundryCreateSpace(&config, telemetryData, cf, &c) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Entry().WithError(err).Fatal("step execution failed") | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| func runCloudFoundryCreateSpace(config *cloudFoundryCreateSpaceOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, s command.ShellRunner) (err error) { | ||||
|  | ||||
| 	var c = cf.Exec | ||||
|  | ||||
| 	cfLoginError := s.RunShell("/bin/sh", fmt.Sprintf("yes '' | cf login -a %s -u %s -p %s", config.CfAPIEndpoint, config.Username, config.Password)) | ||||
|  | ||||
| 	if cfLoginError != nil { | ||||
| 		return fmt.Errorf("Error while logging in occured: %w", cfLoginError) | ||||
| 	} | ||||
| 	log.Entry().Info("Successfully logged into cloud foundry.") | ||||
|  | ||||
| 	defer func() { | ||||
| 		logoutErr := cf.Logout() | ||||
| 		if logoutErr != nil { | ||||
| 			err = fmt.Errorf("Error while logging out occured: %w", logoutErr) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	log.Entry().Infof("Creating Cloud Foundry Space: '%s'", config.CfSpace) | ||||
|  | ||||
| 	cfCreateSpaceScript := []string{"create-space", config.CfSpace, "-o", config.CfOrg} | ||||
|  | ||||
| 	err = c.RunExecutable("cf", cfCreateSpaceScript...) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("Creating a cf space has failed: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	log.Entry().Info("Cloud foundry space has been created successfully") | ||||
|  | ||||
| 	return err | ||||
| } | ||||
							
								
								
									
										163
									
								
								cmd/cloudFoundryCreateSpace_generated.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								cmd/cloudFoundryCreateSpace_generated.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,163 @@ | ||||
| // Code generated by piper's step-generator. DO NOT EDIT. | ||||
|  | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/config" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type cloudFoundryCreateSpaceOptions struct { | ||||
| 	CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"` | ||||
| 	Username      string `json:"username,omitempty"` | ||||
| 	Password      string `json:"password,omitempty"` | ||||
| 	CfOrg         string `json:"cfOrg,omitempty"` | ||||
| 	CfSpace       string `json:"cfSpace,omitempty"` | ||||
| } | ||||
|  | ||||
| // CloudFoundryCreateSpaceCommand Creates a user defined space in Cloud Foundry | ||||
| func CloudFoundryCreateSpaceCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "cloudFoundryCreateSpace" | ||||
|  | ||||
| 	metadata := cloudFoundryCreateSpaceMetadata() | ||||
| 	var stepConfig cloudFoundryCreateSpaceOptions | ||||
| 	var startTime time.Time | ||||
|  | ||||
| 	var createCloudFoundryCreateSpaceCmd = &cobra.Command{ | ||||
| 		Use:   STEP_NAME, | ||||
| 		Short: "Creates a user defined space in Cloud Foundry", | ||||
| 		Long: `Creates a cf space in Cloud Foundry | ||||
| Mandatory: | ||||
| * Cloud Foundry API endpoint, Organization, name of the Cf space to be created`, | ||||
| 		PreRunE: func(cmd *cobra.Command, _ []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			path, _ := os.Getwd() | ||||
| 			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} | ||||
| 			log.RegisterHook(fatalHook) | ||||
|  | ||||
| 			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) | ||||
| 			if err != nil { | ||||
| 				log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 				return err | ||||
| 			} | ||||
| 			log.RegisterSecret(stepConfig.Username) | ||||
| 			log.RegisterSecret(stepConfig.Password) | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Run: func(_ *cobra.Command, _ []string) { | ||||
| 			telemetryData := telemetry.CustomData{} | ||||
| 			telemetryData.ErrorCode = "1" | ||||
| 			handler := func() { | ||||
| 				telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) | ||||
| 				telemetry.Send(&telemetryData) | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			cloudFoundryCreateSpace(stepConfig, &telemetryData) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addCloudFoundryCreateSpaceFlags(createCloudFoundryCreateSpaceCmd, &stepConfig) | ||||
| 	return createCloudFoundryCreateSpaceCmd | ||||
| } | ||||
|  | ||||
| func addCloudFoundryCreateSpaceFlags(cmd *cobra.Command, stepConfig *cloudFoundryCreateSpaceOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", `https://api.cf.eu10.hana.ondemand.com`, "Cloud Foundry API endpoint") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for Cloud Foundry User") | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org") | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "The name of the Cloud Foundry Space to be created") | ||||
|  | ||||
| 	cmd.MarkFlagRequired("cfApiEndpoint") | ||||
| 	cmd.MarkFlagRequired("username") | ||||
| 	cmd.MarkFlagRequired("password") | ||||
| 	cmd.MarkFlagRequired("cfOrg") | ||||
| 	cmd.MarkFlagRequired("cfSpace") | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func cloudFoundryCreateSpaceMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:    "cloudFoundryCreateSpace", | ||||
| 			Aliases: []config.Alias{}, | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Parameters: []config.StepParameters{ | ||||
| 					{ | ||||
| 						Name:        "cfApiEndpoint", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "username", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "cfCredentialsId", | ||||
| 								Param: "username", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "password", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "cfCredentialsId", | ||||
| 								Param: "password", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "cfOrg", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "cfSpace", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
							
								
								
									
										16
									
								
								cmd/cloudFoundryCreateSpace_generated_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								cmd/cloudFoundryCreateSpace_generated_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestCloudFoundryCreateSpaceCommand(t *testing.T) { | ||||
|  | ||||
| 	testCmd := CloudFoundryCreateSpaceCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procedure | ||||
| 	assert.Equal(t, "cloudFoundryCreateSpace", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
							
								
								
									
										74
									
								
								cmd/cloudFoundryCreateSpace_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								cmd/cloudFoundryCreateSpace_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/cloudfoundry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestCloudFoundryCreateSpace(t *testing.T) { | ||||
| 	m := &mock.ExecMockRunner{} | ||||
| 	s := mock.ShellMockRunner{} | ||||
|  | ||||
| 	cf := cloudfoundry.CFUtils{Exec: m} | ||||
| 	cfUtilsMock := cloudfoundry.CfUtilsMock{} | ||||
| 	var telemetryData telemetry.CustomData | ||||
|  | ||||
| 	config := cloudFoundryCreateSpaceOptions{ | ||||
| 		CfAPIEndpoint: "https://api.endpoint.com", | ||||
| 		CfOrg:         "testOrg", | ||||
| 		CfSpace:       "testSpace", | ||||
| 		Username:      "testUser", | ||||
| 		Password:      "testPassword", | ||||
| 	} | ||||
|  | ||||
| 	t.Run("CF login: Success", func(t *testing.T) { | ||||
|  | ||||
| 		err := runCloudFoundryCreateSpace(&config, &telemetryData, cf, &s) | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.Contains(t, s.Calls[0], "yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword") | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF login: failure case", func(t *testing.T) { | ||||
|  | ||||
| 		errorMessage := "cf login failed" | ||||
|  | ||||
| 		defer func() { | ||||
| 			s.Calls = nil | ||||
| 			s.ShouldFailOnCommand = nil | ||||
| 		}() | ||||
|  | ||||
| 		s.ShouldFailOnCommand = map[string]error{"yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword ": fmt.Errorf(errorMessage)} | ||||
|  | ||||
| 		e := runCloudFoundryCreateSpace(&config, &telemetryData, cf, &s) | ||||
| 		assert.EqualError(t, e, "Error while logging in occured: "+errorMessage) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF space creation: Success", func(t *testing.T) { | ||||
|  | ||||
| 		err := runCloudFoundryCreateSpace(&config, &telemetryData, cf, &s) | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.Equal(t, "cf", m.Calls[0].Exec) | ||||
| 			assert.Equal(t, []string{"create-space", "testSpace", "-o", "testOrg"}, m.Calls[0].Params) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF space creation: FAILURE", func(t *testing.T) { | ||||
|  | ||||
| 		defer cfUtilsMock.Cleanup() | ||||
| 		errorMessage := "cf space creation error" | ||||
|  | ||||
| 		m.ShouldFailOnCommand = map[string]error{"cf create-space testSpace -o testOrg": fmt.Errorf(errorMessage)} | ||||
|  | ||||
| 		cfUtilsMock.LoginError = errors.New(errorMessage) | ||||
|  | ||||
| 		gotError := runCloudFoundryCreateSpace(&config, &telemetryData, cf, &s) | ||||
| 		assert.EqualError(t, gotError, "Creating a cf space has failed: "+errorMessage, "Wrong error message") | ||||
| 	}) | ||||
| } | ||||
							
								
								
									
										60
									
								
								cmd/cloudFoundryDeleteSpace.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								cmd/cloudFoundryDeleteSpace.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/cloudfoundry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/command" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| ) | ||||
|  | ||||
| func cloudFoundryDeleteSpace(config cloudFoundryDeleteSpaceOptions, telemetryData *telemetry.CustomData) { | ||||
|  | ||||
| 	c := command.Command{} | ||||
|  | ||||
| 	// reroute command output to logging framework | ||||
| 	c.Stdout(log.Writer()) | ||||
| 	c.Stderr(log.Writer()) | ||||
|  | ||||
| 	cf := cloudfoundry.CFUtils{ | ||||
| 		Exec: &c, | ||||
| 	} | ||||
|  | ||||
| 	err := runCloudFoundryDeleteSpace(&config, telemetryData, cf, &c) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Entry().WithError(err).Fatal("step execution failed") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func runCloudFoundryDeleteSpace(config *cloudFoundryDeleteSpaceOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, s command.ShellRunner) (err error) { | ||||
| 	var c = cf.Exec | ||||
|  | ||||
| 	cfLoginError := s.RunShell("/bin/sh", fmt.Sprintf("yes '' | cf login -a %s -u %s -p %s", config.CfAPIEndpoint, config.Username, config.Password)) | ||||
|  | ||||
| 	if cfLoginError != nil { | ||||
| 		return fmt.Errorf("Error while logging in occured: %w", cfLoginError) | ||||
| 	} | ||||
|  | ||||
| 	defer func() { | ||||
| 		logoutErr := cf.Logout() | ||||
| 		if logoutErr != nil { | ||||
| 			err = fmt.Errorf("Error while logging out occured: %w", logoutErr) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	log.Entry().Infof("Deleting Cloud Foundry Space: '%s'", config.CfSpace) | ||||
|  | ||||
| 	cfDeleteSpaceScript := []string{"delete-space", config.CfSpace, "-o", config.CfOrg, "-f"} | ||||
|  | ||||
| 	err = c.RunExecutable("cf", cfDeleteSpaceScript...) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("Deletion of cf space has failed: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	log.Entry().Info("Cloud foundry space has been deleted successfully") | ||||
|  | ||||
| 	return err | ||||
| } | ||||
							
								
								
									
										163
									
								
								cmd/cloudFoundryDeleteSpace_generated.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								cmd/cloudFoundryDeleteSpace_generated.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,163 @@ | ||||
| // Code generated by piper's step-generator. DO NOT EDIT. | ||||
|  | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/config" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type cloudFoundryDeleteSpaceOptions struct { | ||||
| 	CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"` | ||||
| 	Username      string `json:"username,omitempty"` | ||||
| 	Password      string `json:"password,omitempty"` | ||||
| 	CfOrg         string `json:"cfOrg,omitempty"` | ||||
| 	CfSpace       string `json:"cfSpace,omitempty"` | ||||
| } | ||||
|  | ||||
| // CloudFoundryDeleteSpaceCommand Deletes a space in Cloud Foundry | ||||
| func CloudFoundryDeleteSpaceCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "cloudFoundryDeleteSpace" | ||||
|  | ||||
| 	metadata := cloudFoundryDeleteSpaceMetadata() | ||||
| 	var stepConfig cloudFoundryDeleteSpaceOptions | ||||
| 	var startTime time.Time | ||||
|  | ||||
| 	var createCloudFoundryDeleteSpaceCmd = &cobra.Command{ | ||||
| 		Use:   STEP_NAME, | ||||
| 		Short: "Deletes a space in Cloud Foundry", | ||||
| 		Long: `Deletes a space in Cloud Foundry | ||||
| Mandatory: | ||||
| * Cloud Foundry API endpoint, Organization, Space name`, | ||||
| 		PreRunE: func(cmd *cobra.Command, _ []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			path, _ := os.Getwd() | ||||
| 			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} | ||||
| 			log.RegisterHook(fatalHook) | ||||
|  | ||||
| 			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) | ||||
| 			if err != nil { | ||||
| 				log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 				return err | ||||
| 			} | ||||
| 			log.RegisterSecret(stepConfig.Username) | ||||
| 			log.RegisterSecret(stepConfig.Password) | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Run: func(_ *cobra.Command, _ []string) { | ||||
| 			telemetryData := telemetry.CustomData{} | ||||
| 			telemetryData.ErrorCode = "1" | ||||
| 			handler := func() { | ||||
| 				telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) | ||||
| 				telemetry.Send(&telemetryData) | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			cloudFoundryDeleteSpace(stepConfig, &telemetryData) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addCloudFoundryDeleteSpaceFlags(createCloudFoundryDeleteSpaceCmd, &stepConfig) | ||||
| 	return createCloudFoundryDeleteSpaceCmd | ||||
| } | ||||
|  | ||||
| func addCloudFoundryDeleteSpaceFlags(cmd *cobra.Command, stepConfig *cloudFoundryDeleteSpaceOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", `https://api.cf.eu10.hana.ondemand.com`, "Cloud Foundry API endpoint") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for Cloud Foundry User") | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org") | ||||
| 	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "The name of the Cloud Foundry Space to be deleted") | ||||
|  | ||||
| 	cmd.MarkFlagRequired("cfApiEndpoint") | ||||
| 	cmd.MarkFlagRequired("username") | ||||
| 	cmd.MarkFlagRequired("password") | ||||
| 	cmd.MarkFlagRequired("cfOrg") | ||||
| 	cmd.MarkFlagRequired("cfSpace") | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func cloudFoundryDeleteSpaceMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:    "cloudFoundryDeleteSpace", | ||||
| 			Aliases: []config.Alias{}, | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Parameters: []config.StepParameters{ | ||||
| 					{ | ||||
| 						Name:        "cfApiEndpoint", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "username", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "cfCredentialsId", | ||||
| 								Param: "username", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "password", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "cfCredentialsId", | ||||
| 								Param: "password", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "cfOrg", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "cfSpace", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
							
								
								
									
										16
									
								
								cmd/cloudFoundryDeleteSpace_generated_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								cmd/cloudFoundryDeleteSpace_generated_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestCloudFoundryDeleteSpaceCommand(t *testing.T) { | ||||
|  | ||||
| 	testCmd := CloudFoundryDeleteSpaceCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procedure | ||||
| 	assert.Equal(t, "cloudFoundryDeleteSpace", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
							
								
								
									
										68
									
								
								cmd/cloudFoundryDeleteSpace_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								cmd/cloudFoundryDeleteSpace_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/cloudfoundry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestCloudFoundryDeleteSpace(t *testing.T) { | ||||
| 	m := &mock.ExecMockRunner{} | ||||
| 	s := mock.ShellMockRunner{} | ||||
| 	cf := cloudfoundry.CFUtils{Exec: m} | ||||
| 	var telemetryData telemetry.CustomData | ||||
|  | ||||
| 	config := cloudFoundryDeleteSpaceOptions{ | ||||
| 		CfAPIEndpoint: "https://api.endpoint.com", | ||||
| 		CfOrg:         "testOrg", | ||||
| 		CfSpace:       "testSpace", | ||||
| 		Username:      "testUser", | ||||
| 		Password:      "testPassword", | ||||
| 	} | ||||
|  | ||||
| 	t.Run("CF login: Success", func(t *testing.T) { | ||||
|  | ||||
| 		err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.Contains(t, s.Calls[0], "yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword") | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF login: failure case", func(t *testing.T) { | ||||
|  | ||||
| 		errorMessage := "cf login failed" | ||||
|  | ||||
| 		defer func() { | ||||
| 			s.Calls = nil | ||||
| 			s.ShouldFailOnCommand = nil | ||||
| 		}() | ||||
|  | ||||
| 		s.ShouldFailOnCommand = map[string]error{"yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword ": fmt.Errorf(errorMessage)} | ||||
|  | ||||
| 		e := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) | ||||
| 		assert.EqualError(t, e, "Error while logging in occured: "+errorMessage) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF Delete Space : success case", func(t *testing.T) { | ||||
|  | ||||
| 		err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.Equal(t, "cf", m.Calls[0].Exec) | ||||
| 			assert.Equal(t, []string{"delete-space", "testSpace", "-o", "testOrg", "-f"}, m.Calls[0].Params) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CF Delete space: failure case", func(t *testing.T) { | ||||
|  | ||||
| 		errorMessage := "cf space creation error" | ||||
|  | ||||
| 		m.ShouldFailOnCommand = map[string]error{"cf delete-space testSpace -o testOrg -f": fmt.Errorf(errorMessage)} | ||||
|  | ||||
| 		gotError := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) | ||||
| 		assert.EqualError(t, gotError, "Deletion of cf space has failed: "+errorMessage, "Wrong error message") | ||||
| 	}) | ||||
| } | ||||
| @@ -112,6 +112,8 @@ func Execute() { | ||||
| 	rootCmd.AddCommand(AbapAddonAssemblyKitRegisterPackagesCommand()) | ||||
| 	rootCmd.AddCommand(AbapAddonAssemblyKitReleasePackagesCommand()) | ||||
| 	rootCmd.AddCommand(AbapAddonAssemblyKitReserveNextPackagesCommand()) | ||||
| 	rootCmd.AddCommand(CloudFoundryCreateSpaceCommand()) | ||||
| 	rootCmd.AddCommand(CloudFoundryDeleteSpaceCommand()) | ||||
|  | ||||
| 	addRootFlags(rootCmd) | ||||
| 	if err := rootCmd.Execute(); err != nil { | ||||
|   | ||||
							
								
								
									
										34
									
								
								documentation/docs/steps/cloudFoundryCreateSpace.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								documentation/docs/steps/cloudFoundryCreateSpace.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| # ${docGenStepName} | ||||
|  | ||||
| ## ${docGenDescription} | ||||
|  | ||||
| ## Prerequisites | ||||
|  | ||||
| * You have a user for the SAP Cloud Platform Cloud Foundry Environment | ||||
| * Credentials have been configured in Jenkins with a dedicated Id | ||||
|  | ||||
| ## ${docGenParameters} | ||||
|  | ||||
| ## ${docGenConfiguration} | ||||
|  | ||||
| ## ${docJenkinsPluginDependencies} | ||||
|  | ||||
| ## Example | ||||
|  | ||||
| ### Space Creation in Cloud Foundry with a simple example | ||||
|  | ||||
| The following example creates an user defined space in a Cloud Foundry. | ||||
|  | ||||
| You can store the credentials in Jenkins and use the `cfCredentialsId` parameter to authenticate to Cloud Foundry. | ||||
|  | ||||
| This can be done accordingly: | ||||
|  | ||||
| ```groovy | ||||
| cloudFoundryCreateSpace( | ||||
|     cfApiEndpoint : 'https://test.server.com', | ||||
|     cfOrg : 'cfOrg', | ||||
|     cfSpace: 'cfSpace', //Name of the cf space to be created | ||||
|     cfCredentialsId: 'cfCredentialsId', | ||||
|     script: this, | ||||
| ) | ||||
| ``` | ||||
							
								
								
									
										34
									
								
								documentation/docs/steps/cloudFoundryDeleteSpace.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								documentation/docs/steps/cloudFoundryDeleteSpace.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| # ${docGenStepName} | ||||
|  | ||||
| ## ${docGenDescription} | ||||
|  | ||||
| ## Prerequisites | ||||
|  | ||||
| * You have a user for the SAP Cloud Platform Cloud Foundry Environment | ||||
| * Credentials have been configured in Jenkins with a dedicated Id | ||||
|  | ||||
| ## ${docGenParameters} | ||||
|  | ||||
| ## ${docGenConfiguration} | ||||
|  | ||||
| ## ${docJenkinsPluginDependencies} | ||||
|  | ||||
| ## Example | ||||
|  | ||||
| ### Space deletion in Cloud Foundry with a simple example | ||||
|  | ||||
| The following example deletes an existing space in a Cloud Foundry. | ||||
|  | ||||
| You can store the credentials in Jenkins and use the `cfCredentialsId` parameter to authenticate to Cloud Foundry. | ||||
|  | ||||
| This can be done accordingly: | ||||
|  | ||||
| ```groovy | ||||
| cloudFoundryDeleteSpace( | ||||
|     cfApiEndpoint : 'https://test.server.com', | ||||
|     cfOrg : 'cfOrg', | ||||
|     cfSpace: 'cfSpace', //Name of the cf space to be deleted | ||||
|     cfCredentialsId: 'cfCredentialsId', | ||||
|     script: this, | ||||
| ) | ||||
| ``` | ||||
							
								
								
									
										84
									
								
								resources/metadata/cloudFoundryCreateSpace.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								resources/metadata/cloudFoundryCreateSpace.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | ||||
| metadata: | ||||
|   name: cloudFoundryCreateSpace | ||||
|   description: Creates a user defined space in Cloud Foundry | ||||
|   longDescription: | | ||||
|     Creates a cf space in Cloud Foundry | ||||
|     Mandatory: | ||||
|     * Cloud Foundry API endpoint, Organization, name of the Cf space to be created | ||||
|  | ||||
| spec: | ||||
|   inputs: | ||||
|     secrets: | ||||
|       - name: cfCredentialsId | ||||
|         description: Jenkins credentials ID containing user and password to authenticate to the Cloud Foundry API | ||||
|         type: jenkins | ||||
|         aliases: | ||||
|           - name: cloudFoundry/credentialsId | ||||
|     resources: | ||||
|       - name: deployDescriptor | ||||
|         type: stash | ||||
|     params: | ||||
|       - name: cfApiEndpoint | ||||
|         type: string | ||||
|         description: Cloud Foundry API endpoint | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/apiEndpoint | ||||
|         default: "https://api.cf.eu10.hana.ondemand.com" | ||||
|       - name: username | ||||
|         type: string | ||||
|         description: User or E-Mail for CF | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: cfCredentialsId | ||||
|             type: secret | ||||
|             param: username | ||||
|       - name: password | ||||
|         type: string | ||||
|         description: Password for Cloud Foundry User | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: cfCredentialsId | ||||
|             type: secret | ||||
|             param: password | ||||
|       - name: cfOrg | ||||
|         type: string | ||||
|         description: Cloud Foundry org | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/org | ||||
|       - name: cfSpace | ||||
|         type: string | ||||
|         description: The name of the Cloud Foundry Space to be created | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/space | ||||
|   containers: | ||||
|     - name: cf | ||||
|       image: ppiper/cf-cli | ||||
|       imagePullPolicy: Never | ||||
							
								
								
									
										84
									
								
								resources/metadata/cloudFoundryDeleteSpace.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								resources/metadata/cloudFoundryDeleteSpace.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | ||||
| metadata: | ||||
|   name: cloudFoundryDeleteSpace | ||||
|   description: Deletes a space in Cloud Foundry | ||||
|   longDescription: | | ||||
|     Deletes a space in Cloud Foundry | ||||
|     Mandatory: | ||||
|     * Cloud Foundry API endpoint, Organization, Space name | ||||
|  | ||||
| spec: | ||||
|   inputs: | ||||
|     secrets: | ||||
|       - name: cfCredentialsId | ||||
|         description: Jenkins credentials ID containing user and password to authenticate to the Cloud Foundry API | ||||
|         type: jenkins | ||||
|         aliases: | ||||
|           - name: cloudFoundry/credentialsId | ||||
|     resources: | ||||
|       - name: deployDescriptor | ||||
|         type: stash | ||||
|     params: | ||||
|       - name: cfApiEndpoint | ||||
|         type: string | ||||
|         description: Cloud Foundry API endpoint | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/apiEndpoint | ||||
|         default: "https://api.cf.eu10.hana.ondemand.com" | ||||
|       - name: username | ||||
|         type: string | ||||
|         description: User or E-Mail for CF | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: cfCredentialsId | ||||
|             type: secret | ||||
|             param: username | ||||
|       - name: password | ||||
|         type: string | ||||
|         description: Password for Cloud Foundry User | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: true | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: cfCredentialsId | ||||
|             type: secret | ||||
|             param: password | ||||
|       - name: cfOrg | ||||
|         type: string | ||||
|         description: Cloud Foundry org | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/org | ||||
|       - name: cfSpace | ||||
|         type: string | ||||
|         description: The name of the Cloud Foundry Space to be deleted | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: cloudFoundry/space | ||||
|   containers: | ||||
|     - name: cf | ||||
|       image: ppiper/cf-cli | ||||
|       imagePullPolicy: Never | ||||
| @@ -122,7 +122,9 @@ public class CommonStepsTest extends BasePiperTest{ | ||||
|         'artifactPrepareVersion', | ||||
|         'cloudFoundryCreateService', //implementing new golang pattern without fields | ||||
|         'cloudFoundryCreateServiceKey', //implementing new golang pattern without fields | ||||
|         'cloudFoundryCreateSpace', //implementing new golang pattern without fields | ||||
|         'cloudFoundryDeleteService', //implementing new golang pattern without fields | ||||
|         'cloudFoundryDeleteSpace', //implementing new golang pattern without fields | ||||
|         'durationMeasure', // only expects parameters via signature | ||||
|         'prepareDefaultValues', // special step (infrastructure) | ||||
|         'piperPipeline', // special step (infrastructure) | ||||
|   | ||||
							
								
								
									
										11
									
								
								vars/cloudFoundryCreateSpace.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								vars/cloudFoundryCreateSpace.groovy
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import groovy.transform.Field | ||||
|  | ||||
| @Field String STEP_NAME = getClass().getName() | ||||
| @Field String METADATA_FILE = 'metadata/cloudFoundryCreateSpace.yaml' | ||||
|  | ||||
| void call(Map parameters = [:]) { | ||||
|     List credentials = [ | ||||
|         [type: 'usernamePassword', id: 'cfCredentialsId', env: ['PIPER_username', 'PIPER_password']] | ||||
|     ] | ||||
|     piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials, false, false, true) | ||||
| } | ||||
							
								
								
									
										11
									
								
								vars/cloudFoundryDeleteSpace.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								vars/cloudFoundryDeleteSpace.groovy
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import groovy.transform.Field | ||||
|  | ||||
| @Field String STEP_NAME = getClass().getName() | ||||
| @Field String METADATA_FILE = 'metadata/cloudFoundryDeleteSpace.yaml' | ||||
|  | ||||
| void call(Map parameters = [:]) { | ||||
|     List credentials = [ | ||||
|         [type: 'usernamePassword', id: 'cfCredentialsId', env: ['PIPER_username', 'PIPER_password']] | ||||
|     ] | ||||
|     piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials, false, false, true) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user