Skip to content

Commit

Permalink
Adding validation for name attribute in gcs bucket (#6250) (#4532)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jul 26, 2022
1 parent c6eeea8 commit 468b55b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/6250.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: Added name validation for `google_storage_bucket`
```
3 changes: 3 additions & 0 deletions google-beta/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions google-beta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}

0 comments on commit 468b55b

Please sign in to comment.