Skip to content

Commit 44ee82f

Browse files
modular-magicianEdward Sun
andauthored
Revert "fix local-ssd update issue (#7327)" (#7760) (#5502)
This reverts commit 28fc8cef7abdb8e7e13ae8f640407be797904b51. Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Edward Sun <[email protected]>
1 parent c81c168 commit 44ee82f

File tree

4 files changed

+9
-192
lines changed

4 files changed

+9
-192
lines changed

.changelog/7760.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 can't update a instance with local ssd when stopping is needed on `google_compute_instance` (revert)
3+
```

google-beta/resource_compute_instance.go

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,6 @@ func forceNewIfNetworkIPNotUpdatableFunc(d TerraformResourceDiff) error {
9090
return nil
9191
}
9292

93-
// check if stopping_with_local_ssd_discard is the only updated field
94-
func onlyStoppingWithLocalSsdDiscardChanged(d *schema.ResourceData, resourceSchema map[string]*schema.Schema) bool {
95-
if d.HasChange("stopping_with_local_ssd_discard") {
96-
for field := range resourceSchema {
97-
if field == "stopping_with_local_ssd_discard" {
98-
continue
99-
}
100-
if d.HasChange(field) {
101-
return false
102-
}
103-
}
104-
return true
105-
}
106-
return false
107-
}
108-
109-
func hasLocalSsd(d *schema.ResourceData, config *Config) bool {
110-
n := d.Get("scratch_disk.#")
111-
return n != nil && n.(int) > 0
112-
}
113-
11493
func ResourceComputeInstance() *schema.Resource {
11594
return &schema.Resource{
11695
Create: resourceComputeInstanceCreate,
@@ -958,13 +937,6 @@ be from 0 to 999,999,999 inclusive.`,
958937
},
959938
},
960939
},
961-
"stopping_with_local_ssd_discard": {
962-
Type: schema.TypeBool,
963-
Optional: true,
964-
Default: false,
965-
Description: `Whether allow data on the local ssd to be discarded. The instance can not be stopped when a local ssd being attached to the instance.
966-
True to allow data to be discarded. Default is False. Setting true to allow the local ssd to be recreated. https://cloud.google.com/compute/docs/disks/local-ssd`,
967-
},
968940
},
969941
CustomizeDiff: customdiff.All(
970942
customdiff.If(
@@ -1252,13 +1224,6 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
12521224
delete(md, "startup-script")
12531225
}
12541226

1255-
// Explicitly set virtual fields to default values if unset
1256-
if _, ok := d.GetOkExists("stopping_with_local_ssd_discard"); !ok {
1257-
if err := d.Set("stopping_with_local_ssd_discard", false); err != nil {
1258-
return fmt.Errorf("Error setting stopping_with_local_ssd_discard: %s", err)
1259-
}
1260-
}
1261-
12621227
if err = d.Set("metadata", md); err != nil {
12631228
return fmt.Errorf("Error setting metadata: %s", err)
12641229
}
@@ -1516,16 +1481,6 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
15161481
return err
15171482
}
15181483

1519-
if onlyStoppingWithLocalSsdDiscardChanged(d, ResourceComputeInstance().Schema) {
1520-
if d.Get("stopping_with_local_ssd_discard") != nil {
1521-
if err := d.Set("stopping_with_local_ssd_discard", d.Get("stopping_with_local_ssd_discard")); err != nil {
1522-
return fmt.Errorf("Error reading/setting stopping_with_local_ssd_discard: %s", err)
1523-
}
1524-
}
1525-
// exit updating if stopping_with_local_ssd_discard, a vertual field, is the only field being updated
1526-
return nil
1527-
}
1528-
15291484
// Use beta api directly in order to read network_interface.fingerprint without having to put it in the schema.
15301485
// Change back to getInstance(config, d) once updating alias ips is GA.
15311486
instance, err := config.NewComputeClient(userAgent).Instances.Get(project, zone, d.Get("name").(string)).Do()
@@ -1961,13 +1916,6 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
19611916

19621917
needToStopInstanceBeforeUpdating := scopesChange || d.HasChange("service_account.0.email") || d.HasChange("machine_type") || d.HasChange("min_cpu_platform") || d.HasChange("enable_display") || d.HasChange("shielded_instance_config") || len(updatesToNIWhileStopped) > 0 || bootRequiredSchedulingChange || d.HasChange("advanced_machine_features")
19631918

