Skip to content

Commit

Permalink
feat(instance): add support for tags in snapshot (scaleway#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Mar 28, 2022
1 parent 2d1a8c6 commit 22e4f80
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/resources/instance_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following arguments are supported:
- `name` - (Optional) The name of the snapshot. If not provided it will be randomly generated.
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the snapshot should be created.
- `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.

## Attributes Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220308141350-165c40ac312e
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025 h1:k79FedA5EvuHwgTeNDhDSIJE/JrSY1eotHg0K7lcGjg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220304124138-3f3535da1025/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220308141350-165c40ac312e h1:clWNRWjqkdaqZ2zUCKnzS5fPypivPeuZyKpy0fNe9zU=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20220308141350-165c40ac312e/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
32 changes: 24 additions & 8 deletions scaleway/resource_instance_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func resourceScalewayInstanceSnapshot() *schema.Resource {
Computed: true,
Description: "The size of the snapshot in gigabyte",
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The tags associated with the snapshot",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -71,6 +79,7 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou
Project: expandStringPtr(d.Get("project_id")),
Name: expandOrGenerateString(d.Get("name"), "snap"),
VolumeID: expandZonedID(d.Get("volume_id").(string)).ID,
Tags: expandStrings(d.Get("tags")),
}

res, err := instanceAPI.CreateSnapshot(req, scw.WithContext(ctx))
Expand Down Expand Up @@ -103,6 +112,7 @@ func resourceScalewayInstanceSnapshotRead(ctx context.Context, d *schema.Resourc
_ = d.Set("name", snapshot.Snapshot.Name)
_ = d.Set("created_at", snapshot.Snapshot.CreationDate.Format(time.RFC3339))
_ = d.Set("type", snapshot.Snapshot.VolumeType.String())
_ = d.Set("tags", snapshot.Snapshot.Tags)

return nil
}
Expand All @@ -113,17 +123,23 @@ func resourceScalewayInstanceSnapshotUpdate(ctx context.Context, d *schema.Resou
return diag.FromErr(err)
}

req := &instance.UpdateSnapshotRequest{
SnapshotID: id,
Zone: zone,
}

if d.HasChange("name") {
newName := d.Get("name").(string)
req.Name = &newName
}

_, err = instanceAPI.UpdateSnapshot(&instance.UpdateSnapshotRequest{
SnapshotID: id,
Zone: zone,
Name: &newName,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(fmt.Errorf("couldn't update snapshot: %s", err))
}
if d.HasChange("tags") {
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
}

_, err = instanceAPI.UpdateSnapshot(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(fmt.Errorf("couldn't update snapshot: %s", err))
}

return resourceScalewayInstanceSnapshotRead(ctx, d, meta)
Expand Down

0 comments on commit 22e4f80

Please sign in to comment.