provider/openstack: Fixing boot volume interference

This commit fixes the situation where instances with both a
bootable volume and attached block storage volumes were reporting
an inconsistent state.
This commit is contained in:
Joe Topjian 2016-07-14 15:29:31 +00:00
parent 4ca5c948b6
commit 411ad21fd8
2 changed files with 60 additions and 6 deletions

View File

@ -1436,12 +1436,25 @@ func getVolumeAttachments(computeClient *gophercloud.ServiceClient, d *schema.Re
return err return err
} }
vols := make([]map[string]interface{}, len(attachments)) var vols []map[string]interface{}
for i, attachment := range attachments { for _, attachment := range attachments {
vols[i] = make(map[string]interface{}) // ignore the volume if it is attached as a root device
vols[i]["id"] = attachment.ID rootDevFound := false
vols[i]["volume_id"] = attachment.VolumeID for _, rootDev := range []string{"/dev/vda", "/dev/xda", "/dev/sda", "/dev/xvda"} {
vols[i]["device"] = attachment.Device if attachment.Device == rootDev {
rootDevFound = true
}
}
if rootDevFound {
continue
}
vol := make(map[string]interface{})
vol["id"] = attachment.ID
vol["volume_id"] = attachment.VolumeID
vol["device"] = attachment.Device
vols = append(vols, vol)
} }
log.Printf("[INFO] Volume attachments: %v", vols) log.Printf("[INFO] Volume attachments: %v", vols)
d.Set("volume", vols) d.Set("volume", vols)

View File

@ -388,6 +388,47 @@ func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) {
}) })
} }
func TestAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume(t *testing.T) {
var instance servers.Server
var testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume = fmt.Sprintf(`
resource "openstack_blockstorage_volume_v1" "volume_1" {
name = "volume_1"
size = 1
}
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
security_groups = ["default"]
block_device {
uuid = "%s"
source_type = "image"
volume_size = 2
boot_index = 0
destination_type = "volume"
delete_on_termination = true
}
volume {
volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
}
}`,
os.Getenv("OS_IMAGE_ID"))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeV2InstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance),
),
},
},
})
}
func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) { func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
var instance servers.Server var instance servers.Server
var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(` var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(`