Skip to content

Commit 8ab15f1

Browse files
zhenggulbajolet-hashicorp
authored andcommitted
restruct volume cleanup logic
1 parent 75a9aa1 commit 8ab15f1

1 file changed

Lines changed: 5 additions & 22 deletions

File tree

builder/openstack/step_create_volume.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ type StepCreateVolume struct {
1818
VolumeType string
1919
VolumeAvailabilityZone string
2020
volumeID string
21-
doCleanup bool
2221
}
2322

2423
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@@ -79,15 +78,14 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
7978
// Wait for volume to become available.
8079
ui.Say(fmt.Sprintf("Waiting for volume %s (volume id: %s) to become available...", config.VolumeName, volume.ID))
8180
if err := WaitForVolume(blockStorageClient, volume.ID); err != nil {
81+
// Put the error volume here for cleanup.
82+
s.volumeID = volume.ID
8283
err := fmt.Errorf("Error waiting for volume: %s", err)
8384
state.Put("error", err)
8485
ui.Error(err.Error())
8586
return multistep.ActionHalt
8687
}
8788

88-
// Volume was created, so remember to clean it up.
89-
s.doCleanup = true
90-
9189
// Set the Volume ID in the state.
9290
ui.Message(fmt.Sprintf("Volume ID: %s", volume.ID))
9391
state.Put("volume_id", volume.ID)
@@ -97,7 +95,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
9795
}
9896

9997
func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
100-
if !s.doCleanup {
98+
if s.volumeID == "" {
10199
return
102100
}
103101

@@ -111,24 +109,9 @@ func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
111109
return
112110
}
113111

114-
// Wait for volume to become available.
115-
status, err := GetVolumeStatus(blockStorageClient, s.volumeID)
116-
if err != nil {
117-
ui.Error(fmt.Sprintf(
118-
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
119-
return
120-
}
121-
122-
if status != "available" {
123-
ui.Say(fmt.Sprintf(
124-
"Waiting for volume %s (volume id: %s) to become available...", s.VolumeName, s.volumeID))
125-
if err := WaitForVolume(blockStorageClient, s.volumeID); err != nil {
126-
ui.Error(fmt.Sprintf(
127-
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
128-
return
129-
}
130-
}
131112
ui.Say(fmt.Sprintf("Deleting volume: %s ...", s.volumeID))
113+
114+
// Delete the volume in any status if exists.
132115
err = volumes.Delete(blockStorageClient, s.volumeID, volumes.DeleteOpts{}).ExtractErr()
133116
if err != nil {
134117
ui.Error(fmt.Sprintf(

0 commit comments

Comments
 (0)