Skip to content

Commit dfdf6b5

Browse files
Changed order of updates for min_cpu_platform and machine_type (#8249) (#5911)
* Changed order of updates for min_cpu_platform and machine_type Resolved hashicorp/terraform-provider-google#14945 * Added allow_stopping_for_update = true * Added allow_stopping_for_update to ImportStateVerifyIgnore * Added diff suppress and clarified how to 'unset' the field Signed-off-by: Modular Magician <[email protected]>
1 parent e0b0b72 commit dfdf6b5

File tree

4 files changed

+137
-26
lines changed

4 files changed

+137
-26
lines changed

.changelog/8249.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed logic when unsetting `google_compute_instance.min_cpu_platform` and switching to a `machine_type` that does not support `min_cpu_platform` at the same time
3+
```

google-beta/resource_compute_instance_test.go

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,74 @@ import (
2323
compute "google.golang.org/api/compute/v0.beta"
2424
)
2525

26+
func TestMinCpuPlatformDiffSuppress(t *testing.T) {
27+
cases := map[string]struct {
28+
Old, New string
29+
ExpectDiffSuppress bool
30+
}{
31+
"state: empty, conf: AUTOMATIC": {
32+
Old: "",
33+
New: "AUTOMATIC",
34+
ExpectDiffSuppress: true,
35+
},
36+
"state: empty, conf: automatic": {
37+
Old: "",
38+
New: "automatic",
39+
ExpectDiffSuppress: true,
40+
},
41+
"state: empty, conf: AuToMaTiC": {
42+
Old: "",
43+
New: "AuToMaTiC",
44+
ExpectDiffSuppress: true,
45+
},
46+
"state: empty, conf: Intel Haswell": {
47+
Old: "",
48+
New: "Intel Haswell",
49+
ExpectDiffSuppress: false,
50+
},
51+
// This case should never happen due to the field being
52+
// Optional + Computed; however, including for completeness.
53+
"state: Intel Haswell, conf: empty": {
54+
Old: "Intel Haswell",
55+
New: "",
56+
ExpectDiffSuppress: false,
57+
},
58+
// These cases should never happen given current API behavior; testing
59+
// in case API behavior changes in the future.
60+
"state: AUTOMATIC, conf: Intel Haswell": {
61+
Old: "AUTOMATIC",
62+
New: "Intel Haswell",
63+
ExpectDiffSuppress: false,
64+
},
65+
"state: Intel Haswell, conf: AUTOMATIC": {
66+
Old: "Intel Haswell",
67+
New: "AUTOMATIC",
68+
ExpectDiffSuppress: false,
69+
},
70+
"state: AUTOMATIC, conf: empty": {
71+
Old: "AUTOMATIC",
72+
New: "",
73+
ExpectDiffSuppress: true,
74+
},
75+
"state: automatic, conf: empty": {
76+
Old: "automatic",
77+
New: "",
78+
ExpectDiffSuppress: true,
79+
},
80+
"state: AuToMaTiC, conf: empty": {
81+
Old: "AuToMaTiC",
82+
New: "",
83+
ExpectDiffSuppress: true,
84+
},
85+
}
86+
87+
for tn, tc := range cases {
88+
if tpgcompute.ComputeInstanceMinCpuPlatformEmptyOrAutomaticDiffSuppress("min_cpu_platform", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
89+
t.Errorf("bad: %s, %q => %q expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
90+
}
91+
}
92+
}
93+
2694
func computeInstanceImportStep(zone, instanceName string, additionalImportIgnores []string) resource.TestStep {
2795
// metadata is only read into state if set in the config
2896
// importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
@@ -1431,7 +1499,15 @@ func TestAccComputeInstance_minCpuPlatform(t *testing.T) {
14311499
testAccCheckComputeInstanceHasMinCpuPlatform(&instance, "Intel Haswell"),
14321500
),
14331501
},
1434-
computeInstanceImportStep("us-east1-d", instanceName, []string{}),
1502+
computeInstanceImportStep("us-east1-d", instanceName, []string{"allow_stopping_for_update"}),
1503+
{
1504+
Config: testAccComputeInstance_minCpuPlatform_remove(instanceName),
1505+
Check: resource.ComposeTestCheckFunc(
1506+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
1507+
testAccCheckComputeInstanceHasMinCpuPlatform(&instance, ""),
1508+
),
1509+
},
1510+
computeInstanceImportStep("us-east1-d", instanceName, []string{"allow_stopping_for_update"}),
14351511
},
14361512
})
14371513
}
@@ -5294,6 +5370,35 @@ resource "google_compute_instance" "foobar" {
52945370
}
52955371
52965372
min_cpu_platform = "Intel Haswell"
5373+
allow_stopping_for_update = true
5374+
}
5375+
`, instance)
5376+
}
5377+
5378+
func testAccComputeInstance_minCpuPlatform_remove(instance string) string {
5379+
return fmt.Sprintf(`
5380+
data "google_compute_image" "my_image" {
5381+
family = "debian-11"
5382+
project = "debian-cloud"
5383+
}
5384+
5385+
resource "google_compute_instance" "foobar" {
5386+
name = "%s"
5387+
machine_type = "e2-micro"
5388+
zone = "us-east1-d"
5389+
5390+
boot_disk {
5391+
initialize_params {
5392+
image = data.google_compute_image.my_image.self_link
5393+
}
5394+
}
5395+
5396+
network_interface {
5397+
network = "default"
5398+
}
5399+
5400+
min_cpu_platform = "AuToMaTiC"
5401+
allow_stopping_for_update = true
52975402
}
52985403
`, instance)
52995404
}

