Skip to content

Commit

Permalink
Reorganize configuration package
Browse files Browse the repository at this point in the history
  • Loading branch information
nictas committed Feb 26, 2020
1 parent ae17597 commit 3ff7040
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 126 deletions.
6 changes: 3 additions & 3 deletions commands/deploy_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ func (c *DeployCommand) Execute(args []string) ExecutionStatus {
return Failure
}

chunkSizeInMB := c.configurationSnapshot.GetChunkSizeInMB()
uploadChunkSizeInMB := c.configurationSnapshot.GetUploadChunkSizeInMB()
// Upload the MTA archive file
mtaArchiveUploader := NewFileUploader([]string{mtaArchivePath}, mtaClient, chunkSizeInMB)
mtaArchiveUploader := NewFileUploader([]string{mtaArchivePath}, mtaClient, uploadChunkSizeInMB)
uploadedMtaArchives, status := mtaArchiveUploader.UploadFiles()
if status == Failure {
return Failure
Expand All @@ -301,7 +301,7 @@ func (c *DeployCommand) Execute(args []string) ExecutionStatus {
// Upload the extension descriptor files
var uploadedExtDescriptorIDs []string
if len(extDescriptorPaths) != 0 {
extDescriptorsUploader := NewFileUploader(extDescriptorPaths, mtaClient, chunkSizeInMB)
extDescriptorsUploader := NewFileUploader(extDescriptorPaths, mtaClient, uploadChunkSizeInMB)
uploadedExtDescriptors, status := extDescriptorsUploader.UploadFiles()
if status == Failure {
return Failure
Expand Down
22 changes: 11 additions & 11 deletions commands/file_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (

//FileUploader uploads files for the service with the specified service ID
type FileUploader struct {
files []string
mtaClient mtaclient.MtaClientOperations
chunkSizeInMB uint64
files []string
mtaClient mtaclient.MtaClientOperations
uploadChunkSizeInMB uint64
}

//NewFileUploader creates a new file uploader for the specified service ID, files, and SLMP client
func NewFileUploader(files []string, mtaClient mtaclient.MtaClientOperations, chunkSizeInMB uint64) *FileUploader {
func NewFileUploader(files []string, mtaClient mtaclient.MtaClientOperations, uploadChunkSizeInMB uint64) *FileUploader {
return &FileUploader{
files: files,
mtaClient: mtaClient,
chunkSizeInMB: chunkSizeInMB,
files: files,
mtaClient: mtaClient,
uploadChunkSizeInMB: uploadChunkSizeInMB,
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (f *FileUploader) UploadFiles() ([]*models.FileMetadata, ExecutionStatus) {
ui.Say(" " + fullPath)

// Upload the file
uploaded, err := uploadInChunks(fullPath, fileToUpload, f.chunkSizeInMB, f.mtaClient)
uploaded, err := uploadInChunks(fullPath, fileToUpload, f.uploadChunkSizeInMB, f.mtaClient)
if err != nil {
ui.Failed("Could not upload file %s: %s", terminal.EntityNameColor(fileToUpload.Name()), err.Error())
return nil, Failure
Expand All @@ -101,13 +101,13 @@ func (f *FileUploader) UploadFiles() ([]*models.FileMetadata, ExecutionStatus) {
return uploadedFiles, Success
}

func uploadInChunks(fullPath string, fileToUpload os.File, chunkSizeInMB uint64, mtaClient mtaclient.MtaClientOperations) ([]*models.FileMetadata, error) {
func uploadInChunks(fullPath string, fileToUpload os.File, uploadChunkSizeInMB uint64, mtaClient mtaclient.MtaClientOperations) ([]*models.FileMetadata, error) {
// Upload the file
err := util.ValidateChunkSize(fullPath, chunkSizeInMB)
err := util.ValidateChunkSize(fullPath, uploadChunkSizeInMB)
if err != nil {
return nil, fmt.Errorf("Could not valide file %q: %v", fullPath, baseclient.NewClientError(err))
}
fileToUploadParts, err := util.SplitFile(fullPath, chunkSizeInMB)
fileToUploadParts, err := util.SplitFile(fullPath, uploadChunkSizeInMB)
if err != nil {
return nil, fmt.Errorf("Could not process file %q: %v", fullPath, baseclient.NewClientError(err))
}
Expand Down
14 changes: 7 additions & 7 deletions commands/file_uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/commands"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration/properties"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
Expand Down Expand Up @@ -44,7 +44,7 @@ var _ = Describe("FileUploader", func() {
client := fakeSlmpClientBuilder.GetMtaFiles([]*models.FileMetadata{}, nil).Build()

output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{}, client, properties.DefaultUploadChunkSizeInMB)
uploadedFiles, status = fileUploader.UploadFiles()
})
ex.ExpectSuccess(status.ToInt(), output)
Expand All @@ -57,7 +57,7 @@ var _ = Describe("FileUploader", func() {
client := fakeSlmpClientBuilder.GetMtaFiles([]*models.FileMetadata{&testutil.SimpleFile}, nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{}, client, properties.DefaultUploadChunkSizeInMB)
uploadedFiles, status = fileUploader.UploadFiles()
})
ex.ExpectSuccess(status.ToInt(), output)
Expand All @@ -73,7 +73,7 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(*testFile, testutil.GetFile(*testFile, testFileDigest), nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, properties.DefaultUploadChunkSizeInMB)
uploadedFiles, status = fileUploader.UploadFiles()
})
Expect(len(uploadedFiles)).To(Equal(1))
Expand All @@ -95,7 +95,7 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(*testFile, testutil.GetFile(*testFile, testFileDigest), nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, properties.DefaultUploadChunkSizeInMB)
uploadedFiles, status = fileUploader.UploadFiles()
})
ex.ExpectSuccessWithOutput(status.ToInt(), output, []string{
Expand All @@ -113,7 +113,7 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(*testFile, fileMetadata, nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, properties.DefaultUploadChunkSizeInMB)
uploadedFiles, status = fileUploader.UploadFiles()
})
Expect(len(uploadedFiles)).To(Equal(1))
Expand All @@ -135,7 +135,7 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(*testFile, &models.FileMetadata{}, errors.New("Unexpected error from the backend")).Build()
// var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, configuration.DefaultChunkSizeInMB)
fileUploader = commands.NewFileUploader([]string{testFileAbsolutePath}, client, properties.DefaultUploadChunkSizeInMB)
_, status = fileUploader.UploadFiles()
})
// Expect(len(uploadedFiles)).To(Equal(1))
Expand Down
Empty file removed commands/test.mtar
Empty file.
20 changes: 0 additions & 20 deletions configuration/chunk_size_in_mb_parser.go

This file was deleted.

14 changes: 0 additions & 14 deletions configuration/configurable_property.go

This file was deleted.

56 changes: 16 additions & 40 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,41 @@ package configuration

import (
"fmt"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration/properties"
"os"
)

const (
unknownError = "An unknown error occurred during the parsing of the environment variable \"%s\". Please report this! Value type: %T"
DefaultChunkSizeInMB = uint64(45)
)

var BackendURLConfigurableProperty = configurableProperty{
Name: "MULTIAPPS_CONTROLLER_URL",
DeprecatedNames: []string{
"DEPLOY_SERVICE_URL",
},
Parser: noOpParser{},
ParsingSuccessMessage: "Attention: You've specified a custom backend URL (%s) via the environment variable \"%s\". The application listening on that URL may be outdated, contain bugs or unreleased features or may even be modified by a potentially untrused person. Use at your own risk.\n",
ParsingFailureMessage: "No validation implemented for custom backend URLs. If you're seeing this message then something has gone horribly wrong.\n",
DefaultValue: "",
}

var ChunkSizeInMBConfigurableProperty = configurableProperty{
Name: "MULTIAPPS_UPLOAD_CHUNK_SIZE",
DeprecatedNames: []string{
"CHUNK_SIZE_IN_MB",
},
Parser: chunkSizeInMBParser{},
ParsingSuccessMessage: "Attention: You've specified a custom chunk size (%d MB) via the environment variable \"%s\".\n",
ParsingFailureMessage: "Attention: You've specified an INVALID custom chunk size (%s) via the environment variable \"%s\". Using default: %d\n",
DefaultValue: DefaultChunkSizeInMB,
}
const unknownError = "An unknown error occurred during the parsing of the environment variable \"%s\". Please report this! Value type: %T"

type Snapshot struct {
backendURL string
chunkSizeInMB uint64
backendURL string
uploadChunkSizeInMB uint64
}

func NewSnapshot() Snapshot {
return Snapshot{
backendURL: getBackendURLFromEnvironment(),
chunkSizeInMB: getChunkSizeInMBFromEnvironment(),
backendURL: getBackendURLFromEnvironment(),
uploadChunkSizeInMB: getUploadChunkSizeInMBFromEnvironment(),
}
}

func (c Snapshot) GetBackendURL() string {
return c.backendURL
}

func (c Snapshot) GetChunkSizeInMB() uint64 {
return c.chunkSizeInMB
func (c Snapshot) GetUploadChunkSizeInMB() uint64 {
return c.uploadChunkSizeInMB
}

func getBackendURLFromEnvironment() string {
return getStringProperty(BackendURLConfigurableProperty)
return getStringProperty(properties.BackendURL)
}

func getChunkSizeInMBFromEnvironment() uint64 {
return getUint64Property(ChunkSizeInMBConfigurableProperty)
func getUploadChunkSizeInMBFromEnvironment() uint64 {
return getUint64Property(properties.UploadChunkSizeInMB)
}

func getStringProperty(property configurableProperty) string {
func getStringProperty(property properties.ConfigurableProperty) string {
uncastedValue := getPropertyOrDefault(property)
value, ok := uncastedValue.(string)
if !ok {
Expand All @@ -69,7 +45,7 @@ func getStringProperty(property configurableProperty) string {
return value
}

func getUint64Property(property configurableProperty) uint64 {
func getUint64Property(property properties.ConfigurableProperty) uint64 {
uncastedValue := getPropertyOrDefault(property)
value, ok := uncastedValue.(uint64)
if !ok {
Expand All @@ -78,7 +54,7 @@ func getUint64Property(property configurableProperty) uint64 {
return value
}

func getPropertyOrDefault(property configurableProperty) interface{} {
func getPropertyOrDefault(property properties.ConfigurableProperty) interface{} {
value := getPropertyWithNameOrDefaultIfInvalid(property, property.Name)
if value != nil {
return value
Expand All @@ -93,7 +69,7 @@ func getPropertyOrDefault(property configurableProperty) interface{} {
return property.DefaultValue
}

func getPropertyWithNameOrDefaultIfInvalid(property configurableProperty, name string) interface{} {
func getPropertyWithNameOrDefaultIfInvalid(property properties.ConfigurableProperty, name string) interface{} {
propertyValue, err := getPropertyWithName(name, property.Parser)
if err != nil {
propertyValue = os.Getenv(name)
Expand All @@ -107,7 +83,7 @@ func getPropertyWithNameOrDefaultIfInvalid(property configurableProperty, name s
return nil
}

func getPropertyWithName(name string, parser configurablePropertyParser) (interface{}, error) {
func getPropertyWithName(name string, parser properties.Parser) (interface{}, error) {
propertyValue, isSet := os.LookupEnv(name)
if isSet {
return parser.Parse(propertyValue)
Expand Down
43 changes: 22 additions & 21 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configuration_test

import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration/properties"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -18,25 +19,25 @@ var _ = Describe("Configuration", func() {
Describe("GetBackendURL", func() {

BeforeEach(func() {
os.Unsetenv(configuration.BackendURLConfigurableProperty.Name)
for _, name := range configuration.BackendURLConfigurableProperty.DeprecatedNames {
os.Unsetenv(properties.BackendURL.Name)
for _, name := range properties.BackendURL.DeprecatedNames {
os.Unsetenv(name)
}
})

Context("with a set environment variable", func() {
It("should return its value", func() {
backendURL := "http://my-multiapps-controller.domain.com"
os.Setenv(configuration.BackendURLConfigurableProperty.Name, backendURL)
os.Setenv(properties.BackendURL.Name, backendURL)
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetBackendURL()).To(Equal(backendURL))
})
})
Context("with a set environment variable (deprecated)", func() {
It("should return its value", func() {
if len(configuration.BackendURLConfigurableProperty.DeprecatedNames) > 0 {
if len(properties.BackendURL.DeprecatedNames) > 0 {
backendURL := "http://my-multiapps-controller.domain.com"
os.Setenv(configuration.BackendURLConfigurableProperty.DeprecatedNames[0], backendURL)
os.Setenv(properties.BackendURL.DeprecatedNames[0], backendURL)
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetBackendURL()).To(Equal(backendURL))
}
Expand All @@ -54,52 +55,52 @@ var _ = Describe("Configuration", func() {
Describe("GetChunkSizeInMB", func() {

BeforeEach(func() {
os.Unsetenv(configuration.ChunkSizeInMBConfigurableProperty.Name)
for _, name := range configuration.ChunkSizeInMBConfigurableProperty.DeprecatedNames {
os.Unsetenv(properties.UploadChunkSizeInMB.Name)
for _, name := range properties.UploadChunkSizeInMB.DeprecatedNames {
os.Unsetenv(name)
}
})

Context("with a set environment variable", func() {
Context("containing a positive integer", func() {
It("should return its value", func() {
chunkSizeInMB := uint64(5)
os.Setenv(configuration.ChunkSizeInMBConfigurableProperty.Name, strconv.Itoa(int(chunkSizeInMB)))
uploadChunkSizeInMB := uint64(5)
os.Setenv(properties.UploadChunkSizeInMB.Name, strconv.Itoa(int(uploadChunkSizeInMB)))
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetChunkSizeInMB()).To(Equal(chunkSizeInMB))
Expect(configurationSnapshot.GetUploadChunkSizeInMB()).To(Equal(uploadChunkSizeInMB))
})
})
Context("containing zero", func() {
It("should return the default value", func() {
chunkSizeInMB := 0
os.Setenv(configuration.ChunkSizeInMBConfigurableProperty.Name, strconv.Itoa(chunkSizeInMB))
uploadChunkSizeInMB := 0
os.Setenv(properties.UploadChunkSizeInMB.Name, strconv.Itoa(uploadChunkSizeInMB))
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
Expect(configurationSnapshot.GetUploadChunkSizeInMB()).To(Equal(properties.DefaultUploadChunkSizeInMB))
})
})
Context("containing a string", func() {
It("should return the default value", func() {
chunkSizeInMB := "abc"
os.Setenv(configuration.ChunkSizeInMBConfigurableProperty.Name, chunkSizeInMB)
uploadChunkSizeInMB := "abc"
os.Setenv(properties.UploadChunkSizeInMB.Name, uploadChunkSizeInMB)
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
Expect(configurationSnapshot.GetUploadChunkSizeInMB()).To(Equal(properties.DefaultUploadChunkSizeInMB))
})
})
})
Context("with a set environment variable (deprecated)", func() {
It("should return its value", func() {
if len(configuration.ChunkSizeInMBConfigurableProperty.DeprecatedNames) > 0 {
chunkSizeInMB := uint64(5)
os.Setenv(configuration.ChunkSizeInMBConfigurableProperty.DeprecatedNames[0], strconv.Itoa(int(chunkSizeInMB)))
if len(properties.UploadChunkSizeInMB.DeprecatedNames) > 0 {
uploadChunkSizeInMB := uint64(5)
os.Setenv(properties.UploadChunkSizeInMB.DeprecatedNames[0], strconv.Itoa(int(uploadChunkSizeInMB)))
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetChunkSizeInMB()).To(Equal(chunkSizeInMB))
Expect(configurationSnapshot.GetUploadChunkSizeInMB()).To(Equal(uploadChunkSizeInMB))
}
})
})
Context("without a set environment variable", func() {
It("should return the default value", func() {
configurationSnapshot := configuration.NewSnapshot()
Expect(configurationSnapshot.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
Expect(configurationSnapshot.GetUploadChunkSizeInMB()).To(Equal(properties.DefaultUploadChunkSizeInMB))
})
})

Expand Down
Loading

0 comments on commit 3ff7040

Please sign in to comment.