Skip to content

Commit c70f0b6

Browse files
authored
fix(instance): add support for handling empty security-groups tags (#1206)
1 parent 55dbcc5 commit c70f0b6

19 files changed

+6768
-3291
lines changed

scaleway/resource_instance_placement_group.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func resourceScalewayInstancePlacementGroupUpdate(ctx context.Context, d *schema
131131
req := &instance.UpdatePlacementGroupRequest{
132132
Zone: zone,
133133
PlacementGroupID: ID,
134+
Tags: scw.StringsPtr([]string{}),
134135
}
135136

136137
hasChanged := false
@@ -153,7 +154,9 @@ func resourceScalewayInstancePlacementGroupUpdate(ctx context.Context, d *schema
153154
}
154155

155156
if d.HasChange("tags") {
156-
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
157+
if len(expandStrings(d.Get("tags"))) > 0 {
158+
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
159+
}
157160
hasChanged = true
158161
}
159162

scaleway/resource_instance_placement_group_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestAccScalewayInstancePlacementGroup_Tags(t *testing.T) {
144144
`,
145145
Check: resource.ComposeTestCheckFunc(
146146
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
147-
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
147+
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.#", "0"),
148148
),
149149
},
150150
{
@@ -165,7 +165,7 @@ func TestAccScalewayInstancePlacementGroup_Tags(t *testing.T) {
165165
}
166166
`,
167167
Check: resource.ComposeTestCheckFunc(
168-
resource.TestCheckNoResourceAttr("scaleway_instance_placement_group.main", "tags"),
168+
resource.TestCheckResourceAttr("scaleway_instance_placement_group.main", "tags.#", "0"),
169169
testAccCheckScalewayInstancePlacementGroupExists(tt, "scaleway_instance_placement_group.main"),
170170
),
171171
},

scaleway/resource_instance_security_group.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ func resourceScalewayInstanceSecurityGroupCreate(ctx context.Context, d *schema.
119119
OutboundDefaultPolicy: instance.SecurityGroupPolicy(d.Get("outbound_default_policy").(string)),
120120
EnableDefaultSecurity: expandBoolPtr(d.Get("enable_default_security")),
121121
}
122-
//tags := expandStrings(d.Get("tags"))
123-
//if len(tags) > 0 {
124-
// req.Tags = tags
125-
//}
122+
tags := expandStrings(d.Get("tags"))
123+
if len(tags) > 0 {
124+
req.Tags = tags
125+
}
126126
res, err := instanceAPI.CreateSecurityGroup(req, scw.WithContext(ctx))
127127
if err != nil {
128128
return diag.FromErr(err)
@@ -267,7 +267,12 @@ func resourceScalewayInstanceSecurityGroupUpdate(ctx context.Context, d *schema.
267267
Description: expandStringPtr(description),
268268
InboundDefaultPolicy: &inboundDefaultPolicy,
269269
OutboundDefaultPolicy: &outboundDefaultPolicy,
270-
Tags: scw.StringsPtr(expandStrings(d.Get("tags"))),
270+
Tags: scw.StringsPtr([]string{}),
271+
}
272+
273+
tags := expandStrings(d.Get("tags"))
274+
if len(tags) > 0 {
275+
updateReq.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
271276
}
272277

273278
if d.HasChange("enable_default_security") {

scaleway/resource_instance_security_group_test.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func TestAccScalewayInstanceSecurityGroup_Basic(t *testing.T) {
3838
resource "scaleway_instance_security_group" "base" {
3939
name = "sg-name"
4040
inbound_default_policy = "drop"
41-
tags = [ "test-terraform" ]
4241
4342
inbound_rule {
4443
action = "accept"
@@ -90,7 +89,7 @@ func TestAccScalewayInstanceSecurityGroup_Basic(t *testing.T) {
9089
name = "sg-name"
9190
inbound_default_policy = "accept"
9291
tags = [ "test-terraform" ]
93-
92+
9493
inbound_rule {
9594
action = "drop"
9695
port = 80
@@ -113,6 +112,7 @@ func TestAccScalewayInstanceSecurityGroup_Basic(t *testing.T) {
113112
Check: resource.ComposeTestCheckFunc(
114113
testAccCheckScalewayInstanceSecurityGroupExists(tt, "scaleway_instance_security_group.base"),
115114
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "name", "sg-name"),
115+
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "tags.0", "test-terraform"),
116116
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "inbound_default_policy", "accept"),
117117
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "outbound_default_policy", "accept"),
118118
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "inbound_rule.#", "3"),
@@ -159,10 +159,10 @@ func TestAccScalewayInstanceSecurityGroup_Basic(t *testing.T) {
159159
resource "scaleway_instance_security_group" "base" {
160160
name = "sg-name"
161161
inbound_default_policy = "accept"
162-
tags = [ "test-terraform" ]
163162
}
164163
`,
165164
Check: resource.ComposeTestCheckFunc(
165+
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "tags.#", "0"),
166166
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "inbound_rule.#", "0"),
167167
),
168168
},
@@ -349,12 +349,13 @@ func TestAccScalewayInstanceSecurityGroup_RemovePort(t *testing.T) {
349349
Protocol: instance.SecurityGroupRuleProtocolTCP,
350350
Action: instance.SecurityGroupRuleActionAccept,
351351
}),
352+
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "tags.#", "1"),
353+
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "tags.0", "test-terraform"),
352354
),
353355
},
354356
{
355357
Config: `
356358
resource "scaleway_instance_security_group" "base" {
357-
tags = [ "test-terraform" ]
358359
inbound_rule {
359360
action = "accept"
360361
ip_range = "0.0.0.0/0"
@@ -370,6 +371,7 @@ func TestAccScalewayInstanceSecurityGroup_RemovePort(t *testing.T) {
370371
Protocol: instance.SecurityGroupRuleProtocolTCP,
371372
Action: instance.SecurityGroupRuleActionAccept,
372373
}),
374+
resource.TestCheckResourceAttr("scaleway_instance_security_group.base", "tags.#", "0"),
373375
),
374376
},
375377
},
@@ -463,6 +465,15 @@ func TestAccScalewayInstanceSecurityGroup_Tags(t *testing.T) {
463465
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.1", "buzz"),
464466
),
465467
},
468+
{
469+
Config: `
470+
resource "scaleway_instance_security_group" "main" {
471+
}
472+
`,
473+
Check: resource.ComposeTestCheckFunc(
474+
resource.TestCheckResourceAttr("scaleway_instance_security_group.main", "tags.#", "0"),
475+
),
476+
},
466477
},
467478
})
468479
}

