From 0de2ae96d715a5dff4b957d91c9b08ce1a308215 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 1 Sep 2023 09:24:54 +0000 Subject: [PATCH] Enable snapshot creation before volume deletion Closed #367 --- outscale/resource_outscale_volume.go | 52 +++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/outscale/resource_outscale_volume.go b/outscale/resource_outscale_volume.go index e4581e868..39e779382 100644 --- a/outscale/resource_outscale_volume.go +++ b/outscale/resource_outscale_volume.go @@ -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, @@ -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, } @@ -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(), @@ -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 }