Skip to content

Commit

Permalink
feat(instance_snapshot): add snapshot import (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Oct 4, 2022
1 parent 0a4a425 commit 0bd7069
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
27 changes: 26 additions & 1 deletion docs/resources/instance_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,33 @@ resource "scaleway_instance_snapshot" "main" {
}
```

## Import a local qcow2 file

```hcl
resource "scaleway_object_bucket" "bucket" {
name = "snapshot-qcow-import"
}
resource "scaleway_object" "qcow" {
bucket = scaleway_object_bucket.bucket.name
key = "server.qcow2"
file = "myqcow.qcow2"
}
resource "scaleway_instance_snapshot" "snapshot" {
type = "unified"
import {
bucket = scaleway_object.qcow.bucket
key = scaleway_object.qcow.key
}
}
```

## Arguments Reference

The following arguments are supported:

- `volume_id` - (Required) The ID of the volume to take a snapshot from.
- `volume_id` - (Optional) The ID of the volume to take a snapshot from.
- `type` - (Optional) The snapshot's volume type. The possible values are: `b_ssd` (Block SSD), `l_ssd` (Local SSD) and `unified`.
Updates to this field will recreate a new resource.
- `name` - (Optional) The name of the snapshot. If not provided it will be randomly generated.
Expand All @@ -59,6 +81,9 @@ Updates to this field will recreate a new resource.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the snapshot is
associated with.
- `tags` - (Optional) A list of tags to apply to the snapshot.
- `import` - (Optional) Import a snapshot from a qcow2 file located in a bucket
- `bucket` - Bucket name containing [qcow2](https://en.wikipedia.org/wiki/Qcow) to import
- `key` - Key of the object to import

-> **Note:** The type `unified` could be instantiated on both `l_ssd` and `b_ssd` volumes.

Expand Down
49 changes: 40 additions & 9 deletions scaleway/resource_instance_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func resourceScalewayInstanceSnapshot() *schema.Resource {
Description: "The name of the snapshot",
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the volume to take a snapshot from",
ValidateFunc: validationUUIDorUUIDWithLocality(),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "ID of the volume to take a snapshot from",
ValidateFunc: validationUUIDorUUIDWithLocality(),
ConflictsWith: []string{"import"},
},
"type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -67,6 +68,28 @@ func resourceScalewayInstanceSnapshot() *schema.Resource {
Optional: true,
Description: "The tags associated with the snapshot",
},
"import": {
Type: schema.TypeList,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
Description: "Bucket containing qcow",
},
"key": {
Type: schema.TypeString,
Required: true,
Description: "Key of the qcow file in the specified bucket",
},
},
},
Optional: true,
Description: "Import snapshot from a qcow",
ConflictsWith: []string{"volume_id"},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -86,10 +109,9 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou
}

req := &instance.CreateSnapshotRequest{
Zone: zone,
Project: expandStringPtr(d.Get("project_id")),
Name: expandOrGenerateString(d.Get("name"), "snap"),
VolumeID: expandZonedID(d.Get("volume_id").(string)).ID,
Zone: zone,
Project: expandStringPtr(d.Get("project_id")),
Name: expandOrGenerateString(d.Get("name"), "snap"),
}

if volumeType, ok := d.GetOk("type"); ok {
Expand All @@ -101,6 +123,15 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou
req.Tags = tags
}

if volumeID, volumeIDExist := d.GetOk("volume_id"); volumeIDExist {
req.VolumeID = expandZonedID(volumeID).ID
}

if _, isImported := d.GetOk("import"); isImported {
req.Bucket = expandStringPtr(d.Get("import.0.bucket"))
req.Key = expandStringPtr(d.Get("import.0.key"))
}

res, err := instanceAPI.CreateSnapshot(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down

0 comments on commit 0bd7069

Please sign in to comment.