Skip to content

Commit

Permalink
Enable snapshot creation before volume deletion
Browse files Browse the repository at this point in the history
Closed #367
  • Loading branch information
outscale-toa committed Oct 4, 2023
1 parent ab4054b commit 0de2ae9
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion outscale/resource_outscale_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func resourceOutscaleOAPIVolume() *schema.Resource {
ForceNew: true,
Computed: true,
},
"termination_snapshot_name": {
Type: schema.TypeString,
Optional: true,
},
"volume_type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -234,7 +238,7 @@ func resourceOAPIVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
Target: []string{"available", "in-use"},
Refresh: volumeOAPIStateRefreshFunc(conn, d.Id()),
Timeout: 5 * time.Minute,
Delay: 5 * time.Second,
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}

Expand All @@ -249,6 +253,47 @@ func resourceOAPIVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceOAPIVolumeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

if snpName, ok := d.GetOk("termination_snapshot_name"); ok {
volId := d.Get("volume_id").(string)
description := "Created before volume deletion"
resp := oscgo.CreateSnapshotResponse{}
request := oscgo.CreateSnapshotRequest{
Description: &description,
VolumeId: &volId,
}
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
var err error
r, httpResp, err := conn.SnapshotApi.CreateSnapshot(context.Background()).CreateSnapshotRequest(request).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = r
return nil
})
if err != nil {
return err
}
snapTagsReq := oscgo.CreateTagsRequest{
ResourceIds: []string{resp.Snapshot.GetSnapshotId()},
Tags: []oscgo.ResourceTag{
{
Key: "Name",
Value: snpName.(string),
},
},
}
err = resource.Retry(60*time.Second, func() *resource.RetryError {
_, httpResp, err := conn.TagApi.CreateTags(context.Background()).CreateTagsRequest(snapTagsReq).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
return nil
})
if err != nil {
return err
}
}

return resource.Retry(5*time.Minute, func() *resource.RetryError {
request := oscgo.DeleteVolumeRequest{
VolumeId: d.Id(),
Expand Down Expand Up @@ -317,6 +362,11 @@ func readOAPIVolume(d *schema.ResourceData, volume oscgo.Volume) error {
if err := d.Set("creation_date", volume.GetCreationDate()); err != nil {
return err
}
if snapName, ok := d.GetOk("termination_snapshot_name"); ok {
if err := d.Set("termination_snapshot_name", snapName.(string)); err != nil {
return err
}
}
if err := d.Set("iops", getIops(volume.GetVolumeType(), volume.GetIops())); err != nil {
return err
}
Expand Down

0 comments on commit 0de2ae9

Please sign in to comment.