Skip to content

Commit c577a15

Browse files
Validation for VPC Name (#6348) (#12271)
* Validation check * renamed validateGCPName to validateGCEName * adding doc link for naming convention * Update mmv1/third_party/terraform/utils/validation.go Co-authored-by: Stephen Lewis (Burrows) <[email protected]> * Update mmv1/third_party/terraform/utils/validation.go Co-authored-by: Stephen Lewis (Burrows) <[email protected]> * renamed validateGCPName to validateGCEName Co-authored-by: Stephen Lewis (Burrows) <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
1 parent 9f8d4af commit c577a15

22 files changed

+31
-25
lines changed

.changelog/6348.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added validation for name field on `google_compute_network`
3+
```

google/resource_cloudfunctions_function.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func parseCloudFunctionId(d *schema.ResourceData, config *Config) (*cloudFunctio
7272
}, nil
7373
}
7474

75-
// Differs from validateGcpName because Cloud Functions allow capital letters
75+
// Differs from validateGCEName because Cloud Functions allow capital letters
7676
// at start/end
7777
func validateResourceCloudFunctionsFunctionName(v interface{}, k string) (ws []string, errors []error) {
7878
re := `^(?:[a-zA-Z](?:[-_a-zA-Z0-9]{0,61}[a-zA-Z0-9])?)$`

google/resource_composer_environment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func resourceComposerEnvironment() *schema.Resource {
133133
Type: schema.TypeString,
134134
Required: true,
135135
ForceNew: true,
136-
ValidateFunc: validateGCPName,
136+
ValidateFunc: validateGCEName,
137137
Description: `Name of the environment.`,
138138
},
139139
"region": {

google/resource_compute_autoscaler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ to include directives regarding slower scale down, as described above.`,
280280
Type: schema.TypeString,
281281
Required: true,
282282
ForceNew: true,
283-
ValidateFunc: validateGCPName,
283+
ValidateFunc: validateGCEName,
284284
Description: `Name of the resource. The name must be 1-63 characters long and match
285285
the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?' which means the
286286
first character must be a lowercase letter, and all following

google/resource_compute_firewall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func resourceComputeFirewall() *schema.Resource {
163163
Type: schema.TypeString,
164164
Required: true,
165165
ForceNew: true,
166-
ValidateFunc: validateGCPName,
166+
ValidateFunc: validateGCEName,
167167
Description: `Name of the resource. Provided by the client when the resource is
168168
created. The name must be 1-63 characters long, and comply with
169169
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_forwarding_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func resourceComputeForwardingRule() *schema.Resource {
187187
Optional: true,
188188
ForceNew: true,
189189
Description: "An optional prefix to the service name for this Forwarding Rule. If specified, the prefix is the first label of the fully qualified service name. The label must be 1-63 characters long, and comply with [RFC1035](https://www.ietf.org/rfc/rfc1035.txt). Specifically, the label must be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash. This field is only used for internal load balancing.",
190-
ValidateFunc: validateGCPName,
190+
ValidateFunc: validateGCEName,
191191
},
192192

193193
"subnetwork": {

google/resource_compute_global_network_endpoint_group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func resourceComputeGlobalNetworkEndpointGroup() *schema.Resource {
4343
Type: schema.TypeString,
4444
Required: true,
4545
ForceNew: true,
46-
ValidateFunc: validateGCPName,
46+
ValidateFunc: validateGCEName,
4747
Description: `Name of the resource; provided by the client when the resource is
4848
created. The name must be 1-63 characters long, and comply with
4949
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_ha_vpn_gateway.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func resourceComputeHaVpnGateway() *schema.Resource {
4343
Type: schema.TypeString,
4444
Required: true,
4545
ForceNew: true,
46-
ValidateFunc: validateGCPName,
46+
ValidateFunc: validateGCEName,
4747
Description: `Name of the resource. Provided by the client when the resource is
4848
created. The name must be 1-63 characters long, and comply with
4949
RFC1035. Specifically, the name must be 1-63 characters long and

google/resource_compute_instance_template.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func resourceComputeInstanceTemplate() *schema.Resource {
6767
Computed: true,
6868
ForceNew: true,
6969
ConflictsWith: []string{"name_prefix"},
70-
ValidateFunc: validateGCPName,
70+
ValidateFunc: validateGCEName,
7171
Description: `The name of the instance template. If you leave this blank, Terraform will auto-generate a unique name.`,
7272
},
7373

google/resource_compute_network.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func resourceComputeNetwork() *schema.Resource {
4343

4444
Schema: map[string]*schema.Schema{
4545
"name": {
46-
Type: schema.TypeString,
47-
Required: true,
48-
ForceNew: true,
46+
Type: schema.TypeString,
47+
Required: true,
48+
ForceNew: true,
49+
ValidateFunc: validateGCEName,
4950
Description: `Name of the resource. Provided by the client when the resource is
5051
created. The name must be 1-63 characters long, and comply with
5152
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_network_endpoint_group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func resourceComputeNetworkEndpointGroup() *schema.Resource {
4343
Type: schema.TypeString,
4444
Required: true,
4545
ForceNew: true,
46-
ValidateFunc: validateGCPName,
46+
ValidateFunc: validateGCEName,
4747
Description: `Name of the resource; provided by the client when the resource is
4848
created. The name must be 1-63 characters long, and comply with
4949
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_network_peering.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func resourceComputeNetworkPeering() *schema.Resource {
3636
Type: schema.TypeString,
3737
Required: true,
3838
ForceNew: true,
39-
ValidateFunc: validateGCPName,
39+
ValidateFunc: validateGCEName,
4040
Description: `Name of the peering.`,
4141
},
4242

google/resource_compute_packet_mirroring.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ set to true.`,
114114
"name": {
115115
Type: schema.TypeString,
116116
Required: true,
117-
ValidateFunc: validateGCPName,
117+
ValidateFunc: validateGCEName,
118118
Description: `The name of the packet mirroring rule`,
119119
},
120120
"network": {

google/resource_compute_region_autoscaler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ to include directives regarding slower scale down, as described above.`,
280280
Type: schema.TypeString,
281281
Required: true,
282282
ForceNew: true,
283-
ValidateFunc: validateGCPName,
283+
ValidateFunc: validateGCEName,
284284
Description: `Name of the resource. The name must be 1-63 characters long and match
285285
the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?' which means the
286286
first character must be a lowercase letter, and all following

google/resource_compute_region_network_endpoint_group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func resourceComputeRegionNetworkEndpointGroup() *schema.Resource {
4343
Type: schema.TypeString,
4444
Required: true,
4545
ForceNew: true,
46-
ValidateFunc: validateGCPName,
46+
ValidateFunc: validateGCEName,
4747
Description: `Name of the resource; provided by the client when the resource is
4848
created. The name must be 1-63 characters long, and comply with
4949
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_region_ssl_certificate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The chain must include at least one intermediate cert.`,
6868
Computed: true,
6969
Optional: true,
7070
ForceNew: true,
71-
ValidateFunc: validateGCPName,
71+
ValidateFunc: validateGCEName,
7272
Description: `Name of the resource. Provided by the client when the resource is
7373
created. The name must be 1-63 characters long, and comply with
7474
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func resourceComputeRouter() *schema.Resource {
6666
Type: schema.TypeString,
6767
Required: true,
6868
ForceNew: true,
69-
ValidateFunc: validateGCPName,
69+
ValidateFunc: validateGCEName,
7070
Description: `Name of the resource. The name must be 1-63 characters long, and
7171
comply with RFC1035. Specifically, the name must be 1-63 characters
7272
long and match the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?'

google/resource_compute_security_policy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func resourceComputeSecurityPolicy() *schema.Resource {
3535
Type: schema.TypeString,
3636
Required: true,
3737
ForceNew: true,
38-
ValidateFunc: validateGCPName,
38+
ValidateFunc: validateGCEName,
3939
Description: `The name of the security policy.`,
4040
},
4141

google/resource_compute_ssl_certificate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The chain must include at least one intermediate cert.`,
6868
Computed: true,
6969
Optional: true,
7070
ForceNew: true,
71-
ValidateFunc: validateGCPName,
71+
ValidateFunc: validateGCEName,
7272
Description: `Name of the resource. Provided by the client when the resource is
7373
created. The name must be 1-63 characters long, and comply with
7474
RFC1035. Specifically, the name must be 1-63 characters long and match

google/resource_compute_subnetwork.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ non-overlapping within a network. Only IPv4 is supported.`,
8383
Type: schema.TypeString,
8484
Required: true,
8585
ForceNew: true,
86-
ValidateFunc: validateGCPName,
86+
ValidateFunc: validateGCEName,
8787
Description: `The name of the resource, provided by the client when initially
8888
creating the resource. The name must be 1-63 characters long, and
8989
comply with RFC1035. Specifically, the name must be 1-63 characters
@@ -250,7 +250,7 @@ secondary IP ranges within a network. Only IPv4 is supported.`,
250250
"range_name": {
251251
Type: schema.TypeString,
252252
Required: true,
253-
ValidateFunc: validateGCPName,
253+
ValidateFunc: validateGCEName,
254254
Description: `The name associated with this subnetwork secondary range, used
255255
when adding an alias IP range to a VM instance. The name must
256256
be 1-63 characters long, and comply with RFC1035. The name

google/validation.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ var rfc1918Networks = []string{
8282
"192.168.0.0/16",
8383
}
8484

85-
func validateGCPName(v interface{}, k string) (ws []string, errors []error) {
85+
// validateGCEName ensures that a field matches the requirements for Compute Engine resource names
86+
// https://cloud.google.com/compute/docs/naming-resources#resource-name-format
87+
func validateGCEName(v interface{}, k string) (ws []string, errors []error) {
8688
re := `^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`
8789
return validateRegexp(re)(v, k)
8890
}

google/validation_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1111
)
1212

13-
func TestValidateGCPName(t *testing.T) {
13+
func TestvalidateGCEName(t *testing.T) {
1414
x := []StringValidationTestCase{
1515
// No errors
1616
{TestName: "basic", Value: "foobar"},
@@ -27,7 +27,7 @@ func TestValidateGCPName(t *testing.T) {
2727
{TestName: "too long", Value: "foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoob", ExpectError: true},
2828
}
2929

30-
es := testStringValidationCases(x, validateGCPName)
30+
es := testStringValidationCases(x, validateGCEName)
3131
if len(es) > 0 {
3232
t.Errorf("Failed to validate GCP names: %v", es)
3333
}

0 commit comments

Comments
 (0)