From adc011f01696f370da8d05ad2580e70d4f6af8f6 Mon Sep 17 00:00:00 2001 From: harsh082ip Date: Wed, 10 Jul 2024 03:59:47 +0530 Subject: [PATCH 1/2] fix: reconsider behavior when providing a value lower than 500 for bucket size #394 --- cmd/objectstore/objectstore.go | 3 ++- cmd/objectstore/objectstore_create.go | 29 ++++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cmd/objectstore/objectstore.go b/cmd/objectstore/objectstore.go index 541665e0..a0314b52 100644 --- a/cmd/objectstore/objectstore.go +++ b/cmd/objectstore/objectstore.go @@ -45,10 +45,11 @@ func init() { ObjectStoreCmd.AddCommand(objectStoreCredentialCmd) //Flags for create cmd - objectStoreCreateCmd.Flags().Int64VarP(&bucketSize, "size", "s", 500, "Size of the Object store") + objectStoreCreateCmd.Flags().Int64VarP(&bucketSize, "size", "s", 500, "Size of the Object store in GB (minimum: 500)") 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") diff --git a/cmd/objectstore/objectstore_create.go b/cmd/objectstore/objectstore_create.go index e3cc4ef2..25250e9f 100644 --- a/cmd/objectstore/objectstore_create.go +++ b/cmd/objectstore/objectstore_create.go @@ -16,6 +16,7 @@ import ( var bucketSize int64 var owner string var waitOS bool +var yesFlag bool var objectStoreCreateCmd = &cobra.Command{ Use: "create", @@ -34,7 +35,6 @@ var objectStoreCreateCmd = &cobra.Command{ os.Exit(1) } objectStoreName = args[0] - } else { objectStoreName = utility.RandomName() } @@ -61,21 +61,22 @@ var objectStoreCreateCmd = &cobra.Command{ } if bucketSize < 500 { - utility.YellowConfirm("The minimum size to create an object store is 500 GB. Would you like to create an %s of 500 GB? (y/n) ? ", utility.Green("object store")) - _, err := utility.UserAccepts(os.Stdin) - if err != nil { - utility.Error("Unable to parse the input: %s", err) - os.Exit(1) - } - bucketSize = 500 + utility.Error("The minimum size to create an object store is 500 GB. Please provide a valid size.") + 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) - os.Exit(1) + if !yesFlag { + 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)) + 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(0) + } + } else { + bucketSize = bucketSize + (500 - bucketSize%500) } - bucketSize = bucketSize + (500 - bucketSize%500) } var credential *civogo.ObjectStoreCredential From a0168d02f668297f915f3e90d043b4fc02974e16 Mon Sep 17 00:00:00 2001 From: HARSH VARDHAN SINGH Date: Sat, 28 Jun 2025 10:33:17 +0530 Subject: [PATCH 2/2] fix: enforce valid object store sizes and improve flag validation --- cmd/objectstore/objectstore.go | 7 +++- cmd/objectstore/objectstore_create.go | 46 +++++++++++++++++++++------ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/cmd/objectstore/objectstore.go b/cmd/objectstore/objectstore.go index ef091439..0a271895 100644 --- a/cmd/objectstore/objectstore.go +++ b/cmd/objectstore/objectstore.go @@ -2,6 +2,7 @@ package objectstore import ( "errors" + "fmt" "github.com/spf13/cobra" ) @@ -35,6 +36,10 @@ var objectStoreCredentialCmd = &cobra.Command{ }, } +const ( + minBucketSizeGB = 500 +) + func init() { ObjectStoreCmd.AddCommand(objectStoreListCmd) @@ -45,7 +50,7 @@ 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)) 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") diff --git a/cmd/objectstore/objectstore_create.go b/cmd/objectstore/objectstore_create.go index 87f7d5c2..5140a3dd 100644 --- a/cmd/objectstore/objectstore_create.go +++ b/cmd/objectstore/objectstore_create.go @@ -13,6 +13,8 @@ import ( "github.com/spf13/cobra" ) +var allowedBucketSizes = []int64{500, 1000} + var bucketSize int64 var owner string var waitOS bool @@ -23,7 +25,7 @@ var objectStoreCreateCmd = &cobra.Command{ 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", Args: cobra.MinimumNArgs(0), Run: func(cmd *cobra.Command, args []string) { utility.EnsureCurrentRegion() @@ -60,23 +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 { + } + + 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) + } if !yesFlag { - 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)) + 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(0) + os.Exit(1) } - } else { - bucketSize = bucketSize + (500 - bucketSize%500) } + bucketSize = newSize } var credential *civogo.ObjectStoreCredential @@ -141,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": @@ -160,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 +}