Skip to content

Commit ce44058

Browse files
authored
reduce name prefix in compute resources (#11448)
1 parent cb56491 commit ce44058

12 files changed

+146
-20
lines changed

mmv1/products/compute/RegionSslCertificate.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ async: !ruby/object:Api::OpAsync
4747
docs: !ruby/object:Provider::Terraform::Docs
4848
optional_properties: |
4949
* `name_prefix` - (Optional) Creates a unique name beginning with the
50-
specified prefix. Conflicts with `name`.
50+
specified prefix. Conflicts with `name`. Max length is 54 characters.
51+
Prefixes with lengths longer than 37 characters will use a shortened
52+
UUID that will be more prone to collisions.
53+
Resulting name for a `name_prefix` <= 37 characters:
54+
`name_prefix` + YYYYmmddHHSSssss + 8 digit incremental counter
55+
Resulting name for a `name_prefix` 38 - 54 characters:
56+
`name_prefix` + YYmmdd + 3 digit incremental counter
5157
examples:
5258
- !ruby/object:Provider::Terraform::Examples
5359
name: 'region_ssl_certificate_basic'

mmv1/products/compute/SslCertificate.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ async: !ruby/object:Api::OpAsync
4747
docs: !ruby/object:Provider::Terraform::Docs
4848
optional_properties: |
4949
* `name_prefix` - (Optional) Creates a unique name beginning with the
50-
specified prefix. Conflicts with `name`.
50+
specified prefix. Conflicts with `name`. Max length is 54 characters.
51+
Prefixes with lengths longer than 37 characters will use a shortened
52+
UUID that will be more prone to collisions.
53+
Resulting name for a `name_prefix` <= 37 characters:
54+
`name_prefix` + YYYYmmddHHSSssss + 8 digit incremental counter
55+
Resulting name for a `name_prefix` 38 - 54 characters:
56+
`name_prefix` + YYmmdd + 3 digit incremental counter
5157
examples:
5258
- !ruby/object:Provider::Terraform::Examples
5359
name: 'ssl_certificate_basic'

mmv1/templates/terraform/custom_expand/name_or_name_prefix.go.erb

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d t
1717
if v, ok := d.GetOk("name"); ok {
1818
certName = v.(string)
1919
} else if v, ok := d.GetOk("name_prefix"); ok {
20-
certName = id.PrefixedUniqueId(v.(string))
20+
prefix := v.(string)
21+
if len(prefix) > 37 {
22+
certName = tpgresource.ReducedPrefixedUniqueId(prefix)
23+
} else {
24+
certName = id.PrefixedUniqueId(prefix)
25+
}
2126
} else {
2227
certName = id.UniqueId()
2328
}

mmv1/templates/terraform/encoders/workflow.go.erb

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ var ResName string
22
if v, ok := d.GetOk("name"); ok {
33
ResName = v.(string)
44
} else if v, ok := d.GetOk("name_prefix"); ok {
5-
ResName = id.PrefixedUniqueId(v.(string))
5+
prefix := v.(string)
6+
if len(prefix) > 37 {
7+
ResName = tpgresource.ReducedPrefixedUniqueId(prefix)
8+
} else {
9+
ResName = id.PrefixedUniqueId(prefix)
10+
}
611
} else {
712
ResName = id.UniqueId()
813
}

mmv1/templates/terraform/extra_schema_entry/ssl_certificate.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
Description: "Creates a unique name beginning with the specified prefix. Conflicts with name.",
88
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
99
// https://cloud.google.com/compute/docs/reference/latest/sslCertificates#resource
10-
// uuid is 26 characters, limit the prefix to 37.
10+
// uuid is 9 characters, limit the prefix to 54.
1111
value := v.(string)
12-
if len(value) > 37 {
12+
if len(value) > 54 {
1313
errors = append(errors, fmt.Errorf(
14-
"%q cannot be longer than 37 characters, name is limited to 63", k))
14+
"%q cannot be longer than 54 characters, name is limited to 63", k))
1515
}
1616
return
1717
},

mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.erb

