Skip to content

Commit

Permalink
feat(instance): add support for tags in volume (scaleway#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Mar 28, 2022
1 parent 47c8148 commit d1efe0c
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 148 deletions.
1 change: 1 addition & 0 deletions docs/resources/instance_volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The following arguments are supported:
- `name` - (Optional) The name of the volume. 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 volume should be created.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the volume is associated with.
- `tags` - (Optional) A list of tags to apply to the volume.

## Attributes Reference

Expand Down
32 changes: 24 additions & 8 deletions scaleway/resource_instance_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func resourceScalewayInstanceVolume() *schema.Resource {
Computed: true,
Description: "The server associated with this volume",
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The tags associated with the volume",
},
"organization_id": organizationIDSchema(),
"project_id": projectIDSchema(),
"zone": zoneSchema(),
Expand All @@ -86,6 +94,7 @@ func resourceScalewayInstanceVolumeCreate(ctx context.Context, d *schema.Resourc
Name: expandOrGenerateString(d.Get("name"), "vol"),
VolumeType: instance.VolumeVolumeType(d.Get("type").(string)),
Project: expandStringPtr(d.Get("project_id")),
Tags: expandStrings(d.Get("tags")),
}

if size, ok := d.GetOk("size_in_gb"); ok {
Expand Down Expand Up @@ -134,6 +143,7 @@ func resourceScalewayInstanceVolumeRead(ctx context.Context, d *schema.ResourceD
_ = d.Set("project_id", res.Volume.Project)
_ = d.Set("zone", string(zone))
_ = d.Set("type", res.Volume.VolumeType.String())
_ = d.Set("tags", res.Volume.Tags)

_, fromVolume := d.GetOk("from_volume_id")
_, fromSnapshot := d.GetOk("from_snapshot_id")
Expand All @@ -156,17 +166,18 @@ func resourceScalewayInstanceVolumeUpdate(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

req := &instance.UpdateVolumeRequest{
VolumeID: id,
Zone: zone,
}

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

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

if d.HasChange("size_in_gb") {
Expand Down Expand Up @@ -204,6 +215,11 @@ func resourceScalewayInstanceVolumeUpdate(ctx context.Context, d *schema.Resourc
}
}

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

return resourceScalewayInstanceVolumeRead(ctx, d, meta)
}

Expand Down

0 comments on commit d1efe0c

Please sign in to comment.