Skip to content

Commit

Permalink
Wait for volume to finish detaching when deleting resource vkcs_compu…
Browse files Browse the repository at this point in the history
…te_volume_attach
  • Loading branch information
schirevko authored and ftersin committed Oct 12, 2023
1 parent 55b48b4 commit 433d10b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ description: |-
---

# VKCS Provider's changelog
#### v0.5.1 (unreleased)
- Wait for volume to finish detaching when deleting resource vkcs_compute_volume_attach

#### v0.5.0
- Add Direct Connect resources and data sources
- Fix Kubernetes addon state refresh panic on error
Expand Down
21 changes: 11 additions & 10 deletions vkcs/blockstorage/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const (

var (
bsVolumeStatusBuild = "creating"
bsVolumeStatusActive = "available"
bsVolumeStatusInUse = "in-use"
BSVolumeStatusActive = "available"
BSVolumeStatusInUse = "in-use"
BSVolumeStatusDetaching = "detaching"
bsVolumeStatusRetype = "retyping"
bsVolumeStatusShutdown = "deleting"
bsVolumeStatusDeleted = "deleted"
Expand Down Expand Up @@ -159,8 +160,8 @@ func resourceBlockStorageVolumeCreate(ctx context.Context, d *schema.ResourceDat

stateConf := &retry.StateChangeConf{
Pending: []string{bsVolumeStatusBuild, bsVolumeStatusDownloading},
Target: []string{bsVolumeStatusActive},
Refresh: blockStorageVolumeStateRefreshFunc(blockStorageClient, v.ID),
Target: []string{BSVolumeStatusActive},
Refresh: BlockStorageVolumeStateRefreshFunc(blockStorageClient, v.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: bsVolumeDelay,
MinTimeout: bsVolumeMinTimeout,
Expand Down Expand Up @@ -237,8 +238,8 @@ func resourceBlockStorageVolumeUpdate(ctx context.Context, d *schema.ResourceDat

stateConf := &retry.StateChangeConf{
Pending: []string{bsVolumeStatusBuild, bsVolumeStatusDownloading},
Target: []string{bsVolumeStatusActive, bsVolumeStatusInUse},
Refresh: blockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Target: []string{BSVolumeStatusActive, BSVolumeStatusInUse},
Refresh: BlockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: bsVolumeDelay,
MinTimeout: bsVolumeMinTimeout,
Expand Down Expand Up @@ -266,8 +267,8 @@ func resourceBlockStorageVolumeUpdate(ctx context.Context, d *schema.ResourceDat
}
stateConf := &retry.StateChangeConf{
Pending: []string{bsVolumeStatusBuild, bsVolumeStatusRetype},
Target: []string{bsVolumeStatusActive, bsVolumeStatusInUse},
Refresh: blockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Target: []string{BSVolumeStatusActive, BSVolumeStatusInUse},
Refresh: BlockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: bsVolumeDelay,
MinTimeout: bsVolumeMinTimeout,
Expand All @@ -294,9 +295,9 @@ func resourceBlockStorageVolumeDelete(ctx context.Context, d *schema.ResourceDat
}

stateConf := &retry.StateChangeConf{
Pending: []string{bsVolumeStatusActive, bsVolumeStatusShutdown, bsVolumeStatusInUse},
Pending: []string{BSVolumeStatusActive, bsVolumeStatusShutdown},
Target: []string{bsVolumeStatusDeleted},
Refresh: blockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Refresh: BlockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: bsVolumeDelay,
MinTimeout: bsVolumeMinTimeout,
Expand Down
2 changes: 1 addition & 1 deletion vkcs/blockstorage/resource_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func resourceBlockStorageSnapshotDelete(ctx context.Context, d *schema.ResourceD
stateConf := &retry.StateChangeConf{
Pending: []string{bsSnapshotStatusActive, bsSnapshotStatusShutdown},
Target: []string{bsSnapshotStatusDeleted},
Refresh: blockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Refresh: BlockStorageVolumeStateRefreshFunc(blockStorageClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: bsSnapshotDelay,
MinTimeout: bsSnapshotMinTimeout,
Expand Down
2 changes: 1 addition & 1 deletion vkcs/blockstorage/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (opts volumeChangeTypeOpts) ToVolumeChangeTypeMap() (map[string]interface{}
return util.BuildRequest(opts, "os-retype")
}

func blockStorageVolumeStateRefreshFunc(client *gophercloud.ServiceClient, volumeID string) retry.StateRefreshFunc {
func BlockStorageVolumeStateRefreshFunc(client *gophercloud.ServiceClient, volumeID string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
v, err := volumes.Get(client, volumeID).Extract()
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions vkcs/compute/resource_volume_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/blockstorage"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/clients"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/util"

Expand Down Expand Up @@ -174,5 +175,26 @@ func resourceComputeVolumeAttachDelete(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(util.CheckDeleted(d, err, "Error detaching vkcs_compute_volume_attach"))
}

// Volume may be still in detaching status after detach resource is deleted
blockStorageClient, err := config.BlockStorageV3Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS block storage client: %s", err)
}

volumeID := d.Get("volume_id").(string)

volumeStateConf := &retry.StateChangeConf{
Pending: []string{blockstorage.BSVolumeStatusDetaching, blockstorage.BSVolumeStatusInUse},
Target: []string{blockstorage.BSVolumeStatusActive},
Refresh: blockstorage.BlockStorageVolumeStateRefreshFunc(blockStorageClient, volumeID),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}

if _, err = volumeStateConf.WaitForStateContext(ctx); err != nil {
return diag.FromErr(util.CheckDeleted(d, err, "Error detaching vkcs_compute_volume_attach"))
}

return nil
}

0 comments on commit 433d10b

Please sign in to comment.