From 1eee0546616f56ce67eef3ef44a9bee1dfcdbdd2 Mon Sep 17 00:00:00 2001 From: ak1ra24 Date: Sat, 16 Dec 2023 14:54:11 +0900 Subject: [PATCH] Fix error when updating server-connected disk --- sakuracloud/resource_sakuracloud_disk.go | 20 +++ sakuracloud/resource_sakuracloud_disk_test.go | 125 ++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/sakuracloud/resource_sakuracloud_disk.go b/sakuracloud/resource_sakuracloud_disk.go index b9ea6f8d..8f0936f1 100644 --- a/sakuracloud/resource_sakuracloud_disk.go +++ b/sakuracloud/resource_sakuracloud_disk.go @@ -24,6 +24,7 @@ import ( "github.com/sacloud/iaas-api-go" "github.com/sacloud/iaas-api-go/accessor" "github.com/sacloud/iaas-api-go/helper/cleanup" + "github.com/sacloud/iaas-api-go/helper/power" "github.com/sacloud/iaas-api-go/types" "github.com/sacloud/iaas-service-go/setup" "github.com/sacloud/terraform-provider-sakuracloud/internal/desc" @@ -191,6 +192,25 @@ func resourceSakuraCloudDiskDelete(ctx context.Context, d *schema.ResourceData, return diag.Errorf("could not read SakuraCloud Disk[%s]: %s", d.Id(), err) } + serverID := disk.GetServerID() + if serverID != 0 { + serverOp := iaas.NewServerOp(client) + server, err := serverOp.Read(ctx, zone, serverID) + if err != nil { + return diag.Errorf("could not read SakuraCloud Server[%s]: %s", d.Id(), err) + } + + if server.InstanceStatus.IsUp() { + if err := power.ShutdownServer(ctx, serverOp, zone, server.ID, true); err != nil { + return diag.Errorf("stopping SakuraCloud Server[%s] is failed: %s", server.ID, err) + } + } + + if err := diskOp.DisconnectFromServer(ctx, zone, sakuraCloudID(d.Id())); err != nil { + return diag.Errorf("disconnect from server[%s] is failed: %s", d.Id(), err) + } + } + if err := cleanup.DeleteDisk(ctx, client, zone, disk.ID, client.checkReferencedOption()); err != nil { return diag.Errorf("deleting SakuraCloud Disk[%s] is failed: %s", d.Id(), err) } diff --git a/sakuracloud/resource_sakuracloud_disk_test.go b/sakuracloud/resource_sakuracloud_disk_test.go index 9ac3c8f5..96acd3b3 100644 --- a/sakuracloud/resource_sakuracloud_disk_test.go +++ b/sakuracloud/resource_sakuracloud_disk_test.go @@ -79,6 +79,77 @@ func TestAccSakuraCloudDisk_basic(t *testing.T) { }) } +func TestAccSakuraCloudDisk_with_Server(t *testing.T) { + diskResourceName := "sakuracloud_disk.foobar" + serverResourceName := "sakuracloud_server.foobar" + rand := randomName() + + var disk iaas.Disk + var server iaas.Server + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: resource.ComposeTestCheckFunc( + testCheckSakuraCloudDiskDestroy, + testCheckSakuraCloudServerDestroy, + ), + Steps: []resource.TestStep{ + { + Config: buildConfigWithArgs(testAccSakuraCloudDisk_with_Server, rand), + Check: resource.ComposeTestCheckFunc( + testCheckSakuraCloudDiskExists(diskResourceName, &disk), + testCheckSakuraCloudDiskAttributes(&disk), + resource.TestCheckResourceAttr(diskResourceName, "name", rand), + resource.TestCheckResourceAttr(diskResourceName, "description", "description"), + resource.TestCheckResourceAttr(diskResourceName, "plan", "ssd"), + resource.TestCheckResourceAttr(diskResourceName, "connector", "virtio"), + resource.TestCheckResourceAttr(diskResourceName, "size", "20"), + resource.TestCheckResourceAttr(diskResourceName, "tags.#", "2"), + resource.TestCheckResourceAttr(diskResourceName, "tags.0", "tag1"), + resource.TestCheckResourceAttr(diskResourceName, "tags.1", "tag2"), + testCheckSakuraCloudServerExists(serverResourceName, &server), + testCheckSakuraCloudServerAttributes(&server), + resource.TestCheckResourceAttr(serverResourceName, "name", rand), + resource.TestCheckResourceAttr(serverResourceName, "core", "1"), + resource.TestCheckResourceAttr(serverResourceName, "memory", "2"), + resource.TestCheckResourceAttr(serverResourceName, "disks.#", "1"), + resource.TestCheckResourceAttr(serverResourceName, "interface_driver", "virtio"), + resource.TestCheckResourceAttr(serverResourceName, "network_interface.#", "1"), + resource.TestCheckResourceAttr(serverResourceName, "network_interface.0.upstream", "shared"), + resource.TestCheckResourceAttrSet(serverResourceName, "network_interface.0.mac_address"), + resource.TestCheckResourceAttrSet(serverResourceName, "ip_address"), + ), + }, + { + Config: buildConfigWithArgs(testAccSakuraCloudDisk_with_Server_update, rand), + Check: resource.ComposeTestCheckFunc( + testCheckSakuraCloudDiskExists(diskResourceName, &disk), + testCheckSakuraCloudDiskAttributes(&disk), + resource.TestCheckResourceAttr(diskResourceName, "name", rand+"-upd"), + resource.TestCheckResourceAttr(diskResourceName, "description", "description-upd"), + resource.TestCheckResourceAttr(diskResourceName, "plan", "ssd"), + resource.TestCheckResourceAttr(diskResourceName, "connector", "virtio"), + resource.TestCheckResourceAttr(diskResourceName, "size", "40"), + resource.TestCheckResourceAttr(diskResourceName, "tags.#", "2"), + resource.TestCheckResourceAttr(diskResourceName, "tags.0", "tag1-upd"), + resource.TestCheckResourceAttr(diskResourceName, "tags.1", "tag2-upd"), + testCheckSakuraCloudServerExists(serverResourceName, &server), + // testCheckSakuraCloudServerAttributes(&server), + resource.TestCheckResourceAttr(serverResourceName, "name", rand+"-upd"), + resource.TestCheckResourceAttr(serverResourceName, "core", "2"), + resource.TestCheckResourceAttr(serverResourceName, "memory", "4"), + resource.TestCheckResourceAttr(serverResourceName, "disks.#", "1"), + resource.TestCheckResourceAttr(serverResourceName, "interface_driver", "virtio"), + resource.TestCheckResourceAttr(serverResourceName, "network_interface.#", "1"), + resource.TestCheckResourceAttr(serverResourceName, "network_interface.0.upstream", "shared"), + resource.TestCheckResourceAttrSet(serverResourceName, "network_interface.0.mac_address"), + resource.TestCheckResourceAttrSet(serverResourceName, "ip_address"), + ), + }, + }, + }) +} + func testCheckSakuraCloudDiskExists(n string, disk *iaas.Disk) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -233,3 +304,57 @@ resource "sakuracloud_disk" "foobar" { description = "description-upd" tags = ["tag1-upd", "tag2-upd"] }` + +var testAccSakuraCloudDisk_with_Server = ` +data "sakuracloud_archive" "ubuntu" { + os_type = "ubuntu2004" +} + +resource "sakuracloud_disk" "foobar" { + name = "{{ .arg0 }}" + plan = "ssd" + connector = "virtio" + size = 20 + distant_from = ["111111111111"] + source_archive_id = data.sakuracloud_archive.ubuntu.id + description = "description" + tags = ["tag1", "tag2"] +} + +resource sakuracloud_server "foobar" { + name = "{{ .arg0 }}" + disks = [sakuracloud_disk.foobar.id] + network_interface { + upstream = "shared" + } + core = 1 + memory = 2 +} +` + +var testAccSakuraCloudDisk_with_Server_update = ` +data "sakuracloud_archive" "ubuntu" { + os_type = "ubuntu2004" +} + +resource "sakuracloud_disk" "foobar" { + name = "{{ .arg0 }}-upd" + plan = "ssd" + connector = "virtio" + size = 40 + distant_from = ["111111111111"] + source_archive_id = data.sakuracloud_archive.ubuntu.id + description = "description-upd" + tags = ["tag1-upd", "tag2-upd"] +} + +resource sakuracloud_server "foobar" { + name = "{{ .arg0 }}-upd" + disks = [sakuracloud_disk.foobar.id] + network_interface { + upstream = "shared" + } + core = 2 + memory = 4 +} +`