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 placement group #1132

Merged
merged 3 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_placement_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following arguments are supported:
- `policy_mode` - (Defaults to `optional`) The [policy mode](https://developers.scaleway.com/en/products/instance/api/#placement-groups-d8f653) of the placement group. Possible values are: `optional` or `enforced`.
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the placement group should be created.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the placement group is associated with.
- `tags` - (Optional) A list of tags to apply to the placement group.

## Attributes Reference

Expand Down
15 changes: 15 additions & 0 deletions scaleway/resource_instance_placement_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func resourceScalewayInstancePlacementGroup() *schema.Resource {
Computed: true,
Description: "Is true when the policy is respected.",
},
"tags": {
remyleone marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The tags associated with the placement group",
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),
"project_id": projectIDSchema(),
Expand All @@ -74,6 +82,7 @@ func resourceScalewayInstancePlacementGroupCreate(ctx context.Context, d *schema
Project: expandStringPtr(d.Get("project_id")),
PolicyMode: instance.PlacementGroupPolicyMode(d.Get("policy_mode").(string)),
PolicyType: instance.PlacementGroupPolicyType(d.Get("policy_type").(string)),
Tags: expandStrings(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -109,6 +118,7 @@ func resourceScalewayInstancePlacementGroupRead(ctx context.Context, d *schema.R
_ = d.Set("policy_mode", res.PlacementGroup.PolicyMode.String())
_ = d.Set("policy_type", res.PlacementGroup.PolicyType.String())
_ = d.Set("policy_respected", res.PlacementGroup.PolicyRespected)
_ = d.Set("tags", res.PlacementGroup.Tags)

return nil
}
Expand Down Expand Up @@ -142,6 +152,11 @@ func resourceScalewayInstancePlacementGroupUpdate(ctx context.Context, d *schema
hasChanged = true
}

if d.HasChange("tags") {
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
hasChanged = true
}

if hasChanged {
_, err = instanceAPI.UpdatePlacementGroup(req, scw.WithContext(ctx))
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions scaleway/resource_instance_placement_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,48 @@ func TestAccScalewayInstancePlacementGroup_Rename(t *testing.T) {
})
}

func TestAccScalewayInstancePlacementGroup_Tags(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayInstanceIPDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_placement_group" "main" {}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
),
},
{
Config: `
resource "scaleway_instance_placement_group" "main" {
tags = ["foo", "bar"]
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.0", "foo"),
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.1", "bar"),
),
},
{
Config: `
resource "scaleway_instance_placement_group" "main" {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
),
},
},
})
}

func testAccCheckScalewayInstancePlacementGroupExists(tt *TestTools, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down