1964-
if needToStopInstanceBeforeUpdating {
1965-
if hasLocalSsd(d, config) && !d.Get("stopping_with_local_ssd_discard").(bool) {
1966-
return fmt.Errorf("The instance has local SSD. Stopping insatnce will discard all data on the local SSD. " +
1967-
"Setting stopping_with_local_ssd_discard to true to allow the data on the local SSD to be discarded")
1968-
}
1969-
}
1970-
19711919
if d.HasChange("desired_status") && !needToStopInstanceBeforeUpdating {
19721920
desiredStatus := d.Get("desired_status").(string)
19731921

@@ -1980,17 +1928,9 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
19801928
return errwrap.Wrapf("Error starting instance: {{err}}", err)
19811929
}
19821930
} else if desiredStatus == "TERMINATED" {
1983-
if d.Get("stopping_with_local_ssd_discard").(bool) {
1984-
op, err = config.NewComputeClient(userAgent).Instances.Stop(project, zone, instance.Name).DiscardLocalSsd(true).Do()
1985-
if err != nil {
1986-
return err
1987-
}
1988-
} else {
1989-
// DiscardLocalSsd could happen only when stopping_with_local_ssd_discard = true
1990-
op, err = config.NewComputeClient(userAgent).Instances.Stop(project, zone, instance.Name).Do()
1991-
if err != nil {
1992-
return err
1993-
}
1931+
op, err = config.NewComputeClient(userAgent).Instances.Stop(project, zone, instance.Name).Do()
1932+
if err != nil {
1933+
return err
19941934
}
19951935
}
19961936
opErr := ComputeOperationWaitTime(
@@ -2014,8 +1954,8 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
20141954
"You can also stop it by setting desired_status = \"TERMINATED\", but the instance will not be restarted after the update.")
20151955
}
20161956

