Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when updating server-connected disk #1128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions sakuracloud/resource_sakuracloud_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
125 changes: 125 additions & 0 deletions sakuracloud/resource_sakuracloud_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
`