Skip to content

Commit 0bd7069

Browse files
authored
feat(instance_snapshot): add snapshot import (#1489)
1 parent 0a4a425 commit 0bd7069

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

docs/resources/instance_snapshot.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,33 @@ resource "scaleway_instance_snapshot" "main" {
4646
}
4747
```
4848

49+
## Import a local qcow2 file
50+
51+
```hcl
52+
resource "scaleway_object_bucket" "bucket" {
53+
name = "snapshot-qcow-import"
54+
}
55+
56+
resource "scaleway_object" "qcow" {
57+
bucket = scaleway_object_bucket.bucket.name
58+
key = "server.qcow2"
59+
file = "myqcow.qcow2"
60+
}
61+
62+
resource "scaleway_instance_snapshot" "snapshot" {
63+
type = "unified"
64+
import {
65+
bucket = scaleway_object.qcow.bucket
66+
key = scaleway_object.qcow.key
67+
}
68+
}
69+
```
70+
4971
## Arguments Reference
5072

5173
The following arguments are supported:
5274

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

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

scaleway/resource_instance_snapshot.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ func resourceScalewayInstanceSnapshot() *schema.Resource {
3535
Description: "The name of the snapshot",
3636
},
3737
"volume_id": {
38-
Type: schema.TypeString,
39-
Required: true,
40-
ForceNew: true,
41-
Description: "ID of the volume to take a snapshot from",
42-
ValidateFunc: validationUUIDorUUIDWithLocality(),
38+
Type: schema.TypeString,
39+
Optional: true,
40+
ForceNew: true,
41+
Description: "ID of the volume to take a snapshot from",
42+
ValidateFunc: validationUUIDorUUIDWithLocality(),
43+
ConflictsWith: []string{"import"},
4344
},
4445
"type": {
4546
Type: schema.TypeString,
@@ -67,6 +68,28 @@ func resourceScalewayInstanceSnapshot() *schema.Resource {
6768
Optional: true,
6869
Description: "The tags associated with the snapshot",
6970
},
71+
"import": {
72+
Type: schema.TypeList,
73+
ForceNew: true,
74+
MaxItems: 1,
75+
Elem: &schema.Resource{
76+
Schema: map[string]*schema.Schema{
77+
"bucket": {
78+
Type: schema.TypeString,
79+
Required: true,
80+
Description: "Bucket containing qcow",
81+
},
82+
"key": {
83+
Type: schema.TypeString,
84+
Required: true,
85+
Description: "Key of the qcow file in the specified bucket",
86+
},
87+
},
88+
},
89+
Optional: true,
90+
Description: "Import snapshot from a qcow",
91+
ConflictsWith: []string{"volume_id"},
92+
},
7093
"created_at": {
7194
Type: schema.TypeString,
7295
Computed: true,
@@ -86,10 +109,9 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou
86109
}
87110

88111
req := &instance.CreateSnapshotRequest{
89-
Zone: zone,
90-
Project: expandStringPtr(d.Get("project_id")),
91-
Name: expandOrGenerateString(d.Get("name"), "snap"),
92-
VolumeID: expandZonedID(d.Get("volume_id").(string)).ID,
112+
Zone: zone,
113+
Project: expandStringPtr(d.Get("project_id")),
114+
Name: expandOrGenerateString(d.Get("name"), "snap"),
93115
}
94116

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

126+
if volumeID, volumeIDExist := d.GetOk("volume_id"); volumeIDExist {
127+
req.VolumeID = expandZonedID(volumeID).ID
128+
}
129+
130+
if _, isImported := d.GetOk("import"); isImported {
131+
req.Bucket = expandStringPtr(d.Get("import.0.bucket"))
132+
req.Key = expandStringPtr(d.Get("import.0.key"))
133+
}
134+
104135
res, err := instanceAPI.CreateSnapshot(req, scw.WithContext(ctx))
105136
if err != nil {
106137
return diag.FromErr(err)

0 commit comments

Comments
 (0)