2017-
if statusBeforeUpdate != "TERMINATED" && d.Get("stopping_with_local_ssd_discard").(bool) {
2018-
op, err := config.NewComputeClient(userAgent).Instances.Stop(project, zone, instance.Name).DiscardLocalSsd(true).Do()
1957+
if statusBeforeUpdate != "TERMINATED" {
1958+
op, err := config.NewComputeClient(userAgent).Instances.Stop(project, zone, instance.Name).Do()
20191959
if err != nil {
20201960
return errwrap.Wrapf("Error stopping instance: {{err}}", err)
20211961
}

google-beta/resource_compute_instance_test.go

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func computeInstanceImportStep(zone, instanceName string, additionalImportIgnore
7878
// metadata is only read into state if set in the config
7979
// importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
8080
// it always takes metadata.startup-script
81-
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script", "stopping_with_local_ssd_discard"}
81+
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script"}
8282

8383
return resource.TestStep{
8484
ResourceName: "google_compute_instance.foobar",
@@ -777,65 +777,6 @@ func TestAccComputeInstance_with18TbScratchDisk(t *testing.T) {
777777
})
778778
}
779779

780-
func TestAccComputeInstance_scratchDiskUpdate(t *testing.T) {
781-
t.Parallel()
782-
783-
var instance compute.Instance
784-
var instanceName = fmt.Sprintf("tf-test-%s", RandString(t, 10))
785-
786-
VcrTest(t, resource.TestCase{
787-
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
788-
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
789-
Steps: []resource.TestStep{
790-
{
791-
Config: testAccComputeInstance_scratchDiskUpdate(instanceName, "n1-standard-2"),
792-
Check: resource.ComposeTestCheckFunc(
793-
testAccCheckComputeInstanceExists(
794-
t, "google_compute_instance.foobar", &instance),
795-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME"}),
796-
),
797-
},
798-
{
799-
Config: testAccComputeInstance_scratchDiskUpdate(instanceName, "n2-standard-4"),
800-
ExpectError: regexp.MustCompile("Error: The instance has local SSD. Stopping insatnce will discard all data on the local SSD. Setting stopping_with_local_ssd_discard to true to allow the data on the local SSD to be discarded"),
801-
},
802-
{
803-
Config: testAccComputeInstance_scratchDiskUpdate2(instanceName, "n1-standard-2", "false"),
804-
Check: resource.ComposeTestCheckFunc(
805-
testAccCheckComputeInstanceExists(
806-
t, "google_compute_instance.foobar", &instance),
807-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME"}),
808-
),
809-
},
810-
{
811-
Config: testAccComputeInstance_scratchDiskUpdate(instanceName, "n2-standard-4"),
812-
ExpectError: regexp.MustCompile("Error: The instance has local SSD. Stopping insatnce will discard all data on the local SSD. Setting stopping_with_local_ssd_discard to true to allow the data on the local SSD to be discarded"),
813-
},
814-
{
815-
Config: testAccComputeInstance_scratchDiskUpdate2(instanceName, "n2-standard-4", "true"),
816-
Check: resource.ComposeTestCheckFunc(
817-
testAccCheckComputeInstanceExists(
818-
t, "google_compute_instance.foobar", &instance),
819-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME"}),
820-
),
821-
},
822-
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
823-
{
824-
Config: testAccComputeInstance_scratchDiskUpdate2(instanceName, "n1-standard-2", "true"),
825-
Check: resource.ComposeTestCheckFunc(
826-
testAccCheckComputeInstanceExists(
827-
t, "google_compute_instance.foobar", &instance),
828-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME"}),
829-
),
830-
},
831-
{
832-
Config: testAccComputeInstance_scratchDiskUpdate2(instanceName, "n2-standard-4", "false"),
833-
ExpectError: regexp.MustCompile("Error: The instance has local SSD. Stopping insatnce will discard all data on the local SSD. Setting stopping_with_local_ssd_discard to true to allow the data on the local SSD to be discarded"),
834-
},
835-
},
836-
})
837-
}
838-
839780
func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
840781
t.Parallel()
841782

@@ -4680,70 +4621,6 @@ resource "google_compute_instance" "foobar" {
46804621
}`, instance)
46814622
}
46824623

4683-
func testAccComputeInstance_scratchDiskUpdate(instance, machinetype string) string {
4684-
return fmt.Sprintf(`
4685-
data "google_compute_image" "my_image" {
4686-
family = "debian-11"
4687-
project = "debian-cloud"
4688-
}
4689-
4690-
resource "google_compute_instance" "foobar" {
4691-
name = "%s"
4692-
machine_type = "%s"
4693-
zone = "us-central1-a"
4694-
allow_stopping_for_update = true
4695-
4696-
boot_disk {
4697-
initialize_params {
4698-
image = data.google_compute_image.my_image.self_link
4699-
}
4700-
}
4701-
4702-
scratch_disk {
4703-
interface = "NVME"
4704-
}
4705-
4706-
network_interface {
4707-
network = "default"
4708-
}
4709-
4710-
}
4711-
`, instance, machinetype)
4712-
}
4713-
4714-
func testAccComputeInstance_scratchDiskUpdate2(instance, machinetype, discard string) string {
4715-
return fmt.Sprintf(`
4716-
data "google_compute_image" "my_image" {
4717-
family = "debian-11"
4718-
project = "debian-cloud"
4719-
}
4720-
4721-
resource "google_compute_instance" "foobar" {
4722-
name = "%s"
4723-
machine_type = "%s"
4724-
zone = "us-central1-a"
4725-
allow_stopping_for_update = true
4726-
4727-
boot_disk {
4728-
initialize_params {
4729-
image = data.google_compute_image.my_image.self_link
4730-
}
4731-
}
4732-
4733-
scratch_disk {
4734-
interface = "NVME"
4735-
}
4736-
4737-
network_interface {
4738-
network = "default"
4739-
}
4740-
4741-
stopping_with_local_ssd_discard = "%s"
4742-
4743-
}
4744-
`, instance, machinetype, discard)
4745-
}
4746-
47474624
func testAccComputeInstance_serviceAccount(instance string) string {
47484625
return fmt.Sprintf(`
47494626
data "google_compute_image" "my_image" {

website/docs/r/compute_instance.html.markdown

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ is desired, you will need to modify your state file manually using
186186
in `guest-os-features`, and `network_interface.0.nic-type` must be `GVNIC`
187187
in order for this setting to take effect.
188188

189-
* `stopping_with_local_ssd_discard` - (Optional) Whether allow data on the local ssd to be discarded. The instance can not be stopped when a local ssd being attached to the instance.
190-
True to allow data to be discarded. Default is False. Setting true to allow the local ssd to be recreated. https://cloud.google.com/compute/docs/disks/local-ssd
191-
192189
---
193190

194191
<a name="nested_boot_disk"></a>The `boot_disk` block supports:

0 commit comments

Comments
 (0)