Skip to content

Commit 9e8c9cf

Browse files
fix: (storage) remove client side bucket name validation as same validations being performed on API (#14582) (#23719)
[upstream:7bf0d21a138ee3a419b497aa14f26e8475b09520] Signed-off-by: Modular Magician <[email protected]>
1 parent 70db6ba commit 9e8c9cf

File tree

6 files changed

+16
-107
lines changed

6 files changed

+16
-107
lines changed

.changelog/14582.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
storage: remove bucket name validations for `google_storage_bucket` as the same validations being performed on API
3+
```

google/services/storage/resource_storage_bucket.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131

3232
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
3333
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
34-
"github.com/hashicorp/terraform-provider-google/google/verify"
3534

3635
"github.com/gammazero/workerpool"
3736
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
@@ -84,11 +83,10 @@ func ResourceStorageBucket() *schema.Resource {
8483

8584
Schema: map[string]*schema.Schema{
8685
"name": {
87-
Type: schema.TypeString,
88-
Required: true,
89-
ForceNew: true,
90-
Description: `The name of the bucket.`,
91-
ValidateFunc: verify.ValidateGCSName,
86+
Type: schema.TypeString,
87+
Required: true,
88+
ForceNew: true,
89+
Description: `The name of the bucket.`,
9290
},
9391

9492
"encryption": {

google/services/storage/resource_storage_bucket_600_migration.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424

2525
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2626
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
27-
28-
"github.com/hashicorp/terraform-provider-google/google/verify"
2927
)
3028

3129
func resourceStorageBucketV1() *schema.Resource {
@@ -40,11 +38,10 @@ func resourceStorageBucketV1() *schema.Resource {
4038

4139
Schema: map[string]*schema.Schema{
4240
"name": {
43-
Type: schema.TypeString,
44-
Required: true,
45-
ForceNew: true,
46-
Description: `The name of the bucket.`,
47-
ValidateFunc: verify.ValidateGCSName,
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
Description: `The name of the bucket.`,
4845
},
4946

5047
"encryption": {
@@ -560,11 +557,10 @@ func resourceStorageBucketV2() *schema.Resource {
560557
},
561558
Schema: map[string]*schema.Schema{
562559
"name": {
563-
Type: schema.TypeString,
564-
Required: true,
565-
ForceNew: true,
566-
Description: `The name of the bucket.`,
567-
ValidateFunc: verify.ValidateGCSName,
560+
Type: schema.TypeString,
561+
Required: true,
562+
ForceNew: true,
563+
Description: `The name of the bucket.`,
568564
},
569565

570566
"encryption": {

google/verify/validation.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ var (
9090
Rfc6996Asn32BitMin = int64(4200000000)
9191
Rfc6996Asn32BitMax = int64(4294967294)
9292
GcpRouterPartnerAsn = int64(16550)
93-
94-
// Format of GCS Bucket Name
95-
// https://cloud.google.com/storage/docs/naming-buckets
96-
GCSNameValidChars = "^[a-z0-9_.-]*$"
97-
GCSNameStartEndChars = "^[a-z|0-9].*[a-z|0-9]$"
98-
GCSNameLength = "^.{3,222}"
99-
GCSNameLengthSplit = "^.{1,63}$"
100-
GCSNameCidr = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$"
101-
GCSNameGoogPrefix = "^goog.*$"
102-
GCSNameContainsGoogle = "^.*google.*$"
10393
)
10494

10595
var Rfc1918Networks = []string{
@@ -115,44 +105,6 @@ func ValidateGCEName(v interface{}, k string) (ws []string, errors []error) {
115105
return ValidateRegexp(re)(v, k)
116106
}
117107

118-
// validateGCSName ensures the name of a gcs bucket matches the requirements for GCS Buckets
119-
// https://cloud.google.com/storage/docs/naming-buckets
120-
func ValidateGCSName(v interface{}, k string) (ws []string, errors []error) {
121-
value := v.(string)
122-
123-
if !regexp.MustCompile(GCSNameValidChars).MatchString(value) {
124-
errors = append(errors, fmt.Errorf("%q name value can only contain lowercase letters, numeric characters, dashes (-), underscores (_), and dots (.)", value))
125-
}
126-
127-
if !regexp.MustCompile(GCSNameStartEndChars).MatchString(value) {
128-
errors = append(errors, fmt.Errorf("%q name value must start and end with a number or letter", value))
129-
}
130-
131-
if !regexp.MustCompile(GCSNameLength).MatchString(value) {
132-
errors = append(errors, fmt.Errorf("%q name value must contain 3-63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters", value))
133-
}
134-
135-
for _, str := range strings.Split(value, ".") {
136-
if !regexp.MustCompile(GCSNameLengthSplit).MatchString(str) {
137-
errors = append(errors, fmt.Errorf("%q name value must contain 3-63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters", value))
138-
}
139-
}
140-
141-
if regexp.MustCompile(GCSNameCidr).MatchString(value) {
142-
errors = append(errors, fmt.Errorf("%q name value cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4)", value))
143-
}
144-
145-
if regexp.MustCompile(GCSNameGoogPrefix).MatchString(value) {
146-
errors = append(errors, fmt.Errorf("%q name value cannot begin with the \"goog\" prefix", value))
147-
}
148-
149-
if regexp.MustCompile(GCSNameContainsGoogle).MatchString(strings.ReplaceAll(value, "0", "o")) {
150-
errors = append(errors, fmt.Errorf("%q name value cannot contain \"google\" or close misspellings, such as \"g00gle\"", value))
151-
}
152-
153-
return
154-
}
155-
156108
// Ensure that the BGP ASN value of Cloud Router is a valid value as per RFC6996 or a value of 16550
157109
func ValidateRFC6996Asn(v interface{}, k string) (ws []string, errors []error) {
158110
value := int64(v.(int))

google/verify/validation_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -339,43 +339,3 @@ func TestValidateIAMCustomRoleIDRegex(t *testing.T) {
339339
t.Errorf("Failed to validate IAMCustomRole IDs: %v", es)
340340
}
341341
}
342-
343-
func TestValidateGCSName(t *testing.T) {
344-
x := []StringValidationTestCase{
345-
// No errors
346-
{TestName: "basic", Value: "foobar"},
347-
{TestName: "has number", Value: "foobar1"},
348-
{TestName: "all numbers", Value: "12345"},
349-
{TestName: "all _", Value: "foo_bar_baz"},
350-
{TestName: "all -", Value: "foo-bar-baz"},
351-
{TestName: "begins with number", Value: "1foo-bar_baz"},
352-
{TestName: "ends with number", Value: "foo-bar_baz1"},
353-
{TestName: "almost an ip", Value: "192.168.5.foo"},
354-
{TestName: "has _", Value: "foo-bar_baz"},
355-
{TestName: "--", Value: "foo--bar"},
356-
{TestName: "__", Value: "foo__bar"},
357-
{TestName: "-goog", Value: "foo-goog"},
358-
{TestName: ".goog", Value: "foo.goog"},
359-
360-
// With errors
361-
{TestName: "invalid char $", Value: "foo$bar", ExpectError: true},
362-
{TestName: "has uppercase", Value: "fooBar", ExpectError: true},
363-
{TestName: "begins with -", Value: "-foobar", ExpectError: true},
364-
{TestName: "ends with -", Value: "foobar-", ExpectError: true},
365-
{TestName: "begins with _", Value: "_foobar", ExpectError: true},
366-
{TestName: "ends with _", Value: "foobar_", ExpectError: true},
367-
{TestName: "less than 3 chars", Value: "fo", ExpectError: true},
368-
{TestName: "..", Value: "foo..bar", ExpectError: true},
369-
{TestName: "greater than 63 chars with no .", Value: "my-really-long-bucket-name-with-invalid-that-does-not-contain-a-period", ExpectError: true},
370-
{TestName: "greater than 63 chars between .", Value: "my.really-long-bucket-name-with-invalid-that-does-contain-a-period-but.is-too-long", ExpectError: true},
371-
{TestName: "has goog prefix", Value: "goog-foobar", ExpectError: true},
372-
{TestName: "almost an ip", Value: "192.168.5.1", ExpectError: true},
373-
{TestName: "contains google", Value: "foobar-google", ExpectError: true},
374-
{TestName: "contains close misspelling of google", Value: "foo-go0gle-bar", ExpectError: true},
375-
}
376-
377-
es := TestStringValidationCases(x, ValidateGCSName)
378-
if len(es) > 0 {
379-
t.Errorf("Failed to validate GCS names: %v", es)
380-
}
381-
}

website/docs/r/storage_bucket.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ resource "google_storage_bucket" "hns-enabled" {
135135

136136
The following arguments are supported:
137137

138-
* `name` - (Required) The name of the bucket.
138+
* `name` - (Required) The name of the bucket. Bucket names must be in lowercase and no more than 63 characters long. You can find the complete list of bucket naming rules [here](https://cloud.google.com/storage/docs/buckets#naming).
139139

140140
* `location` - (Required) The [GCS location](https://cloud.google.com/storage/docs/bucket-locations).
141141

0 commit comments

Comments
 (0)