diff --git a/.changelog/6250.txt b/.changelog/6250.txt new file mode 100644 index 0000000000..79c86ebc1b --- /dev/null +++ b/.changelog/6250.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +storage: Added name validation for `google_storage_bucket` +``` diff --git a/google-beta/resource_storage_bucket.go b/google-beta/resource_storage_bucket.go index 9c8dd05fd5..8dfcc591d7 100644 --- a/google-beta/resource_storage_bucket.go +++ b/google-beta/resource_storage_bucket.go @@ -401,6 +401,9 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error // Get the bucket and location bucket := d.Get("name").(string) + if err := checkGCSName(bucket); err != nil { + return err + } location := d.Get("location").(string) // Create a bucket, setting the labels, location and name. diff --git a/google-beta/utils.go b/google-beta/utils.go index 0ce18e799b..7d7dea1290 100644 --- a/google-beta/utils.go +++ b/google-beta/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "regexp" "sort" "strconv" "strings" @@ -525,3 +526,28 @@ func fake404(reasonResourceType, resourceName string) *googleapi.Error { Message: fmt.Sprintf("%v object %v not found", reasonResourceType, resourceName), } } + +// validate name of the gcs bucket. +func checkGCSName(name string) error { + MAX_LENGTH := 63 + var err error + for _, str := range strings.Split(name, ".") { + strLen := len(str) + fmt.Println(str) + if strLen > MAX_LENGTH { + return fmt.Errorf("error: maximum length exceeded %v\n", str) + } + valid, _ := regexp.MatchString("^[a-z0-9_]+(-[a-z0-9]+)*$", str) + if !valid { + return fmt.Errorf("error: string validation failed %v\n", str) + } + gPrefix := strings.HasPrefix(str, "goog") + if gPrefix { + return fmt.Errorf("error: string cannot start with %q %v\n", "goog", str) + } + if err != nil { + break + } + } + return nil +}