+10-5
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ func ResourceComputeInstanceTemplate() *schema.Resource {
9696
Optional: true,
9797
Computed: true,
9898
ForceNew: true,
99-
Description: `Creates a unique name beginning with the specified prefix. Conflicts with name.`,
99+
Description: `Creates a unique name beginning with the specified prefix. Conflicts with name. Max length is 54 characters. Prefixes with lengths longer than 37 characters will use a shortened UUID that will be more prone to collisions.`,
100100
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
101101
// https://cloud.google.com/compute/docs/reference/latest/instanceTemplates#resource
102-
// uuid is 26 characters, limit the prefix to 37.
102+
// shortened uuid is 9 characters, limit the prefix to 55.
103103
value := v.(string)
104-
if len(value) > 37 {
104+
if len(value) > 54 {
105105
errors = append(errors, fmt.Errorf(
106-
"%q cannot be longer than 37 characters, name is limited to 63", k))
106+
"%q cannot be longer than 54 characters, name is limited to 63", k))
107107
}
108108
return
109109
},
@@ -1434,7 +1434,12 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
14341434
if v, ok := d.GetOk("name"); ok {
14351435
itName = v.(string)
14361436
} else if v, ok := d.GetOk("name_prefix"); ok {
1437-
itName = id.PrefixedUniqueId(v.(string))
1437+
prefix := v.(string)
1438+
if len(prefix) > 37 {
1439+
itName = tpgresource.ReducedPrefixedUniqueId(prefix)
1440+
} else {
1441+
itName = id.PrefixedUniqueId(prefix)
1442+
}
14381443
} else {
14391444
itName = id.UniqueId()
14401445
}

mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.erb

+61
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,45 @@ func TestAccComputeInstanceTemplate_invalidDiskType(t *testing.T) {
875875
})
876876
}
877877

