Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/objectstore/objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package objectstore

import (
"errors"
"fmt"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,6 +36,10 @@ var objectStoreCredentialCmd = &cobra.Command{
},
}

const (
minBucketSizeGB = 500
)

func init() {

ObjectStoreCmd.AddCommand(objectStoreListCmd)
Expand All @@ -45,10 +50,11 @@ func init() {
ObjectStoreCmd.AddCommand(objectStoreCredentialCmd)

//Flags for create cmd
objectStoreCreateCmd.Flags().Int64VarP(&bucketSize, "size", "s", 500, "Size of the Object store (Minimum size is 500GB)")
objectStoreCreateCmd.Flags().Int64VarP(&bucketSize, "size", "s", minBucketSizeGB, fmt.Sprintf("Size of the Object store (Allowed sizes: %dGB or 1000GB)", minBucketSizeGB))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the 500 is set as constant while the 1000 isn't?

objectStoreCreateCmd.Flags().StringVarP(&owner, "owner-name", "n", "", "Name of Owner of the Object store. You can reference name of any civo object store credential created before")
objectStoreCreateCmd.Flags().StringVarP(&owner, "owner-access-key", "a", "", "Access Key ID of Owner of the Object store. You can reference name of any civo object store credential created before")
objectStoreCreateCmd.Flags().BoolVarP(&waitOS, "wait", "w", false, "a simple flag (e.g. --wait) that will cause the CLI to spin and wait for the Object Store to be ready")
objectStoreCreateCmd.Flags().BoolVarP(&yesFlag, "yes", "y", false, "Automatically agree to any confirmation prompts")

//Flags for update cmd
objectStoreUpdateCmd.Flags().Int64VarP(&bucketSize, "size", "s", 500, "Size of the object store (Minimum size is 500GB)")
Expand Down
56 changes: 45 additions & 11 deletions cmd/objectstore/objectstore_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ import (
"github.com/spf13/cobra"
)

var allowedBucketSizes = []int64{500, 1000}

var bucketSize int64
var owner string
var waitOS bool
var yesFlag bool

var objectStoreCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: "civo objectstore create OBJECTSTORE_NAME --size SIZE",
Short: "Create a new Object Store",
Long: "Bucket size should be in Gigabytes (GB) and must be a multiple of 500, starting from 500.\n",
Long: "Bucket size should be in Gigabytes (GB) and must be either 500 or 1000.\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we use the constants of before, declared in a common package?

Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()
Expand All @@ -34,7 +37,6 @@ var objectStoreCreateCmd = &cobra.Command{
os.Exit(1)
}
objectStoreName = args[0]

} else {
objectStoreName = utility.RandomName()
}
Expand All @@ -60,17 +62,29 @@ var objectStoreCreateCmd = &cobra.Command{
client.Region = common.RegionSet
}

if bucketSize < 500 {
utility.Error("The minimum size to create an object store is 500 GB. Please provide a valid size.")
if bucketSize < minBucketSizeGB {
utility.Error("The minimum size to create an object store is %d GB. Please provide a valid size.", minBucketSizeGB)
os.Exit(1)
} else if bucketSize%500 != 0 {
utility.YellowConfirm("The size to create an object store must be a multiple of 500. Would you like to create an %s of %d GB instead? (y/n) ? ", utility.Green("object store"), bucketSize+(500-bucketSize%500))
_, err := utility.UserAccepts(os.Stdin)
if err != nil {
utility.Error("Unable to parse the input: %s", err)
}

if !isAllowedSize(bucketSize) {
newSize := adjustedBucketSize(bucketSize)
if !isAllowedSize(newSize) {
utility.Error("Only 500 GB and 1000 GB sizes are allowed for Object Store.")
os.Exit(1)
}
bucketSize = bucketSize + (500 - bucketSize%500)
if !yesFlag {
utility.YellowConfirm("Only 500 GB and 1000 GB sizes are allowed. Would you like to create an object store of %d GB instead? (y/n)", newSize)
accept, err := utility.UserAccepts(os.Stdin)
if err != nil {
utility.Error("Unable to parse the input: %s", err)
os.Exit(1)
}
if !accept {
os.Exit(1)
}
}
bucketSize = newSize
}

var credential *civogo.ObjectStoreCredential
Expand Down Expand Up @@ -135,7 +149,11 @@ var objectStoreCreateCmd = &cobra.Command{
os.Exit(1)
}

ow := utility.NewOutputWriterWithMap(map[string]string{"name": objectStore.Name, "id": objectStore.ID, "access_key": objectStore.OwnerInfo.AccessKeyID})
ow := utility.NewOutputWriterWithMap(map[string]string{
"name": objectStore.Name,
"id": objectStore.ID,
"access_key": objectStore.OwnerInfo.AccessKeyID,
})

switch common.OutputFormat {
case "json":
Expand All @@ -154,3 +172,19 @@ var objectStoreCreateCmd = &cobra.Command{
}
},
}

func adjustedBucketSize(size int64) int64 {
if size < minBucketSizeGB {
return minBucketSizeGB
}
return minBucketSizeGB * ((size + minBucketSizeGB - 1) / minBucketSizeGB)
}

func isAllowedSize(size int64) bool {
for _, allowed := range allowedBucketSizes {
if size == allowed {
return true
}
}
return false
}