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.
This commit is contained in:
ZhiQiang Fan 2019-06-28 23:18:41 +08:00
parent 44d2198a1b
commit 310a40f8fe
5 changed files with 54 additions and 22 deletions

View File

@ -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,

View File

@ -31,6 +31,7 @@ type TencentCloudRunConfig struct {
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
}

View File

@ -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 {

View File

@ -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",

View File

@ -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",