Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume deletion snapshot #382

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,5 @@ func getIops(volumeType string, iops int32) int32 {
if volumeType != "standard" {
return iops
}
return defaultIops
return utils.DefaultIops
}
4 changes: 2 additions & 2 deletions outscale/resource_outscale_nic_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func resourceOutscaleOAPINetworkInterfaceAttachmentRead(d *schema.ResourceData,
resp, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for Volume to attach to Instance: %s, error: %s", nicID, err)
"Error waiting for NIC to attach to Instance: %s, error: %s", nicID, err)
}

r := resp.(oscgo.ReadNicsResponse)
Expand Down Expand Up @@ -206,7 +206,7 @@ func resourceOutscaleNetworkInterfaceAttachmentImportState(d *schema.ResourceDat
resp, err := stateConf.WaitForState()
if err != nil {
return nil, fmt.Errorf(
"Error waiting for Volume to attach to Instance: %s, error: %s", d.Id(), err)
"Error waiting for NIC to attach to Instance: %s, error: %s", d.Id(), err)
}
r := resp.(oscgo.ReadNicsResponse)
linkNic := r.GetNics()[0].GetLinkNic()
Expand Down
54 changes: 51 additions & 3 deletions outscale/resource_outscale_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const defaultIops = 150

func resourceOutscaleOAPIVolume() *schema.Resource {
return &schema.Resource{
Create: resourceOAPIVolumeCreate,
Expand Down Expand Up @@ -69,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 @@ -236,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 @@ -251,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 @@ -319,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
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
{
"schema_version": 0,
"attributes": {
"access_key_id": "##id-1##",
"access_key_id": "##id-0##",
"creation_date": "########",
"expiration_date": "",
"id": "##id-1##",
"id": "##id-0##",
"last_modification_date": "########",
"request_id": "########",
"secret_key": "########",
Expand All @@ -72,10 +72,10 @@
{
"schema_version": 0,
"attributes": {
"access_key_id": "##id-0##",
"access_key_id": "##id-1##",
"creation_date": "########",
"expiration_date": "",
"id": "##id-0##",
"id": "##id-1##",
"last_modification_date": "########",
"request_id": "########",
"secret_key": "########",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-3##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-3##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-3##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"value": "test-Volume"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-0##",
"volume_type": "io1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"value": "volume-standard"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-0##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"value": "test1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -96,6 +97,7 @@
"state": "available",
"subregion_name": "eu-west-2a",
"tags": [],
"termination_snapshot_name": null,
"volume_id": "##id-3##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"value": "volume-io1-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-1##",
"volume_type": "io1"
},
Expand Down Expand Up @@ -123,6 +124,7 @@
"value": "volume-io1-2"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "io1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"value": "volume-standard-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -115,6 +116,7 @@
"value": "volume-io1-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-1##",
"volume_type": "io1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"value": "volume-standard-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-1##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -115,6 +116,7 @@
"value": "volume-standard-2"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"value": "volume-standard-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-1##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -241,6 +242,7 @@
"value": "volume-standard-2"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -273,6 +275,7 @@
"value": "io1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-4##",
"volume_type": "io1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
"value": "volume-standard-1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-1##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -250,6 +251,7 @@
"value": "volume-standard-2"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-2##",
"volume_type": "standard"
},
Expand Down Expand Up @@ -290,6 +292,7 @@
"value": "io1"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-4##",
"volume_type": "io1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"value": "standard"
}
],
"termination_snapshot_name": null,
"volume_id": "##id-6##",
"volume_type": "standard"
},
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
MaxPort int = 65535
MinIops int = 100
MaxIops int = 13000
DefaultIops int = 150
DefaultIops int32 = 150
MaxSize int = 14901
InvalidState string = "InvalidState"
VolumeIOPSError string = `
Expand Down
Loading