878+
func TestAccComputeInstanceTemplate_withNamePrefix(t *testing.T) {
879+
t.Parallel()
880+
881+
// 8 + 46 = 54 which is the valid max
882+
normalPrefix := "tf-test-" + fmt.Sprintf("%046s", "")
883+
reducedSuffixPrefix := "tf-test-" + fmt.Sprintf("%029s", "")
884+
invalidPrefix := "tf-test-" + fmt.Sprintf("%047s", "")
885+
886+
acctest.VcrTest(t, resource.TestCase{
887+
PreCheck: func() { acctest.AccTestPreCheck(t) },
888+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
889+
Steps: []resource.TestStep{
890+
{
891+
Config: testAccComputeInstanceTemplate_withNamePrefix(normalPrefix),
892+
},
893+
{
894+
ResourceName: "google_compute_instance_template.foobar",
895+
ImportState: true,
896+
ImportStateVerify: true,
897+
ImportStateVerifyIgnore: []string{"name_prefix"},
898+
},
899+
{
900+
Config: testAccComputeInstanceTemplate_withNamePrefix(invalidPrefix),
901+
PlanOnly: true,
902+
ExpectError: regexp.MustCompile("cannot be longer than 54 characters"),
903+
},
904+
{
905+
Config: testAccComputeInstanceTemplate_withNamePrefix(reducedSuffixPrefix),
906+
},
907+
{
908+
ResourceName: "google_compute_instance_template.foobar",
909+
ImportState: true,
910+
ImportStateVerify: true,
911+
ImportStateVerifyIgnore: []string{"name_prefix"},
912+
},
913+
},
914+
})
915+
}
916+
878917
func TestAccComputeInstanceTemplate_withScratchDisk(t *testing.T) {
879918
t.Parallel()
880919

@@ -2609,6 +2648,28 @@ resource "google_compute_instance_template" "foobar" {
26092648
`, suffix, suffix)
26102649
}
26112650

2651+
func testAccComputeInstanceTemplate_withNamePrefix(prefix string) string {
2652+
return fmt.Sprintf(`
2653+
data "google_compute_image" "my_image" {
2654+
family = "debian-12"
2655+
project = "debian-cloud"
2656+
}
2657+
resource "google_compute_instance_template" "foobar" {
2658+
name_prefix = "%s"
2659+
machine_type = "n1-standard-1" // can't be e2 because of local-ssd
2660+
can_ip_forward = false
2661+
disk {
2662+
source_image = data.google_compute_image.my_image.name
2663+
auto_delete = true
2664+
boot = true
2665+
}
2666+
network_interface {
2667+
network = "default"
2668+
}
2669+
}
2670+
`, prefix)
2671+
}
2672+
26122673
func testAccComputeInstanceTemplate_with375GbScratchDisk(suffix string) string {
26132674
return fmt.Sprintf(`
26142675
data "google_compute_image" "my_image" {

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.erb

+9-4
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ func ResourceComputeRegionInstanceTemplate() *schema.Resource {
7777
Description: `Creates a unique name beginning with the specified prefix. Conflicts with name.`,
7878
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
7979
// https://cloud.google.com/compute/docs/reference/latest/instanceTemplates#resource
80-
// uuid is 26 characters, limit the prefix to 37.
80+
// uuid is 9 characters, limit the prefix to 54.
8181
value := v.(string)
82-
if len(value) > 37 {
82+
if len(value) > 54 {
8383
errors = append(errors, fmt.Errorf(
84-
"%q cannot be longer than 37 characters, name is limited to 63", k))
84+
"%q cannot be longer than 54 characters, name is limited to 63", k))
8585
}
8686
return
8787
},
@@ -1144,7 +1144,12 @@ func resourceComputeRegionInstanceTemplateCreate(d *schema.ResourceData, meta in
11441144
if v, ok := d.GetOk("name"); ok {
11451145
itName = v.(string)
11461146
} else if v, ok := d.GetOk("name_prefix"); ok {
1147-
itName = id.PrefixedUniqueId(v.(string))
1147+
prefix := v.(string)
1148+
if len(prefix) > 37 {
1149+
itName = tpgresource.ReducedPrefixedUniqueId(prefix)
1150+
} else {
1151+
itName = id.PrefixedUniqueId(prefix)
1152+
}
11481153
} else {
11491154
itName = id.UniqueId()
11501155
}

mmv1/third_party/terraform/tpgresource/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/hashicorp/go-cty/cty"
2323
fwDiags "github.com/hashicorp/terraform-plugin-framework/diag"
2424
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
2526
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2627
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2728
"golang.org/x/exp/maps"
@@ -878,3 +879,16 @@ func DefaultProviderZone(_ context.Context, diff *schema.ResourceDiff, meta inte
878879

879880
return nil
880881
}
882+
883+
// id.UniqueId() returns a timestamp + incremental hash
884+
// This function truncates the timestamp to provide a prefix + 9 using
885+
// YYmmdd + last 3 digits of the incremental hash
886+
func ReducedPrefixedUniqueId(prefix string) string {
887+
// uniqueID is timestamp + 8 digit counter (YYYYmmddHHMMSSssss + 12345678)
888+
uniqueId := id.PrefixedUniqueId("")
889+
// last three digits of the counter (678)
890+
counter := uniqueId[len(uniqueId)-3:]
891+
// YYmmdd of date
892+
date := uniqueId[2:8]
893+
return prefix + date + counter
894+
}

mmv1/third_party/terraform/website/docs/guides/version_6_upgrade.html.markdown

+7-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ terraform {
8888

8989
## Provider
9090

91-
### Provider-level change example header
91+
### Compute: `name_prefix` max length has been extended from 37 to 54 characters
9292

93-
Description of the change and how users should adjust their configuration (if needed).
93+
Affected resources: `google_compute_instance_template`, `google_compute_region_instance_template`, `google_compute_ssl_certificate`,
94+
and `google_compute_region_ssl_certificate`
95+
96+
Previously, the max length of `name_prefix` was 37 characters since the autogenerated UUID suffix was 26 characters which combined to
97+
the total max length for names of 63 characters.
98+
In 6.0, providing a `name_prefix` larger than 37 characters will prompt the provider to use a shortened suffix of only 9 characters, leading to a new max of 54 characters for `name_prefix`. This shortened suffix is inevitably more prone to collisions, so use the longer max `name_prefix` length with caution.
9499

95100
## Datasources
96101

mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown

+8-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,14 @@ The following arguments are supported:
295295
this blank, Terraform will auto-generate a unique name.
296296

297297
* `name_prefix` - (Optional) Creates a unique name beginning with the specified
298-
prefix. Conflicts with `name`.
298+
prefix. Conflicts with `name`. Max length is 54 characters.
299+
Prefixes with lengths longer than 37 characters will use a shortened
300+
UUID that will be more prone to collisions.
301+
302+
Resulting name for a `name_prefix` <= 37 characters:
303+
`name_prefix` + YYYYmmddHHSSssss + 8 digit incremental counter
304+
Resulting name for a `name_prefix` 38 - 54 characters:
305+
`name_prefix` + YYmmdd + 3 digit incremental counter
299306

300307
* `can_ip_forward` - (Optional) Whether to allow sending and receiving of
301308
packets with non-matching source or destination IPs. This defaults to false.

mmv1/third_party/terraform/website/docs/r/compute_region_instance_template.html.markdown

+8-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ The following arguments are supported:
307307
this blank, Terraform will auto-generate a unique name.
308308

309309
* `name_prefix` - (Optional) Creates a unique name beginning with the specified
310-
prefix. Conflicts with `name`.
310+
prefix. Conflicts with `name`. Max length is 54 characters.
311+
Prefixes with lengths longer than 37 characters will use a shortened
312+
UUID that will be more prone to collisions.
313+
314+
Resulting name for a `name_prefix` <= 37 characters:
315+
`name_prefix` + YYYYmmddHHSSssss + 8 digit incremental counter
316+
Resulting name for a `name_prefix` 38 - 54 characters:
317+
`name_prefix` + YYmmdd + 3 digit incremental counter
311318

312319
* `can_ip_forward` - (Optional) Whether to allow sending and receiving of
313320
packets with non-matching source or destination IPs. This defaults to false.

0 commit comments

Comments
 (0)