Skip to content

Commit

Permalink
feat: add support for tags (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored Jul 12, 2024
1 parent caa5643 commit 3a2f91b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .web-docs/components/builder/scaleway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ can also be supplied to override the typical auto-generated key:

- `user_data_timeout` (duration string | ex: "1h5m2s") - A custom timeout for user data to assure its completion. Defaults to "0s"

- `tags` ([]string) - A list of tags to apply on the created image, volumes, and snapshots

- `api_token` (string) - The token to use to authenticate with your account.
It can also be specified via environment variable SCALEWAY_API_TOKEN. You
can see and generate tokens in the "Credentials"
Expand Down
1 change: 1 addition & 0 deletions builder/scaleway/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ source "scaleway" "basic" {
ssh_username = "root"
zone = "fr-par-1"
remove_volume = true
tags = ["devtools", "provider", "packer"]
}
build {
Expand Down
3 changes: 3 additions & 0 deletions builder/scaleway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type Config struct {
// A custom timeout for user data to assure its completion. Defaults to "0s"
UserDataTimeout time.Duration `mapstructure:"user_data_timeout" required:"false"`

// A list of tags to apply on the created image, volumes, and snapshots
Tags []string `mapstructure:"tags" required:"false"`

UserAgent string `mapstructure-to-hcl2:",skip"`
ctx interpolate.Context

Expand Down
2 changes: 2 additions & 0 deletions builder/scaleway/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 45 additions & 1 deletion builder/scaleway/step_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ func (s *stepBackup) Run(ctx context.Context, state multistep.StateBag) multiste
return multistep.ActionHalt
}

state.Put("snapshots", artifactSnapshotFromImage(image))
snapshots := artifactSnapshotFromImage(image)

// Apply tags to image, volumes and snapshots
if len(c.Tags) != 0 {
err = applyTags(instanceAPI, scw.Zone(c.Zone), imageID, server.Volumes, snapshots, c.Tags, ctx)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}

state.Put("snapshots", snapshots)
state.Put("image_id", image.ID)
state.Put("image_name", c.ImageName)
state.Put("region", c.Zone) // Deprecated
Expand Down Expand Up @@ -104,3 +116,35 @@ func imageIDFromBackupResult(hrefResult string) (string, error) {

return imageID, nil
}

func applyTags(instanceAPI *instance.API, zone scw.Zone, imageID string, volumes map[string]*instance.VolumeServer, snapshots []ArtifactSnapshot, tags []string, ctx context.Context) error {
if _, err := instanceAPI.UpdateImage(&instance.UpdateImageRequest{
ImageID: imageID,
Zone: zone,
Tags: &tags,
}, scw.WithContext(ctx)); err != nil {
return fmt.Errorf("failed to set tags on the image: %w", err)
}

for _, volume := range volumes {
if _, err := instanceAPI.UpdateVolume(&instance.UpdateVolumeRequest{
VolumeID: volume.ID,
Zone: zone,
Tags: &tags,
}, scw.WithContext(ctx)); err != nil {
return fmt.Errorf("failed to set tags on the volume: %w", err)
}
}

for _, snapshot := range snapshots {
if _, err := instanceAPI.UpdateSnapshot(&instance.UpdateSnapshotRequest{
SnapshotID: snapshot.ID,
Zone: zone,
Tags: &tags,
}, scw.WithContext(ctx)); err != nil {
return fmt.Errorf("failed to set tags on the snapshot: %w", err)
}
}

return nil
}
2 changes: 2 additions & 0 deletions docs-partials/builder/scaleway/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

- `user_data_timeout` (duration string | ex: "1h5m2s") - A custom timeout for user data to assure its completion. Defaults to "0s"

- `tags` ([]string) - A list of tags to apply on the created image, volumes, and snapshots

- `api_token` (string) - The token to use to authenticate with your account.
It can also be specified via environment variable SCALEWAY_API_TOKEN. You
can see and generate tokens in the "Credentials"
Expand Down
1 change: 1 addition & 0 deletions example/build_scaleway.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ source "scaleway" "basic" {
image_name = "basic build"
ssh_username = "root"
zone = "fr-par-1"
tags = ["foo", "bar" ]
}

build {
Expand Down

0 comments on commit 3a2f91b

Please sign in to comment.