mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-28 05:47:08 +02:00
Add step abapEnvironmentCreateSystem (#2273)
* Create initial step * Add parameters for system creation * Creating a tmp manifest.yml * Add descriptions * regenerate * Create tests * fix codeclimate issue * Test * Test2 * Test3 * Replace os.getTempDir * Change to fileUtils * Remove FileUtil
This commit is contained in:
parent
7946265e21
commit
aa9dd3b199
164
cmd/abapEnvironmentCreateSystem.go
Normal file
164
cmd/abapEnvironmentCreateSystem.go
Normal file
@ -0,0 +1,164 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/abaputils"
|
||||
"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"
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func abapEnvironmentCreateSystem(config abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData) {
|
||||
|
||||
cf := cloudfoundry.CFUtils{Exec: &command.Command{}}
|
||||
u := &googleUUID{}
|
||||
|
||||
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
|
||||
err := runAbapEnvironmentCreateSystem(&config, telemetryData, cf, u)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("step execution failed")
|
||||
}
|
||||
}
|
||||
|
||||
func runAbapEnvironmentCreateSystem(config *abapEnvironmentCreateSystemOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, u uuidGenerator) error {
|
||||
|
||||
if config.ServiceManifest != "" {
|
||||
// if the manifest file is provided, it is directly passed through to cloudFoundryCreateService
|
||||
createServiceConfig := cloudFoundryCreateServiceOptions{
|
||||
CfAPIEndpoint: config.CfAPIEndpoint,
|
||||
CfOrg: config.CfOrg,
|
||||
CfSpace: config.CfSpace,
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
ServiceManifest: config.ServiceManifest,
|
||||
}
|
||||
runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
|
||||
} else {
|
||||
// if no manifest file is provided, it is created with the provided config values
|
||||
manifestYAML, err := generateManifestYAML(config)
|
||||
|
||||
// writing the yaml into a temporary file
|
||||
path, _ := os.Getwd()
|
||||
path = path + "/generated_service_manifest-" + u.getUUID() + ".yml"
|
||||
log.Entry().Debugf("Path: %s", path)
|
||||
err = ioutil.WriteFile(path, manifestYAML, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", "Could not generate manifest file for the cloud foundry cli", err)
|
||||
}
|
||||
|
||||
defer os.Remove(path)
|
||||
|
||||
// Calling cloudFoundryCreateService with the respective parameters
|
||||
createServiceConfig := cloudFoundryCreateServiceOptions{
|
||||
CfAPIEndpoint: config.CfAPIEndpoint,
|
||||
CfOrg: config.CfOrg,
|
||||
CfSpace: config.CfSpace,
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
ServiceManifest: path,
|
||||
}
|
||||
runCloudFoundryCreateService(&createServiceConfig, telemetryData, cf)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateManifestYAML(config *abapEnvironmentCreateSystemOptions) ([]byte, error) {
|
||||
addonProduct := ""
|
||||
addonVersion := ""
|
||||
if config.AddonDescriptorFileName != "" {
|
||||
descriptor, err := abaputils.ReadAddonDescriptor(config.AddonDescriptorFileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cloud not read addonProduct and addonVersion from %s: %w", config.AddonDescriptorFileName, err)
|
||||
}
|
||||
addonProduct = descriptor.AddonProduct
|
||||
addonVersion = descriptor.AddonVersionYAML
|
||||
}
|
||||
params := abapSystemParameters{
|
||||
AdminEmail: config.AdminEmail,
|
||||
Description: config.Description,
|
||||
IsDevelopmentAllowed: config.IsDevelopmentAllowed,
|
||||
SapSystemName: config.SapSystemName,
|
||||
SizeOfPersistence: config.SizeOfPersistence,
|
||||
SizeOfRuntime: config.SizeOfRuntime,
|
||||
AddonProductName: addonProduct,
|
||||
AddonProductVersion: addonVersion,
|
||||
}
|
||||
|
||||
serviceParameters, err := json.Marshal(params)
|
||||
serviceParametersString := string(serviceParameters)
|
||||
log.Entry().Debugf("Service Parameters: %s", serviceParametersString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not generate parameter string for the cloud foundry cli: %w", err)
|
||||
}
|
||||
|
||||
/*
|
||||
Generating the temporary manifest yaml file
|
||||
*/
|
||||
service := Service{
|
||||
Name: config.CfServiceInstance,
|
||||
Broker: config.CfService,
|
||||
Plan: config.CfServicePlan,
|
||||
Parameters: serviceParametersString,
|
||||
}
|
||||
|
||||
serviceManifest := serviceManifest{CreateServices: []Service{service}}
|
||||
errorMessage := "Could not generate manifest for the cloud foundry cli"
|
||||
|
||||
// converting the golang structure to json
|
||||
manifestJSON, err := json.Marshal(serviceManifest)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", errorMessage, err)
|
||||
}
|
||||
|
||||
// converting the json to yaml
|
||||
manifestYAML, err := yaml.JSONToYAML(manifestJSON)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", errorMessage, err)
|
||||
}
|
||||
|
||||
log.Entry().Debug(string(manifestYAML))
|
||||
|
||||
return manifestYAML, nil
|
||||
}
|
||||
|
||||
type abapSystemParameters struct {
|
||||
AdminEmail string `json:"admin_email,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
IsDevelopmentAllowed bool `json:"is_development_allowed,omitempty"`
|
||||
SapSystemName string `json:"sapsystemname,omitempty"`
|
||||
SizeOfPersistence int `json:"size_of_persistence,omitempty"`
|
||||
SizeOfRuntime int `json:"size_of_runtime,omitempty"`
|
||||
AddonProductName string `json:"addon_product_name,omitempty"`
|
||||
AddonProductVersion string `json:"addon_product_version,omitempty"`
|
||||
}
|
||||
|
||||
type serviceManifest struct {
|
||||
CreateServices []Service `json:"create-services"`
|
||||
}
|
||||
|
||||
// Service struct for creating a cloud foundry service
|
||||
type Service struct {
|
||||
Name string `json:"name"`
|
||||
Broker string `json:"broker"`
|
||||
Plan string `json:"plan"`
|
||||
Parameters string `json:"parameters,omitempty"`
|
||||
}
|
||||
|
||||
type uuidGenerator interface {
|
||||
getUUID() string
|
||||
}
|
||||
|
||||
type googleUUID struct {
|
||||
}
|
||||
|
||||
func (u *googleUUID) getUUID() string {
|
||||
return uuid.New().String()
|
||||
}
|
285
cmd/abapEnvironmentCreateSystem_generated.go
Normal file
285
cmd/abapEnvironmentCreateSystem_generated.go
Normal file
@ -0,0 +1,285 @@
|
||||
// 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 abapEnvironmentCreateSystemOptions 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"`
|
||||
CfService string `json:"cfService,omitempty"`
|
||||
CfServicePlan string `json:"cfServicePlan,omitempty"`
|
||||
CfServiceInstance string `json:"cfServiceInstance,omitempty"`
|
||||
ServiceManifest string `json:"serviceManifest,omitempty"`
|
||||
AdminEmail string `json:"adminEmail,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
IsDevelopmentAllowed bool `json:"isDevelopmentAllowed,omitempty"`
|
||||
SapSystemName string `json:"sapSystemName,omitempty"`
|
||||
SizeOfPersistence int `json:"sizeOfPersistence,omitempty"`
|
||||
SizeOfRuntime int `json:"sizeOfRuntime,omitempty"`
|
||||
AddonDescriptorFileName string `json:"addonDescriptorFileName,omitempty"`
|
||||
}
|
||||
|
||||
// AbapEnvironmentCreateSystemCommand Creates a SAP Cloud Platform ABAP Environment system (aka Steampunk system)
|
||||
func AbapEnvironmentCreateSystemCommand() *cobra.Command {
|
||||
const STEP_NAME = "abapEnvironmentCreateSystem"
|
||||
|
||||
metadata := abapEnvironmentCreateSystemMetadata()
|
||||
var stepConfig abapEnvironmentCreateSystemOptions
|
||||
var startTime time.Time
|
||||
|
||||
var createAbapEnvironmentCreateSystemCmd = &cobra.Command{
|
||||
Use: STEP_NAME,
|
||||
Short: "Creates a SAP Cloud Platform ABAP Environment system (aka Steampunk system)",
|
||||
Long: `creates a SAP Cloud Platform ABAP Environment system (aka Steampunk system)`,
|
||||
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() {
|
||||
config.RemoveVaultSecretFiles()
|
||||
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
||||
telemetryData.ErrorCategory = log.GetErrorCategory().String()
|
||||
telemetry.Send(&telemetryData)
|
||||
}
|
||||
log.DeferExitHandler(handler)
|
||||
defer handler()
|
||||
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
||||
abapEnvironmentCreateSystem(stepConfig, &telemetryData)
|
||||
telemetryData.ErrorCode = "0"
|
||||
log.Entry().Info("SUCCESS")
|
||||
},
|
||||
}
|
||||
|
||||
addAbapEnvironmentCreateSystemFlags(createAbapEnvironmentCreateSystemCmd, &stepConfig)
|
||||
return createAbapEnvironmentCreateSystemCmd
|
||||
}
|
||||
|
||||
func addAbapEnvironmentCreateSystemFlags(cmd *cobra.Command, stepConfig *abapEnvironmentCreateSystemOptions) {
|
||||
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"), "Cloud Foundry Space")
|
||||
cmd.Flags().StringVar(&stepConfig.CfService, "cfService", os.Getenv("PIPER_cfService"), "Parameter for Cloud Foundry Service to be used for creating Cloud Foundry Service")
|
||||
cmd.Flags().StringVar(&stepConfig.CfServicePlan, "cfServicePlan", os.Getenv("PIPER_cfServicePlan"), "Parameter for Cloud Foundry Service Plan to be used when creating a Cloud Foundry Service")
|
||||
cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter for naming the Service Instance when creating a Cloud Foundry Service")
|
||||
cmd.Flags().StringVar(&stepConfig.ServiceManifest, "serviceManifest", os.Getenv("PIPER_serviceManifest"), "Path to Cloud Foundry Service Manifest in YAML format for multiple service creations that are being passed to a Create-Service-Push Cloud Foundry cli plugin")
|
||||
cmd.Flags().StringVar(&stepConfig.AdminEmail, "adminEmail", os.Getenv("PIPER_adminEmail"), "Admin E-Mail address for the initial administrator of the system")
|
||||
cmd.Flags().StringVar(&stepConfig.Description, "description", `Test system created by an automated pipeline`, "Description for the ABAP Environment system")
|
||||
cmd.Flags().BoolVar(&stepConfig.IsDevelopmentAllowed, "isDevelopmentAllowed", true, "This parameter determines, if development is allowed on the system")
|
||||
cmd.Flags().StringVar(&stepConfig.SapSystemName, "sapSystemName", `H02`, "The three character name of the system")
|
||||
cmd.Flags().IntVar(&stepConfig.SizeOfPersistence, "sizeOfPersistence", 0, "The size of the persistence")
|
||||
cmd.Flags().IntVar(&stepConfig.SizeOfRuntime, "sizeOfRuntime", 0, "The size of the runtime")
|
||||
cmd.Flags().StringVar(&stepConfig.AddonDescriptorFileName, "addonDescriptorFileName", os.Getenv("PIPER_addonDescriptorFileName"), "The file name of the addonDescriptor")
|
||||
|
||||
cmd.MarkFlagRequired("cfApiEndpoint")
|
||||
cmd.MarkFlagRequired("username")
|
||||
cmd.MarkFlagRequired("password")
|
||||
cmd.MarkFlagRequired("cfOrg")
|
||||
cmd.MarkFlagRequired("cfSpace")
|
||||
}
|
||||
|
||||
// retrieve step metadata
|
||||
func abapEnvironmentCreateSystemMetadata() config.StepData {
|
||||
var theMetaData = config.StepData{
|
||||
Metadata: config.StepMetadata{
|
||||
Name: "abapEnvironmentCreateSystem",
|
||||
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",
|
||||
},
|
||||
|
||||
{
|
||||
Name: "",
|
||||
Paths: []string{"$(vaultPath)/cloudfoundry-$(cfOrg)-$(cfSpace)", "$(vaultBasePath)/$(vaultPipelineName)/cloudfoundry-$(cfOrg)-$(cfSpace)", "$(vaultBasePath)/GROUP-SECRETS/cloudfoundry-$(cfOrg)-$(cfSpace)"},
|
||||
Type: "vaultSecret",
|
||||
},
|
||||
},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "string",
|
||||
Mandatory: true,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
ResourceRef: []config.ResourceReference{
|
||||
{
|
||||
Name: "cfCredentialsId",
|
||||
Param: "password",
|
||||
Type: "secret",
|
||||
},
|
||||
|
||||
{
|
||||
Name: "",
|
||||
Paths: []string{"$(vaultPath)/cloudfoundry-$(cfOrg)-$(cfSpace)", "$(vaultBasePath)/$(vaultPipelineName)/cloudfoundry-$(cfOrg)-$(cfSpace)", "$(vaultBasePath)/GROUP-SECRETS/cloudfoundry-$(cfOrg)-$(cfSpace)"},
|
||||
Type: "vaultSecret",
|
||||
},
|
||||
},
|
||||
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"}},
|
||||
},
|
||||
{
|
||||
Name: "cfService",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{{Name: "cloudFoundry/service"}},
|
||||
},
|
||||
{
|
||||
Name: "cfServicePlan",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{{Name: "cloudFoundry/servicePlan"}},
|
||||
},
|
||||
{
|
||||
Name: "cfServiceInstance",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
|
||||
},
|
||||
{
|
||||
Name: "serviceManifest",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{{Name: "cloudFoundry/serviceManifest"}, {Name: "cfServiceManifest"}},
|
||||
},
|
||||
{
|
||||
Name: "adminEmail",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "description",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "isDevelopmentAllowed",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "sapSystemName",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "sizeOfPersistence",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "int",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "sizeOfRuntime",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "int",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
{
|
||||
Name: "addonDescriptorFileName",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return theMetaData
|
||||
}
|
16
cmd/abapEnvironmentCreateSystem_generated_test.go
Normal file
16
cmd/abapEnvironmentCreateSystem_generated_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAbapEnvironmentCreateSystemCommand(t *testing.T) {
|
||||
|
||||
testCmd := AbapEnvironmentCreateSystemCommand()
|
||||
|
||||
// only high level testing performed - details are tested in step generation procedure
|
||||
assert.Equal(t, "abapEnvironmentCreateSystem", testCmd.Use, "command name incorrect")
|
||||
|
||||
}
|
155
cmd/abapEnvironmentCreateSystem_test.go
Normal file
155
cmd/abapEnvironmentCreateSystem_test.go
Normal file
@ -0,0 +1,155 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
|
||||
"github.com/SAP/jenkins-library/pkg/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRunAbapEnvironmentCreateSystem(t *testing.T) {
|
||||
m := &mock.ExecMockRunner{}
|
||||
cf := cloudfoundry.CFUtils{Exec: m}
|
||||
u := &uuidMock{}
|
||||
|
||||
t.Run("Create service with generated manifest", func(t *testing.T) {
|
||||
defer cfMockCleanup(m)
|
||||
config := abapEnvironmentCreateSystemOptions{
|
||||
CfAPIEndpoint: "https://api.endpoint.com",
|
||||
CfOrg: "testOrg",
|
||||
CfSpace: "testSpace",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
CfService: "testService",
|
||||
CfServiceInstance: "testName",
|
||||
CfServicePlan: "testPlan",
|
||||
}
|
||||
wd, _ := os.Getwd()
|
||||
err := runAbapEnvironmentCreateSystem(&config, nil, cf, u)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, []mock.ExecCall{
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", wd + "/generated_service_manifest-my-uuid.yml"}},
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
|
||||
m.Calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Create service with mainfest", func(t *testing.T) {
|
||||
defer cfMockCleanup(m)
|
||||
config := abapEnvironmentCreateSystemOptions{
|
||||
CfAPIEndpoint: "https://api.endpoint.com",
|
||||
CfOrg: "testOrg",
|
||||
CfSpace: "testSpace",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
ServiceManifest: "customManifest.yml",
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "test variable substitution")
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create temporary directory")
|
||||
}
|
||||
oldCWD, _ := os.Getwd()
|
||||
_ = os.Chdir(dir)
|
||||
// clean up tmp dir
|
||||
defer func() {
|
||||
_ = os.Chdir(oldCWD)
|
||||
_ = os.RemoveAll(dir)
|
||||
}()
|
||||
|
||||
manifestFileString := `
|
||||
---
|
||||
create-services:
|
||||
- name: ((name))
|
||||
broker: "testBroker"
|
||||
plan: "testPlan"
|
||||
|
||||
- name: ((name2))
|
||||
broker: "testBroker"
|
||||
plan: "testPlan"
|
||||
|
||||
- name: "test3"
|
||||
broker: "testBroker"
|
||||
plan: "testPlan"`
|
||||
|
||||
manifestFileStringBody := []byte(manifestFileString)
|
||||
err = ioutil.WriteFile("customManifest.yml", manifestFileStringBody, 0644)
|
||||
|
||||
err = runAbapEnvironmentCreateSystem(&config, nil, cf, u)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, []mock.ExecCall{
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}},
|
||||
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
|
||||
m.Calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestManifestGeneration(t *testing.T) {
|
||||
|
||||
t.Run("Create service with generated manifest", func(t *testing.T) {
|
||||
config := abapEnvironmentCreateSystemOptions{
|
||||
CfAPIEndpoint: "https://api.endpoint.com",
|
||||
CfOrg: "testOrg",
|
||||
CfSpace: "testSpace",
|
||||
Username: "testUser",
|
||||
Password: "testPassword",
|
||||
CfService: "testService",
|
||||
CfServiceInstance: "testName",
|
||||
CfServicePlan: "testPlan",
|
||||
AdminEmail: "user@example.com",
|
||||
SapSystemName: "H02",
|
||||
IsDevelopmentAllowed: true,
|
||||
SizeOfPersistence: 4,
|
||||
SizeOfRuntime: 4,
|
||||
AddonDescriptorFileName: "addon.yml",
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "test variable substitution")
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create temporary directory")
|
||||
}
|
||||
oldCWD, _ := os.Getwd()
|
||||
_ = os.Chdir(dir)
|
||||
// clean up tmp dir
|
||||
defer func() {
|
||||
_ = os.Chdir(oldCWD)
|
||||
_ = os.RemoveAll(dir)
|
||||
}()
|
||||
|
||||
addonYML := `addonProduct: myProduct
|
||||
addonVersion: 1.2.3
|
||||
repositories:
|
||||
- name: '/DMO/REPO'
|
||||
`
|
||||
|
||||
addonYMLBytes := []byte(addonYML)
|
||||
err = ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)
|
||||
|
||||
expectedResult := `create-services:
|
||||
- broker: testService
|
||||
name: testName
|
||||
parameters: '{"admin_email":"user@example.com","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4,"addon_product_name":"myProduct","addon_product_version":"1.2.3"}'
|
||||
plan: testPlan
|
||||
`
|
||||
|
||||
resultBytes, err := generateManifestYAML(&config)
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
result := string(resultBytes)
|
||||
assert.Equal(t, expectedResult, result, "Result not as expected")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type uuidMock struct {
|
||||
}
|
||||
|
||||
func (u *uuidMock) getUUID() string {
|
||||
return "my-uuid"
|
||||
}
|
@ -81,6 +81,7 @@ func Execute() {
|
||||
rootCmd.AddCommand(AbapEnvironmentPullGitRepoCommand())
|
||||
rootCmd.AddCommand(AbapEnvironmentCloneGitRepoCommand())
|
||||
rootCmd.AddCommand(AbapEnvironmentCheckoutBranchCommand())
|
||||
rootCmd.AddCommand(AbapEnvironmentCreateSystemCommand())
|
||||
rootCmd.AddCommand(CheckmarxExecuteScanCommand())
|
||||
rootCmd.AddCommand(FortifyExecuteScanCommand())
|
||||
rootCmd.AddCommand(MtaBuildCommand())
|
||||
|
@ -4,13 +4,14 @@ package mock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
"github.com/bmatcuk/doublestar"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
"github.com/bmatcuk/doublestar"
|
||||
)
|
||||
|
||||
var dirContent []byte
|
||||
|
@ -4,12 +4,13 @@ import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/bmatcuk/doublestar"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bmatcuk/doublestar"
|
||||
)
|
||||
|
||||
// FileUtils ...
|
||||
|
193
resources/metadata/abapEnvironmentCreateSystem.yaml
Normal file
193
resources/metadata/abapEnvironmentCreateSystem.yaml
Normal file
@ -0,0 +1,193 @@
|
||||
metadata:
|
||||
name: abapEnvironmentCreateSystem
|
||||
description: Creates a SAP Cloud Platform ABAP Environment system (aka Steampunk system)
|
||||
longDescription: |
|
||||
creates a SAP Cloud Platform ABAP Environment system (aka Steampunk system)
|
||||
|
||||
spec:
|
||||
inputs:
|
||||
secrets:
|
||||
- name: cfCredentialsId
|
||||
description: Jenkins 'Username with password' credentials ID containing user and password to authenticate to the Cloud Foundry API.
|
||||
type: jenkins
|
||||
aliases:
|
||||
- name: cloudFoundry/credentialsId
|
||||
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
|
||||
- type: vaultSecret
|
||||
paths:
|
||||
- $(vaultPath)/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- $(vaultBasePath)/$(vaultPipelineName)/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- $(vaultBasePath)/GROUP-SECRETS/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- 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
|
||||
- type: vaultSecret
|
||||
paths:
|
||||
- $(vaultPath)/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- $(vaultBasePath)/$(vaultPipelineName)/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- $(vaultBasePath)/GROUP-SECRETS/cloudfoundry-$(cfOrg)-$(cfSpace)
|
||||
- name: cfOrg
|
||||
type: string
|
||||
description: Cloud Foundry org
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: true
|
||||
aliases:
|
||||
- name: cloudFoundry/org
|
||||
- name: cfSpace
|
||||
type: string
|
||||
description: Cloud Foundry Space
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: true
|
||||
aliases:
|
||||
- name: cloudFoundry/space
|
||||
- name: cfService
|
||||
type: string
|
||||
description: Parameter for Cloud Foundry Service to be used for creating Cloud Foundry Service
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: false
|
||||
aliases:
|
||||
- name: cloudFoundry/service
|
||||
- name: cfServicePlan
|
||||
type: string
|
||||
description: Parameter for Cloud Foundry Service Plan to be used when creating a Cloud Foundry Service
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: false
|
||||
aliases:
|
||||
- name: cloudFoundry/servicePlan
|
||||
- name: cfServiceInstance
|
||||
type: string
|
||||
description: Parameter for naming the Service Instance when creating a Cloud Foundry Service
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: false
|
||||
aliases:
|
||||
- name: cloudFoundry/serviceInstance
|
||||
- name: serviceManifest
|
||||
type: string
|
||||
description: Path to Cloud Foundry Service Manifest in YAML format for multiple service creations that are being passed to a Create-Service-Push Cloud Foundry cli plugin
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
mandatory: false
|
||||
aliases:
|
||||
- name: cloudFoundry/serviceManifest
|
||||
- name: cfServiceManifest
|
||||
- name: adminEmail
|
||||
type: string
|
||||
description: Admin E-Mail address for the initial administrator of the system
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
- name: description
|
||||
description: Description for the ABAP Environment system
|
||||
type: string
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
default: 'Test system created by an automated pipeline'
|
||||
- name: isDevelopmentAllowed
|
||||
type: bool
|
||||
description: This parameter determines, if development is allowed on the system
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
default: true
|
||||
- name: sapSystemName
|
||||
description: The three character name of the system
|
||||
type: string
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
default: 'H02'
|
||||
- name: sizeOfPersistence
|
||||
description: The size of the persistence
|
||||
type: int
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
- name: sizeOfRuntime
|
||||
description: The size of the runtime
|
||||
type: int
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
- name: addonDescriptorFileName
|
||||
description: The file name of the addonDescriptor
|
||||
type: string
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- GENERAL
|
||||
containers:
|
||||
- name: cf
|
||||
image: ppiper/cf-cli
|
||||
imagePullPolicy: Never
|
Loading…
x
Reference in New Issue
Block a user