Skip to content

Commit

Permalink
Merge pull request #414 from civo/bck-name
Browse files Browse the repository at this point in the history
Random Bucket Name
  • Loading branch information
uzaxirr authored May 17, 2024
2 parents 413890c + b7acbe4 commit d24595c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 16 additions & 4 deletions cmd/objectstore/objectstore_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ var objectStoreCreateCmd = &cobra.Command{
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",
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

var objectStoreName string
if len(args) > 0 {
if utility.ValidNameLength(args[0]) {
utility.Warning("the bucket name cannot be longer than 63 characters")
os.Exit(1)
}
objectStoreName = args[0]

} else {
objectStoreName = utility.RandomName()
}

check, region, err := utility.CheckAvailability("object_store", common.RegionSet)
if err != nil {
utility.Error("Error checking availability %s", err)
Expand Down Expand Up @@ -75,7 +87,7 @@ var objectStoreCreateCmd = &cobra.Command{
os.Exit(1)
}
store, err = client.NewObjectStore(&civogo.CreateObjectStoreRequest{
Name: args[0],
Name: objectStoreName,
MaxSizeGB: bucketSize,
AccessKeyID: credential.AccessKeyID,
Region: client.Region,
Expand All @@ -86,7 +98,7 @@ var objectStoreCreateCmd = &cobra.Command{
}
} else {
store, err = client.NewObjectStore(&civogo.CreateObjectStoreRequest{
Name: args[0],
Name: objectStoreName,
MaxSizeGB: bucketSize,
Region: client.Region,
})
Expand Down Expand Up @@ -121,7 +133,7 @@ var objectStoreCreateCmd = &cobra.Command{
executionTime = utility.TrackTime(startTime)
}

objectStore, err := client.FindObjectStore(args[0])
objectStore, err := client.FindObjectStore(objectStoreName)
if err != nil {
utility.Error("ObjectStore %s", err)
os.Exit(1)
Expand Down
12 changes: 9 additions & 3 deletions utility/random.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utility

import (
"github.com/google/uuid"
"math/rand"
"time"
)
Expand Down Expand Up @@ -40,9 +41,14 @@ var nouns = [...]string{
"shore", "cavern", "gorge", "spring", "arrow", "heap",
}

// RandomName generates a Heroku-style random name for instances/clusters/etc
// RandomName generates a Heroku-style random name for instances/clusters/etc and appends a short UUID
func RandomName() string {
rand.Seed(time.Now().Unix())
rand.Seed(time.Now().UnixNano()) // Seed the random number generator

return adjectives[rand.Intn(len(adjectives))] + "-" + nouns[rand.Intn(len(nouns))]
// Generate a random adjective and noun combination
name := adjectives[rand.Intn(len(adjectives))] + "-" + nouns[rand.Intn(len(nouns))]

// Generate a UUID and truncate it to the first 8 characters
shortUUID := uuid.New().String()[:8]
return name + "-" + shortUUID
}

0 comments on commit d24595c

Please sign in to comment.