scaleway/resource_instance_snapshot.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou
7979
Project: expandStringPtr(d.Get("project_id")),
8080
Name: expandOrGenerateString(d.Get("name"), "snap"),
8181
VolumeID: expandZonedID(d.Get("volume_id").(string)).ID,
82-
Tags: expandStrings(d.Get("tags")),
82+
}
83+
tags := expandStrings(d.Get("tags"))
84+
if len(tags) > 0 {
85+
req.Tags = tags
8386
}
8487

8588
res, err := instanceAPI.CreateSnapshot(req, scw.WithContext(ctx))
@@ -137,8 +140,13 @@ func resourceScalewayInstanceSnapshotUpdate(ctx context.Context, d *schema.Resou
137140
req := &instance.UpdateSnapshotRequest{
138141
SnapshotID: id,
139142
Zone: zone,
140-
Tags: scw.StringsPtr(expandStrings(d.Get("tags"))),
141143
Name: scw.StringPtr(d.Get("name").(string)),
144+
Tags: scw.StringsPtr([]string{}),
145+
}
146+
147+
tags := expandStrings(d.Get("tags"))
148+
if d.HasChange("tags") && len(tags) > 0 {
149+
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
142150
}
143151

144152
_, err = instanceAPI.UpdateSnapshot(req, scw.WithContext(ctx))

scaleway/resource_instance_snapshot_test.go

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package scaleway
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
710
)
811

