Skip to content

Commit

Permalink
feat(instance): add support for tags in placement group (scaleway#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Mar 28, 2022
1 parent 0d31176 commit 0efc2b3
Show file tree
Hide file tree
Showing 4 changed files with 616 additions and 0 deletions.
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": {
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

0 comments on commit 0efc2b3

Please sign in to comment.