google-beta/services/compute/resource_compute_instance.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func forceNewIfNetworkIPNotUpdatableFunc(d tpgresource.TerraformResourceDiff) er
9696
return nil
9797
}
9898

99+
// User may specify AUTOMATIC using any case; the API will accept it and return an empty string.
100+
func ComputeInstanceMinCpuPlatformEmptyOrAutomaticDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
101+
old = strings.ToLower(old)
102+
new = strings.ToLower(new)
103+
defaultVal := "automatic"
104+
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
105+
}
106+
99107
func ResourceComputeInstance() *schema.Resource {
100108
return &schema.Resource{
101109
Create: resourceComputeInstanceCreate,
@@ -590,10 +598,11 @@ func ResourceComputeInstance() *schema.Resource {
590598
},
591599

592600
"min_cpu_platform": {
593-
Type: schema.TypeString,
594-
Optional: true,
595-
Computed: true,
596-
Description: `The minimum CPU platform specified for the VM instance.`,
601+
Type: schema.TypeString,
602+
Optional: true,
603+
Computed: true,
604+
Description: `The minimum CPU platform specified for the VM instance.`,
605+
DiffSuppressFunc: ComputeInstanceMinCpuPlatformEmptyOrAutomaticDiffSuppress,
597606
},
598607

599608
"project": {
@@ -2008,40 +2017,34 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
20082017
}
20092018
}
20102019

2011-
if d.HasChange("machine_type") {
2012-
mt, err := tpgresource.ParseMachineTypesFieldValue(d.Get("machine_type").(string), d, config)
2013-
if err != nil {
2014-
return err
2015-
}
2016-
req := &compute.InstancesSetMachineTypeRequest{
2017-
MachineType: mt.RelativeLink(),
2020+
if d.HasChange("min_cpu_platform") {
2021+
minCpuPlatform := d.Get("min_cpu_platform")
2022+
req := &compute.InstancesSetMinCpuPlatformRequest{
2023+
MinCpuPlatform: minCpuPlatform.(string),
20182024
}
2019-
op, err := config.NewComputeClient(userAgent).Instances.SetMachineType(project, zone, instance.Name, req).Do()
2025+
op, err := config.NewComputeClient(userAgent).Instances.SetMinCpuPlatform(project, zone, instance.Name, req).Do()
20202026
if err != nil {
20212027
return err
20222028
}
2023-
opErr := ComputeOperationWaitTime(config, op, project, "updating machinetype", userAgent, d.Timeout(schema.TimeoutUpdate))
2029+
opErr := ComputeOperationWaitTime(config, op, project, "updating min cpu platform", userAgent, d.Timeout(schema.TimeoutUpdate))
20242030
if opErr != nil {
20252031
return opErr
20262032
}
20272033
}
20282034

2029-
if d.HasChange("min_cpu_platform") {
2030-
minCpuPlatform, ok := d.GetOk("min_cpu_platform")
2031-
// Even though you don't have to set minCpuPlatform on create, you do have to set it to an
2032-
// actual value on update. "Automatic" is the default. This will be read back from the API as empty,
2033-
// so we don't need to worry about diffs.
2034-
if !ok {
2035-
minCpuPlatform = "Automatic"
2035+
if d.HasChange("machine_type") {
2036+
mt, err := tpgresource.ParseMachineTypesFieldValue(d.Get("machine_type").(string), d, config)
2037+
if err != nil {
2038+
return err
20362039
}
2037-
req := &compute.InstancesSetMinCpuPlatformRequest{
2038-
MinCpuPlatform: minCpuPlatform.(string),
2040+
req := &compute.InstancesSetMachineTypeRequest{
2041+
MachineType: mt.RelativeLink(),
20392042
}
2040-
op, err := config.NewComputeClient(userAgent).Instances.SetMinCpuPlatform(project, zone, instance.Name, req).Do()
2043+
op, err := config.NewComputeClient(userAgent).Instances.SetMachineType(project, zone, instance.Name, req).Do()
20412044
if err != nil {
20422045
return err
20432046
}
2044-
opErr := ComputeOperationWaitTime(config, op, project, "updating min cpu platform", userAgent, d.Timeout(schema.TimeoutUpdate))
2047+
opErr := ComputeOperationWaitTime(config, op, project, "updating machinetype", userAgent, d.Timeout(schema.TimeoutUpdate))
20452048
if opErr != nil {
20462049
return opErr
20472050
}

website/docs/d/compute_instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The following arguments are supported:
6161

6262
* `metadata` - Metadata key/value pairs made available within the instance.
6363

64-
* `min_cpu_platform` - The minimum CPU platform specified for the VM instance.
64+
* `min_cpu_platform` - The minimum CPU platform specified for the VM instance. Set to "AUTOMATIC" to remove a previously-set value.
6565

6666
* `scheduling` - The scheduling strategy being used by the instance. Structure is [documented below](#nested_scheduling)
6767

0 commit comments

Comments
 (0)