From 310a40f8fedfbe9dfc6158fe96f840c5f8def3c0 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Fri, 28 Jun 2019 23:18:41 +0800 Subject: [PATCH] feature: add run_tags to instance in tencentcloud builder Instance tags are useful, our customer asks us to support it in packer as well, to enable them to identify the purpose of the instance, even the instance runs in a very short time. --- builder/tencentcloud/cvm/builder.go | 1 + builder/tencentcloud/cvm/run_config.go | 45 ++++++++++--------- builder/tencentcloud/cvm/step_run_instance.go | 17 +++++++ examples/tencentcloud/basic.json | 5 ++- .../docs/builders/tencentcloud-cvm.html.md | 8 +++- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/builder/tencentcloud/cvm/builder.go b/builder/tencentcloud/cvm/builder.go index d73428665..8fb894b0a 100644 --- a/builder/tencentcloud/cvm/builder.go +++ b/builder/tencentcloud/cvm/builder.go @@ -107,6 +107,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack HostName: b.config.HostName, InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + Tags: b.config.RunTags, }, &communicator.StepConnect{ Config: &b.config.TencentCloudRunConfig.Comm, diff --git a/builder/tencentcloud/cvm/run_config.go b/builder/tencentcloud/cvm/run_config.go index a4f9320d0..182ee9938 100644 --- a/builder/tencentcloud/cvm/run_config.go +++ b/builder/tencentcloud/cvm/run_config.go @@ -11,26 +11,27 @@ import ( ) type TencentCloudRunConfig struct { - AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"` - SourceImageId string `mapstructure:"source_image_id"` - InstanceType string `mapstructure:"instance_type"` - InstanceName string `mapstructure:"instance_name"` - DiskType string `mapstructure:"disk_type"` - DiskSize int64 `mapstructure:"disk_size"` - VpcId string `mapstructure:"vpc_id"` - VpcName string `mapstructure:"vpc_name"` - VpcIp string `mapstructure:"vpc_ip"` - SubnetId string `mapstructure:"subnet_id"` - SubnetName string `mapstructure:"subnet_name"` - CidrBlock string `mapstructure:"cidr_block"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16 - SubnectCidrBlock string `mapstructure:"subnect_cidr_block"` - InternetChargeType string `mapstructure:"internet_charge_type"` - InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out"` - SecurityGroupId string `mapstructure:"security_group_id"` - SecurityGroupName string `mapstructure:"security_group_name"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` - HostName string `mapstructure:"host_name"` + AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"` + SourceImageId string `mapstructure:"source_image_id"` + InstanceType string `mapstructure:"instance_type"` + InstanceName string `mapstructure:"instance_name"` + DiskType string `mapstructure:"disk_type"` + DiskSize int64 `mapstructure:"disk_size"` + VpcId string `mapstructure:"vpc_id"` + VpcName string `mapstructure:"vpc_name"` + VpcIp string `mapstructure:"vpc_ip"` + SubnetId string `mapstructure:"subnet_id"` + SubnetName string `mapstructure:"subnet_name"` + CidrBlock string `mapstructure:"cidr_block"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16 + SubnectCidrBlock string `mapstructure:"subnect_cidr_block"` + InternetChargeType string `mapstructure:"internet_charge_type"` + InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out"` + SecurityGroupId string `mapstructure:"security_group_id"` + SecurityGroupName string `mapstructure:"security_group_name"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + HostName string `mapstructure:"host_name"` + RunTags map[string]string `mapstructure:"run_tags"` // Communicator settings Comm communicator.Config `mapstructure:",squash"` @@ -120,6 +121,10 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error { cf.HostName = cf.InstanceName[:15] } + if cf.RunTags == nil { + cf.RunTags = make(map[string]string) + } + return errs } diff --git a/builder/tencentcloud/cvm/step_run_instance.go b/builder/tencentcloud/cvm/step_run_instance.go index a8c6c072d..9654b35e9 100644 --- a/builder/tencentcloud/cvm/step_run_instance.go +++ b/builder/tencentcloud/cvm/step_run_instance.go @@ -26,6 +26,7 @@ type stepRunInstance struct { HostName string InternetMaxBandwidthOut int64 AssociatePublicIpAddress bool + Tags map[string]string } func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -89,6 +90,22 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul req.ClientToken = &s.InstanceName req.HostName = &s.HostName req.UserData = &userData + var tags []*cvm.Tag + for k, v := range s.Tags { + tags = append(tags, &cvm.Tag{ + Key: &k, + Value: &v, + }) + } + resourceType := "instance" + if len(tags) > 0 { + req.TagSpecification = []*cvm.TagSpecification{ + &cvm.TagSpecification{ + ResourceType: &resourceType, + Tags: tags, + }, + } + } resp, err := client.RunInstances(req) if err != nil { diff --git a/examples/tencentcloud/basic.json b/examples/tencentcloud/basic.json index 5229a4c73..4e90efd36 100644 --- a/examples/tencentcloud/basic.json +++ b/examples/tencentcloud/basic.json @@ -15,7 +15,10 @@ "image_name": "PackerTest", "disk_type": "CLOUD_PREMIUM", "packer_debug": true, - "associate_public_ip_address": true + "associate_public_ip_address": true, + "run_tags": { + "good": "luck" + } }], "provisioners": [{ "type": "shell", diff --git a/website/source/docs/builders/tencentcloud-cvm.html.md b/website/source/docs/builders/tencentcloud-cvm.html.md index 544dee29e..2ac57401a 100644 --- a/website/source/docs/builders/tencentcloud-cvm.html.md +++ b/website/source/docs/builders/tencentcloud-cvm.html.md @@ -116,6 +116,9 @@ builder. - `host_name` (string) - host name. +- `run_tags` (map of strings) - Tags to apply to the instance that is *launched* to create the image. + These tags are *not* applied to the resulting image. + ## Basic Example Here is a basic example for Tencentcloud. @@ -137,7 +140,10 @@ Here is a basic example for Tencentcloud. "ssh_username" : "root", "image_name": "packerTest2", "packer_debug": true, - "associate_public_ip_address": true + "associate_public_ip_address": true, + "run_tags": { + "good": "luck" + } }], "provisioners": [{ "type": "shell",