Skip to content

Commit 0efc2b3

Browse files
committed
feat(instance): add support for tags in placement group (scaleway#1132)
1 parent 0d31176 commit 0efc2b3

4 files changed

+616
-0
lines changed

docs/resources/instance_placement_group.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The following arguments are supported:
2323
- `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`.
2424
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the placement group should be created.
2525
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the placement group is associated with.
26+
- `tags` - (Optional) A list of tags to apply to the placement group.
2627

2728
## Attributes Reference
2829

scaleway/resource_instance_placement_group.go

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func resourceScalewayInstancePlacementGroup() *schema.Resource {
5555
Computed: true,
5656
Description: "Is true when the policy is respected.",
5757
},
58+
"tags": {
59+
Type: schema.TypeList,
60+
Elem: &schema.Schema{
61+
Type: schema.TypeString,
62+
},
63+
Optional: true,
64+
Description: "The tags associated with the placement group",
65+
},
5866
"zone": zoneSchema(),
5967
"organization_id": organizationIDSchema(),
6068
"project_id": projectIDSchema(),
@@ -74,6 +82,7 @@ func resourceScalewayInstancePlacementGroupCreate(ctx context.Context, d *schema
7482
Project: expandStringPtr(d.Get("project_id")),
7583
PolicyMode: instance.PlacementGroupPolicyMode(d.Get("policy_mode").(string)),
7684
PolicyType: instance.PlacementGroupPolicyType(d.Get("policy_type").(string)),
85+
Tags: expandStrings(d.Get("tags")),
7786
}, scw.WithContext(ctx))
7887
if err != nil {
7988
return diag.FromErr(err)
@@ -109,6 +118,7 @@ func resourceScalewayInstancePlacementGroupRead(ctx context.Context, d *schema.R
109118
_ = d.Set("policy_mode", res.PlacementGroup.PolicyMode.String())
110119
_ = d.Set("policy_type", res.PlacementGroup.PolicyType.String())
111120
_ = d.Set("policy_respected", res.PlacementGroup.PolicyRespected)
121+
_ = d.Set("tags", res.PlacementGroup.Tags)
112122

113123
return nil
114124
}
@@ -142,6 +152,11 @@ func resourceScalewayInstancePlacementGroupUpdate(ctx context.Context, d *schema
142152
hasChanged = true
143153
}
144154

155+
if d.HasChange("tags") {
156+
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
157+
hasChanged = true
158+
}
159+
145160
if hasChanged {
146161
_, err = instanceAPI.UpdatePlacementGroup(req, scw.WithContext(ctx))
147162
if err != nil {

scaleway/resource_instance_placement_group_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,48 @@ func TestAccScalewayInstancePlacementGroup_Rename(t *testing.T) {
131131
})
132132
}
133133

134+
func TestAccScalewayInstancePlacementGroup_Tags(t *testing.T) {
135+
tt := NewTestTools(t)
136+
defer tt.Cleanup()
137+
resource.ParallelTest(t, resource.TestCase{
138+
ProviderFactories: tt.ProviderFactories,
139+
CheckDestroy: testAccCheckScalewayInstanceIPDestroy(tt),
140+
Steps: []resource.TestStep{
141+
{
142+
Config: `
143+
resource "scaleway_instance_placement_group" "main" {}
144+
`,
145+
Check: resource.ComposeTestCheckFunc(
146+
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
147+
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
148+
),
149+
},
150+
{
151+
Config: `
152+
resource "scaleway_instance_placement_group" "main" {
153+
tags = ["foo", "bar"]
154+
}
155+
`,
156+
Check: resource.ComposeTestCheckFunc(
157+
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
158+
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.0", "foo"),
159+
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.1", "bar"),
160+
),
161+
},
162+
{
163+
Config: `
164+
resource "scaleway_instance_placement_group" "main" {
165+
}
166+
`,
167+
Check: resource.ComposeTestCheckFunc(
168+
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
169+
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
170+
),
171+
},
172+
},
173+
})
174+
}
175+
134176
func testAccCheckScalewayInstancePlacementGroupExists(tt *TestTools, n string) resource.TestCheckFunc {
135177
return func(s *terraform.State) error {
136178
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)