OpenStack: wait for volume availability when cleaning up

This commit is contained in:
Daisuke Takahashi 2018-09-13 18:29:24 +09:00
parent ed38fe9bef
commit 3b8aba2d24
2 changed files with 29 additions and 3 deletions

View File

@ -102,6 +102,23 @@ func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
return
}
// Wait for volume to become available.
status, err := GetVolumeStatus(blockStorageClient, s.volumeID)
if err != nil {
ui.Error(fmt.Sprintf(
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
return
}
if status != "available" {
ui.Say(fmt.Sprintf(
"Waiting for volume %s (volume id: %s) to become available...", s.VolumeName, s.volumeID))
if err := WaitForVolume(blockStorageClient, s.volumeID); err != nil {
ui.Error(fmt.Sprintf(
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
return
}
}
ui.Say(fmt.Sprintf("Deleting volume: %s ...", s.volumeID))
err = volumes.Delete(blockStorageClient, s.volumeID).ExtractErr()
if err != nil {

View File

@ -15,7 +15,7 @@ func WaitForVolume(blockStorageClient *gophercloud.ServiceClient, volumeID strin
numErrors := 0
for {
volume, err := volumes.Get(blockStorageClient, volumeID).Extract()
status, err := GetVolumeStatus(blockStorageClient, volumeID)
if err != nil {
errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if ok && (errCode.Actual == 500 || errCode.Actual == 404) {
@ -32,11 +32,11 @@ func WaitForVolume(blockStorageClient *gophercloud.ServiceClient, volumeID strin
return err
}
if volume.Status == "available" {
if status == "available" {
return nil
}
log.Printf("Waiting for volume creation status: %s", volume.Status)
log.Printf("Waiting for volume creation status: %s", status)
time.Sleep(2 * time.Second)
}
}
@ -65,3 +65,12 @@ func GetVolumeSize(imageClient *gophercloud.ServiceClient, imageID string) (int,
return volumeSizeGB, nil
}
func GetVolumeStatus(blockStorageClient *gophercloud.ServiceClient, volumeID string) (string, error) {
volume, err := volumes.Get(blockStorageClient, volumeID).Extract()
if err != nil {
return "", err
}
return volume.Status, nil
}