Skip to content

Commit 06b610d

Browse files
authored
fix: use block api for setting tags (#231)
* fix: use block api for setting tags * lint * lint
1 parent 9da6243 commit 06b610d

14 files changed

+1264
-894
lines changed

builder/scaleway/step_backup.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77

88
"github.com/hashicorp/packer-plugin-sdk/multistep"
99
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
10+
"github.com/scaleway/scaleway-sdk-go/api/block/v1"
1011
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1112
"github.com/scaleway/scaleway-sdk-go/scw"
1213
)
1314

1415
type stepBackup struct{}
1516

1617
func (s *stepBackup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
17-
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
18+
client := state.Get("client").(*scw.Client)
19+
instanceAPI := instance.NewAPI(client)
20+
blockAPI := block.NewAPI(client)
1821
ui := state.Get("ui").(packersdk.Ui)
1922
c := state.Get("config").(*Config)
2023
server := state.Get("server").(*instance.Server)
@@ -61,7 +64,7 @@ func (s *stepBackup) Run(ctx context.Context, state multistep.StateBag) multiste
6164

6265
// Apply tags to image, volumes and snapshots
6366
if len(c.Tags) != 0 {
64-
err = applyTags(ctx, instanceAPI, scw.Zone(c.Zone), imageID, server.Volumes, snapshots, c.Tags)
67+
err = applyTags(ctx, instanceAPI, blockAPI, scw.Zone(c.Zone), imageID, server.Volumes, snapshots, c.Tags)
6568
if err != nil {
6669
state.Put("error", err)
6770
ui.Error(err.Error())
@@ -122,7 +125,7 @@ func imageIDFromBackupResult(hrefResult string) (string, error) {
122125
return imageID, nil
123126
}
124127

125-
func applyTags(ctx context.Context, instanceAPI *instance.API, zone scw.Zone, imageID string, volumes map[string]*instance.VolumeServer, snapshots []ArtifactSnapshot, tags []string) error {
128+
func applyTags(ctx context.Context, instanceAPI *instance.API, blockAPI *block.API, zone scw.Zone, imageID string, volumes map[string]*instance.VolumeServer, snapshots []ArtifactSnapshot, tags []string) error {
126129
if _, err := instanceAPI.UpdateImage(&instance.UpdateImageRequest{
127130
ImageID: imageID,
128131
Zone: zone,
@@ -132,7 +135,7 @@ func applyTags(ctx context.Context, instanceAPI *instance.API, zone scw.Zone, im
132135
}
133136

134137
for _, volume := range volumes {
135-
if _, err := instanceAPI.UpdateVolume(&instance.UpdateVolumeRequest{
138+
if _, err := blockAPI.UpdateVolume(&block.UpdateVolumeRequest{
136139
VolumeID: volume.ID,
137140
Zone: zone,
138141
Tags: &tags,
@@ -142,7 +145,7 @@ func applyTags(ctx context.Context, instanceAPI *instance.API, zone scw.Zone, im
142145
}
143146

144147
for _, snapshot := range snapshots {
145-
if _, err := instanceAPI.UpdateSnapshot(&instance.UpdateSnapshotRequest{
148+
if _, err := blockAPI.UpdateSnapshot(&block.UpdateSnapshotRequest{
146149
SnapshotID: snapshot.ID,
147150
Zone: zone,
148151
Tags: &tags,

e2e_tests/tmp.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/checks/image.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Image(zone scw.Zone, name string) *ImageCheck {
2121
type ImageCheck struct {
2222
zone scw.Zone
2323
imageName string
24+
tags []string
2425

2526
rootVolumeType *string
2627
rootVolumeSnapshot *BlockSnapshotCheck
@@ -56,6 +57,12 @@ func (c *ImageCheck) SizeInGb(size uint64) *ImageCheck {
5657
return c
5758
}
5859

60+
func (c *ImageCheck) Tags(tags []string) *ImageCheck {
61+
c.tags = tags
62+
63+
return c
64+
}
65+
5966
func (c *ImageCheck) Check(ctx context.Context) error {
6067
testCtx := tester.ExtractCtx(ctx)
6168
api := instance.NewAPI(testCtx.ScwClient)
@@ -101,6 +108,7 @@ func (c *ImageCheck) Check(ctx context.Context) error {
101108
if c.extraVolumesType != nil {
102109
for k, v := range c.extraVolumesType {
103110
vol, exists := image.ExtraVolumes[k]
111+
104112
if !exists {
105113
return fmt.Errorf("extra volume %s does not exist", k)
106114
}
@@ -111,5 +119,23 @@ func (c *ImageCheck) Check(ctx context.Context) error {
111119
}
112120
}
113121

122+
if c.tags != nil {
123+
for _, expectedTag := range c.tags {
124+
found := false
125+
126+
for _, actualTag := range image.Tags {
127+
if actualTag == expectedTag {
128+
found = true
129+
130+
break
131+
}
132+
}
133+
134+
if !found {
135+
return fmt.Errorf("expected tag %q not found on image %s", expectedTag, c.imageName)
136+
}
137+
}
138+
}
139+
114140
return nil
115141
}

internal/checks/snapshot.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type SnapshotCheck struct {
1313
zone scw.Zone
1414
snapshotName string
15+
tags []string
1516

1617
size *scw.Size
1718
}
@@ -22,6 +23,12 @@ func (c *SnapshotCheck) SizeInGB(size uint64) *SnapshotCheck {
2223
return c
2324
}
2425

26+
func (c *SnapshotCheck) Tags(tags []string) *SnapshotCheck {
27+
c.tags = tags
28+
29+
return c
30+
}
31+
2532
func (c *SnapshotCheck) Check(ctx context.Context) error {
2633
testCtx := tester.ExtractCtx(ctx)
2734
api := instance.NewAPI(testCtx.ScwClient)
@@ -53,6 +60,24 @@ func (c *SnapshotCheck) Check(ctx context.Context) error {
5360
return fmt.Errorf("snapshot size %d does not match expected size %d", snapshot.Size, *c.size)
5461
}
5562

63+
if len(c.tags) > 0 {
64+
for _, expectedTag := range c.tags {
65+
found := false
66+
67+
for _, actualTag := range snapshot.Tags {
68+
if actualTag == expectedTag {
69+
found = true
70+
71+
break
72+
}
73+
}
74+
75+
if !found {
76+
return fmt.Errorf("expected tag %q not found on snapshot %s", expectedTag, c.snapshotName)
77+
}
78+
}
79+
}
80+
5681
return nil
5782
}
5883

internal/checks/volume.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func NoVolume(zone scw.Zone) *NoVolumesCheck {
5858
type VolumeCheck struct {
5959
zone scw.Zone
6060
volumeName string
61+
tags []string
6162

6263
size *scw.Size
6364
}
@@ -68,6 +69,12 @@ func (c *VolumeCheck) SizeInGB(size uint64) *VolumeCheck {
6869
return c
6970
}
7071

72+
func (c *VolumeCheck) Tags(tags []string) *VolumeCheck {
73+
c.tags = tags
74+
75+
return c
76+
}
77+
7178
func (c *VolumeCheck) Check(ctx context.Context) error {
7279
testCtx := tester.ExtractCtx(ctx)
7380
api := instance.NewAPI(testCtx.ScwClient)
@@ -99,6 +106,24 @@ func (c *VolumeCheck) Check(ctx context.Context) error {
99106
return fmt.Errorf("volume size %d does not match expected size %d", volume.Size, *c.size)
100107
}
101108

109+
if len(c.tags) > 0 {
110+
for _, expectedTag := range c.tags {
111+
found := false
112+
113+
for _, actualTag := range volume.Tags {
114+
if actualTag == expectedTag {
115+
found = true
116+
117+
break
118+
}
119+
}
120+
121+
if !found {
122+
return fmt.Errorf("expected tag %q not found on volume %s", expectedTag, c.volumeName)
123+
}
124+
}
125+
}
126+
102127
return nil
103128
}
104129

internal/tests/simple_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ source "scaleway" "basic" {
2121
image_name = "packer-e2e-simple"
2222
ssh_username = "root"
2323
remove_volume = true
24+
tags = ["devtools", "provider", "packer"]
2425
}
2526
2627
build {
@@ -29,7 +30,8 @@ build {
2930
`,
3031
Checks: []tester.PackerCheck{
3132
checks.Image(zone, "packer-e2e-simple").
32-
RootVolumeType("sbs_snapshot"),
33+
RootVolumeType("sbs_snapshot").
34+
Tags([]string{"devtools", "provider", "packer"}),
3335
checks.NoVolume(zone),
3436
},
3537
})

internal/tests/testdata/block.cassette.yaml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ interactions:
1616
form: {}
1717
headers:
1818
User-Agent:
19-
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.5; darwin; arm64)
20-
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/images?name=packer-e2e-block&page=1&project=b577a333-d52a-4d86-9310-b413d5fe9bb1
19+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.24.0; darwin; amd64)
20+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/images?name=packer-e2e-block&page=1&project=564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5
2121
method: GET
2222
response:
2323
proto: HTTP/2.0
@@ -27,7 +27,7 @@ interactions:
2727
trailer: {}
2828
content_length: 720
2929
uncompressed: false
30-
body: '{"images": [{"id": "37a6357e-f971-4fbf-a80d-1fc6b7cc328a", "name": "packer-e2e-block", "organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b", "project": "b577a333-d52a-4d86-9310-b413d5fe9bb1", "root_volume": {"volume_type": "sbs_snapshot", "id": "c9e1d66a-a330-4636-9419-b5f660df4d6e", "size": 0, "name": ""}, "extra_volumes": {"1": {"volume_type": "sbs_snapshot", "id": "1057e812-e695-43b1-be59-75d40c4433bd", "size": 0, "name": ""}}, "public": false, "arch": "x86_64", "creation_date": "2025-02-10T15:36:41.434137+00:00", "modification_date": "2025-02-10T15:36:41.434137+00:00", "default_bootscript": null, "from_server": "c12a0843-fb85-4985-9441-2dc15014ed13", "state": "available", "tags": [], "zone": "fr-par-1"}]}'
30+
body: '{"images": [{"id": "3d85957d-5b0a-4f7d-ab2f-719188eb9f8f", "name": "packer-e2e-block", "organization": "564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5", "project": "564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5", "root_volume": {"volume_type": "sbs_snapshot", "id": "dad059d6-6dde-498f-9f87-4f657d06dea9", "size": 0, "name": ""}, "extra_volumes": {"1": {"volume_type": "sbs_snapshot", "id": "10e765f3-33ad-49c1-add4-188102d1f71d", "size": 0, "name": ""}}, "public": false, "arch": "x86_64", "creation_date": "2025-04-07T07:39:13.824910+00:00", "modification_date": "2025-04-07T07:39:13.824910+00:00", "default_bootscript": null, "from_server": "82f66ae2-fdd2-4f26-91be-91bbe6a0bb9d", "state": "available", "tags": [], "zone": "fr-par-1"}]}'
3131
headers:
3232
Content-Length:
3333
- "720"
@@ -36,24 +36,24 @@ interactions:
3636
Content-Type:
3737
- application/json
3838
Date:
39-
- Mon, 10 Feb 2025 15:36:44 GMT
39+
- Mon, 07 Apr 2025 07:39:16 GMT
4040
Link:
41-
- </images?page=1&per_page=50&name=packer-e2e-block&project=b577a333-d52a-4d86-9310-b413d5fe9bb1>; rel="last"
41+
- </images?page=1&per_page=50&name=packer-e2e-block&project=564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5>; rel="last"
4242
Server:
43-
- Scaleway API Gateway (fr-par-1;edge02)
43+
- Scaleway API Gateway (fr-par-3;edge03)
4444
Strict-Transport-Security:
4545
- max-age=63072000
4646
X-Content-Type-Options:
4747
- nosniff
4848
X-Frame-Options:
4949
- DENY
5050
X-Request-Id:
51-
- 3470787c-c644-4234-9b58-70dcaa8e776c
51+
- e2400454-0582-4b23-8489-ab593ded107e
5252
X-Total-Count:
5353
- "1"
5454
status: 200 OK
5555
code: 200
56-
duration: 384.537ms
56+
duration: 198.418486ms
5757
- id: 1
5858
request:
5959
proto: HTTP/1.1
@@ -69,8 +69,8 @@ interactions:
6969
form: {}
7070
headers:
7171
User-Agent:
72-
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.5; darwin; arm64)
73-
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes?project=b577a333-d52a-4d86-9310-b413d5fe9bb1
72+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.24.0; darwin; amd64)
73+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes?project=564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5
7474
method: GET
7575
response:
7676
proto: HTTP/2.0
@@ -89,24 +89,24 @@ interactions:
8989
Content-Type:
9090
- application/json
9191
Date:
92-
- Mon, 10 Feb 2025 15:36:44 GMT
92+
- Mon, 07 Apr 2025 07:39:16 GMT
9393
Link:
94-
- </volumes?page=0&per_page=100&project=b577a333-d52a-4d86-9310-b413d5fe9bb1>; rel="last"
94+
- </volumes?page=0&per_page=100&project=564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5>; rel="last"
9595
Server:
96-
- Scaleway API Gateway (fr-par-1;edge02)
96+
- Scaleway API Gateway (fr-par-3;edge03)
9797
Strict-Transport-Security:
9898
- max-age=63072000
9999
X-Content-Type-Options:
100100
- nosniff
101101
X-Frame-Options:
102102
- DENY
103103
X-Request-Id:
104-
- d41b6018-2520-4663-94b5-005fd9eece59
104+
- 308ce6e9-afe3-4581-9f0a-295e55d46ae4
105105
X-Total-Count:
106106
- "0"
107107
status: 200 OK
108108
code: 200
109-
duration: 66.034792ms
109+
duration: 113.311892ms
110110
- id: 2
111111
request:
112112
proto: HTTP/1.1
@@ -122,8 +122,8 @@ interactions:
122122
form: {}
123123
headers:
124124
User-Agent:
125-
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.5; darwin; arm64)
126-
url: https://api.scaleway.com/block/v1alpha1/zones/fr-par-1/volumes?order_by=created_at_asc&project_id=b577a333-d52a-4d86-9310-b413d5fe9bb1
125+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.24.0; darwin; amd64)
126+
url: https://api.scaleway.com/block/v1alpha1/zones/fr-par-1/volumes?order_by=created_at_asc&project_id=564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5
127127
method: GET
128128
response:
129129
proto: HTTP/2.0
@@ -142,17 +142,17 @@ interactions:
142142
Content-Type:
143143
- application/json
144144
Date:
145-
- Mon, 10 Feb 2025 15:36:45 GMT
145+
- Mon, 07 Apr 2025 07:39:16 GMT
146146
Server:
147-
- Scaleway API Gateway (fr-par-1;edge02)
147+
- Scaleway API Gateway (fr-par-3;edge03)
148148
Strict-Transport-Security:
149149
- max-age=63072000
150150
X-Content-Type-Options:
151151
- nosniff
152152
X-Frame-Options:
153153
- DENY
154154
X-Request-Id:
155-
- cbf10e3f-5cb5-4de6-a599-c144fdee008d
155+
- 85411b7a-cdbf-4e70-b4f6-9990605ab5ef
156156
status: 200 OK
157157
code: 200
158-
duration: 54.641125ms
158+
duration: 34.368324ms

0 commit comments

Comments
 (0)