912
func TestAccScalewayInstanceSnapshot_BlockVolume(t *testing.T) {
@@ -24,6 +27,9 @@ func TestAccScalewayInstanceSnapshot_BlockVolume(t *testing.T) {
2427
resource "scaleway_instance_snapshot" "main" {
2528
volume_id = scaleway_instance_volume.main.id
2629
}`,
30+
Check: resource.ComposeTestCheckFunc(
31+
testAccCheckScalewayInstanceSnapShotExists(tt, "scaleway_instance_snapshot.main"),
32+
),
2733
},
2834
},
2935
})
@@ -47,6 +53,9 @@ func TestAccScalewayInstanceSnapshot_Server(t *testing.T) {
4753
resource "scaleway_instance_snapshot" "main" {
4854
volume_id = scaleway_instance_server.main.root_volume.0.volume_id
4955
}`,
56+
Check: resource.ComposeTestCheckFunc(
57+
testAccCheckScalewayInstanceSnapShotExists(tt, "scaleway_instance_snapshot.main"),
58+
),
5059
},
5160
},
5261
})
@@ -79,6 +88,9 @@ func TestAccScalewayInstanceSnapshot_ServerWithBlockVolume(t *testing.T) {
7988
resource "scaleway_instance_snapshot" "main" {
8089
volume_id = scaleway_instance_volume.block.id
8190
}`,
91+
Check: resource.ComposeTestCheckFunc(
92+
testAccCheckScalewayInstanceSnapShotExists(tt, "scaleway_instance_snapshot.main"),
93+
),
8294
},
8395
},
8496
})
@@ -104,6 +116,10 @@ func TestAccScalewayInstanceSnapshot_RenameSnapshot(t *testing.T) {
104116
name = "first_name"
105117
tags = ["test-terraform"]
106118
}`,
119+
Check: resource.ComposeTestCheckFunc(
120+
testAccCheckScalewayInstanceSnapShotExists(tt, "scaleway_instance_snapshot.main"),
121+
resource.TestCheckResourceAttr("scaleway_instance_snapshot.main", "tags.0", "test-terraform"),
122+
),
107123
},
108124
{
109125
Config: `
@@ -115,9 +131,37 @@ func TestAccScalewayInstanceSnapshot_RenameSnapshot(t *testing.T) {
115131
resource "scaleway_instance_snapshot" "main" {
116132
volume_id = scaleway_instance_volume.main.id
117133
name = "second_name"
118-
tags = ["test-terraform"]
119134
}`,
135+
Check: resource.ComposeTestCheckFunc(
136+
testAccCheckScalewayInstanceSnapShotExists(tt, "scaleway_instance_snapshot.main"),
137+
resource.TestCheckResourceAttr("scaleway_instance_snapshot.main", "tags.#", "0"),
138+
),
120139
},
121140
},
122141
})
123142
}
143+
144+
func testAccCheckScalewayInstanceSnapShotExists(tt *TestTools, n string) resource.TestCheckFunc {
145+
return func(s *terraform.State) error {
146+
rs, ok := s.RootModule().Resources[n]
147+
if !ok {
148+
return fmt.Errorf("resource not found: %s", n)
149+
}
150+
151+
instanceAPI, zone, ID, err := instanceAPIWithZoneAndID(tt.Meta, rs.Primary.ID)
152+
if err != nil {
153+
return err
154+
}
155+
156+
_, err = instanceAPI.GetSnapshot(&instance.GetSnapshotRequest{
157+
Zone: zone,
158+
SnapshotID: ID,
159+
})
160+
161+
if err != nil {
162+
return err
163+
}
164+
165+
return nil
166+
}
167+
}

scaleway/resource_instance_volume.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func resourceScalewayInstanceVolumeCreate(ctx context.Context, d *schema.Resourc
9494
Name: expandOrGenerateString(d.Get("name"), "vol"),
9595
VolumeType: instance.VolumeVolumeType(d.Get("type").(string)),
9696
Project: expandStringPtr(d.Get("project_id")),
97-
Tags: expandStrings(d.Get("tags")),
97+
}
98+
tags := expandStrings(d.Get("tags"))
99+
if len(tags) > 0 {
100+
createVolumeRequest.Tags = tags
98101
}
99102

100103
if size, ok := d.GetOk("size_in_gb"); ok {
@@ -179,14 +182,16 @@ func resourceScalewayInstanceVolumeUpdate(ctx context.Context, d *schema.Resourc
179182
req := &instance.UpdateVolumeRequest{
180183
VolumeID: id,
181184
Zone: zone,
185+
Tags: scw.StringsPtr([]string{}),
182186
}
183187

184188
if d.HasChange("name") {
185189
newName := d.Get("name").(string)
186190
req.Name = &newName
187191
}
188192

189-
if d.HasChange("tags") {
193+
tags := expandStrings(d.Get("tags"))
194+
if d.HasChange("tags") && len(tags) > 0 {
190195
req.Tags = scw.StringsPtr(expandStrings(d.Get("tags")))
191196
}
192197

scaleway/resource_instance_volume_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ func TestAccScalewayInstanceVolume_Basic(t *testing.T) {
5858
resource "scaleway_instance_volume" "test" {
5959
type = "l_ssd"
6060
size_in_gb = 20
61+
tags = ["test-terraform"]
6162
}
6263
`,
6364
Check: resource.ComposeTestCheckFunc(
6465
testAccCheckScalewayInstanceVolumeExists(tt, "scaleway_instance_volume.test"),
6566
resource.TestCheckResourceAttr("scaleway_instance_volume.test", "size_in_gb", "20"),
67+
resource.TestCheckResourceAttr("scaleway_instance_volume.test", "tags.0", "test-terraform"),
6668
),
6769
},
6870
{
@@ -77,6 +79,7 @@ func TestAccScalewayInstanceVolume_Basic(t *testing.T) {
7779
testAccCheckScalewayInstanceVolumeExists(tt, "scaleway_instance_volume.test"),
7880
resource.TestCheckResourceAttr("scaleway_instance_volume.test", "name", "terraform-test"),
7981
resource.TestCheckResourceAttr("scaleway_instance_volume.test", "size_in_gb", "20"),
82+
resource.TestCheckResourceAttr("scaleway_instance_volume.test", "tags.#", "0"),
8083
),
8184
},
8285
},

0 commit comments

Comments
 (0)