Skip to content

Commit 20c5808

Browse files
authored
add internet_charge_type and bandwidth_package_id support (#49)
* add internet_charge_type and bandwidth_package_id support * update go mod * fixed config validation
1 parent e6aebdf commit 20c5808

10 files changed

Lines changed: 55 additions & 44 deletions

File tree

builder/tencentcloud/cvm/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
115115
DiskSize: b.config.DiskSize,
116116
DataDisks: b.config.DataDisks,
117117
HostName: b.config.HostName,
118+
InternetChargeType: b.config.InternetChargeType,
118119
InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut,
120+
BandwidthPackageId: b.config.BandwidthPackageId,
119121
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
120122
Tags: b.config.RunTags,
121123
},

builder/tencentcloud/cvm/builder.hcl2spec.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/tencentcloud/cvm/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ func WaitForInstance(ctx context.Context, client *cvm.Client, instanceId string,
3939
return fmt.Errorf("instance(%s) not exist", instanceId)
4040
}
4141
if *resp.Response.InstanceSet[0].InstanceState == status &&
42-
(resp.Response.InstanceSet[0].LatestOperationState == nil || *resp.Response.InstanceSet[0].LatestOperationState != "OPERATING") {
42+
(resp.Response.InstanceSet[0].LatestOperationState == nil ||
43+
*resp.Response.InstanceSet[0].LatestOperationState != "OPERATING") {
4344
break
4445
}
4546
time.Sleep(DefaultWaitForInterval * time.Second)

builder/tencentcloud/cvm/run_config.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ type TencentCloudRunConfig struct {
6969
CidrBlock string `mapstructure:"cidr_block" required:"false"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16
7070
// Specify cider block of the subnet you will create if
7171
// subnet_id not set
72-
SubnectCidrBlock string `mapstructure:"subnect_cidr_block" required:"false"`
73-
InternetChargeType string `mapstructure:"internet_charge_type"`
72+
SubnectCidrBlock string `mapstructure:"subnect_cidr_block" required:"false"`
73+
// Internet charge type of cvm, values can be TRAFFIC_POSTPAID_BY_HOUR, BANDWIDTH_POSTPAID_BY_HOUR, BANDWIDTH_PACKAGE
74+
InternetChargeType string `mapstructure:"internet_charge_type" required:"false"`
7475
// Max bandwidth out your cvm will be launched by(in MB).
7576
// values can be set between 1 ~ 100.
7677
InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out" required:"false"`
78+
// When internet_charge_type is BANDWIDTH_PACKAGE, bandwidth_package_id is required
79+
BandwidthPackageId string `mapstructure:"bandwidth_package_id" required:"false"`
7780
// Specify securitygroup your cvm will be launched by.
7881
SecurityGroupId string `mapstructure:"security_group_id" required:"false"`
7982
// Specify security name you will create if security_group_id not set.
@@ -171,6 +174,24 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
171174
cf.DiskSize = 50
172175
}
173176

177+
if cf.InternetChargeType == "" {
178+
cf.InternetChargeType = "TRAFFIC_POSTPAID_BY_HOUR"
179+
}
180+
181+
validChargeTypes := map[string]int{
182+
"TRAFFIC_POSTPAID_BY_HOUR": 0,
183+
"BANDWIDTH_POSTPAID_BY_HOUR": 0,
184+
"BANDWIDTH_PACKAGE": 0,
185+
}
186+
if _, ok := validChargeTypes[cf.InternetChargeType]; !ok {
187+
errs = append(errs, fmt.Errorf("specified internet_charge_type(%s) is invalid.", cf.InternetChargeType))
188+
}
189+
190+
if cf.InternetChargeType == "BANDWIDTH_PACKAGE" && cf.BandwidthPackageId == "" {
191+
errs = append(errs,
192+
fmt.Errorf("bandwidth_package_id is required when internet_charge_type is BANDWIDTH_PACKAGE"))
193+
}
194+
174195
if cf.AssociatePublicIpAddress && cf.InternetMaxBandwidthOut <= 0 {
175196
cf.InternetMaxBandwidthOut = 1
176197
}

builder/tencentcloud/cvm/step_config_subnet.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func (s *stepConfigSubnet) Run(ctx context.Context, state multistep.StateBag) mu
3737
if *resp.Response.TotalCount > 0 {
3838
s.isCreate = false
3939
if *resp.Response.SubnetSet[0].VpcId != vpcId {
40-
return Halt(state, fmt.Errorf("The specified subnet(%s) does not belong to the specified vpc(%s)", s.SubnetId, vpcId), "")
40+
return Halt(state, fmt.Errorf("The specified subnet(%s) does not belong to the specified vpc(%s)",
41+
s.SubnetId, vpcId), "")
4142
}
4243
state.Put("subnet_id", *resp.Response.SubnetSet[0].SubnetId)
4344
Message(state, *resp.Response.SubnetSet[0].SubnetName, "Subnet found")

builder/tencentcloud/cvm/step_run_instance.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99

1010
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1112
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
1213
)
1314

@@ -21,7 +22,9 @@ type stepRunInstance struct {
2122
DiskType string
2223
DiskSize int64
2324
HostName string
25+
InternetChargeType string
2426
InternetMaxBandwidthOut int64
27+
BandwidthPackageId string
2528
AssociatePublicIpAddress bool
2629
Tags map[string]string
2730
DataDisks []tencentCloudDataDisk
@@ -103,11 +106,14 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul
103106
VpcId: &vpc_id,
104107
SubnetId: &subnet_id,
105108
}
106-
TRAFFIC_POSTPAID_BY_HOUR := "TRAFFIC_POSTPAID_BY_HOUR"
107109
if s.AssociatePublicIpAddress {
108110
req.InternetAccessible = &cvm.InternetAccessible{
109-
InternetChargeType: &TRAFFIC_POSTPAID_BY_HOUR,
111+
InternetChargeType: &s.InternetChargeType,
110112
InternetMaxBandwidthOut: &s.InternetMaxBandwidthOut,
113+
PublicIpAssigned: common.BoolPtr(true),
114+
}
115+
if s.BandwidthPackageId != "" {
116+
req.InternetAccessible.BandwidthPackageId = &s.BandwidthPackageId
111117
}
112118
}
113119
req.InstanceName = &s.InstanceName
@@ -125,6 +131,8 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul
125131
req.UserData = &userData
126132
var tags []*cvm.Tag
127133
for k, v := range s.Tags {
134+
k := k
135+
v := v
128136
tags = append(tags, &cvm.Tag{
129137
Key: &k,
130138
Value: &v,

builder/tencentcloud/cvm/step_share_image.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (s *stepShareImage) Cleanup(state multistep.StateBag) {
6363
req.Permission = common.StringPtr("CANCEL")
6464
accounts := make([]*string, 0, len(s.ShareAccounts))
6565
for _, account := range s.ShareAccounts {
66+
account := account
6667
accounts = append(accounts, &account)
6768
}
6869
req.AccountIds = accounts

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
- `subnect_cidr_block` (string) - Specify cider block of the subnet you will create if
4646
subnet_id not set
4747

48-
- `internet_charge_type` (string) - Internet Charge Type
48+
- `internet_charge_type` (string) - Internet charge type of cvm, values can be TRAFFIC_POSTPAID_BY_HOUR, BANDWIDTH_POSTPAID_BY_HOUR, BANDWIDTH_PACKAGE
4949

5050
- `internet_max_bandwidth_out` (int64) - Max bandwidth out your cvm will be launched by(in MB).
5151
values can be set between 1 ~ 100.
5252

53+
- `bandwidth_package_id` (string) - When internet_charge_type is BANDWIDTH_PACKAGE, bandwidth_package_id is required
54+
5355
- `security_group_id` (string) - Specify securitygroup your cvm will be launched by.
5456

5557
- `security_group_name` (string) - Specify security name you will create if security_group_id not set.

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ require (
66
github.com/hashicorp/hcl/v2 v2.10.1
77
github.com/hashicorp/packer-plugin-sdk v0.2.7
88
github.com/pkg/errors v0.9.1
9-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.233
10-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.233
11-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.233
9+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.273
10+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.273
11+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.273
1212
github.com/zclconf/go-cty v1.9.1
1313
)
1414

0 commit comments

Comments
 (0)