diff --git a/.web-docs/components/builder/scaleway/README.md b/.web-docs/components/builder/scaleway/README.md index 9933512..60a50ee 100644 --- a/.web-docs/components/builder/scaleway/README.md +++ b/.web-docs/components/builder/scaleway/README.md @@ -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" diff --git a/builder/scaleway/builder_acc_test.go b/builder/scaleway/builder_acc_test.go index 94bf80a..94e4db0 100644 --- a/builder/scaleway/builder_acc_test.go +++ b/builder/scaleway/builder_acc_test.go @@ -45,6 +45,7 @@ source "scaleway" "basic" { ssh_username = "root" zone = "fr-par-1" remove_volume = true + tags = ["devtools", "provider", "packer"] } build { diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 6d9c02d..8421546 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -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 diff --git a/builder/scaleway/config.hcl2spec.go b/builder/scaleway/config.hcl2spec.go index cc187a2..df4a4c6 100644 --- a/builder/scaleway/config.hcl2spec.go +++ b/builder/scaleway/config.hcl2spec.go @@ -88,6 +88,7 @@ type FlatConfig struct { ServerShutdownTimeout *string `mapstructure:"server_shutdown_timeout" required:"false" cty:"server_shutdown_timeout" hcl:"server_shutdown_timeout"` UserData map[string]string `mapstructure:"user_data" required:"false" cty:"user_data" hcl:"user_data"` UserDataTimeout *string `mapstructure:"user_data_timeout" required:"false" cty:"user_data_timeout" hcl:"user_data_timeout"` + Tags []string `mapstructure:"tags" required:"false" cty:"tags" hcl:"tags"` Token *string `mapstructure:"api_token" required:"false" cty:"api_token" hcl:"api_token"` Organization *string `mapstructure:"organization_id" required:"false" cty:"organization_id" hcl:"organization_id"` Region *string `mapstructure:"region" required:"false" cty:"region" hcl:"region"` @@ -183,6 +184,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "server_shutdown_timeout": &hcldec.AttrSpec{Name: "server_shutdown_timeout", Type: cty.String, Required: false}, "user_data": &hcldec.AttrSpec{Name: "user_data", Type: cty.Map(cty.String), Required: false}, "user_data_timeout": &hcldec.AttrSpec{Name: "user_data_timeout", Type: cty.String, Required: false}, + "tags": &hcldec.AttrSpec{Name: "tags", Type: cty.List(cty.String), Required: false}, "api_token": &hcldec.AttrSpec{Name: "api_token", Type: cty.String, Required: false}, "organization_id": &hcldec.AttrSpec{Name: "organization_id", Type: cty.String, Required: false}, "region": &hcldec.AttrSpec{Name: "region", Type: cty.String, Required: false}, diff --git a/builder/scaleway/step_backup.go b/builder/scaleway/step_backup.go index e0f2928..2b07ba0 100644 --- a/builder/scaleway/step_backup.go +++ b/builder/scaleway/step_backup.go @@ -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 @@ -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 +} diff --git a/docs-partials/builder/scaleway/Config-not-required.mdx b/docs-partials/builder/scaleway/Config-not-required.mdx index 7714214..a6663c1 100644 --- a/docs-partials/builder/scaleway/Config-not-required.mdx +++ b/docs-partials/builder/scaleway/Config-not-required.mdx @@ -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" diff --git a/example/build_scaleway.pkr.hcl b/example/build_scaleway.pkr.hcl index 61a452f..cbb55e2 100644 --- a/example/build_scaleway.pkr.hcl +++ b/example/build_scaleway.pkr.hcl @@ -16,6 +16,7 @@ source "scaleway" "basic" { image_name = "basic build" ssh_username = "root" zone = "fr-par-1" + tags = ["foo", "bar" ] } build {