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

feat(instance): add support for tags in volume #1129

Merged
merged 2 commits into from
Mar 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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