Skip to content

Commit 616f5d3

Browse files
authored
Add image_tags attribute for setting tags on the created image (#105)
1 parent 5d53d33 commit 616f5d3

6 files changed

Lines changed: 54 additions & 12 deletions

File tree

builder/tencentcloud/cvm/builder.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/tencentcloud/cvm/image_config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type TencentCloudImageConfig struct {
3636
ImageShareAccounts []string `mapstructure:"image_share_accounts" required:"false"`
3737
// Do not check region and zone when validate.
3838
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
39+
// Key/value pair tags that will be applied to the resulting image.
40+
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
3941
}
4042

4143
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
@@ -74,6 +76,10 @@ func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
7476
cf.ImageCopyRegions = regions
7577
}
7678

79+
if cf.ImageTags == nil {
80+
cf.ImageTags = make(map[string]string)
81+
}
82+
7783
if len(errs) > 0 {
7884
return errs
7985
}

builder/tencentcloud/cvm/image_config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ func TestTencentCloudImageConfig_Prepare(t *testing.T) {
3434
if err := cf.Prepare(nil); err != nil {
3535
t.Fatalf("shouldn't have err:%v", err)
3636
}
37+
38+
cf.ImageTags = map[string]string{
39+
"createdBy": "packer",
40+
}
41+
if err := cf.Prepare(nil); err != nil {
42+
t.Fatalf("shouldn't have err:%v", err)
43+
}
3744
}

builder/tencentcloud/cvm/step_create_image.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
5252
req.Sysprep = &False
5353
}
5454

55+
var tags []*cvm.Tag
56+
for k, v := range config.ImageTags {
57+
k := k
58+
v := v
59+
tags = append(tags, &cvm.Tag{
60+
Key: &k,
61+
Value: &v,
62+
})
63+
}
64+
65+
resourceType := "image"
66+
if len(tags) > 0 {
67+
req.TagSpecification = []*cvm.TagSpecification{
68+
{
69+
ResourceType: &resourceType,
70+
Tags: tags,
71+
},
72+
}
73+
}
74+
5575
err := Retry(ctx, func(ctx context.Context) error {
5676
_, e := client.CreateImage(req)
5777
return e

docs-partials/builder/tencentcloud/cvm/TencentCloudImageConfig-not-required.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020

2121
- `skip_region_validation` (bool) - Do not check region and zone when validate.
2222

23+
- `image_tags` (map[string]string) - Key/value pair tags that will be applied to the resulting image.
24+
2325
<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->

example/build.pkr.hcl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,25 @@ variable "secret_key" {
1212
}
1313

1414
source "tencentcloud-cvm" "example" {
15-
associate_public_ip_address = true
16-
disk_type = "CLOUD_PREMIUM"
17-
image_name = "PackerTest"
18-
instance_type = "S4.SMALL1"
19-
packer_debug = true
20-
region = "ap-guangzhou"
21-
run_tags = {
15+
disk_type = "CLOUD_PREMIUM"
16+
image_name = "PackerTest"
17+
instance_type = "SA2.MEDIUM2"
18+
packer_debug = true
19+
region = "ap-guangzhou"
20+
run_tags = {
2221
good = "luck"
2322
}
24-
secret_id = "${var.secret_id}"
25-
secret_key = "${var.secret_key}"
26-
source_image_id = "img-oikl1tzv"
27-
ssh_username = "root"
28-
zone = "ap-guangzhou-4"
23+
secret_id = "${var.secret_id}"
24+
secret_key = "${var.secret_key}"
25+
source_image_id = "img-9qrfy1xt"
26+
ssh_username = "root"
27+
zone = "ap-guangzhou-3"
28+
security_group_id = "sg-r7kju7cf"
29+
associate_public_ip_address = true
30+
image_tags = {
31+
createdBy = "packer"
32+
usedBy = "test"
33+
}
2934
}
3035

3136
build {

0 commit comments

Comments
 (0)