Skip to content

Commit 5a6d197

Browse files
JinXingYounglbajolet-hashicorp
authored andcommitted
support TCE
1 parent b790482 commit 5a6d197

7 files changed

Lines changed: 105 additions & 7 deletions

File tree

builder/tencentcloud/cvm/access_config.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ type TencentCloudAccessConfig struct {
6464
Zone string `mapstructure:"zone" required:"true"`
6565
// Do not check region and zone when validate.
6666
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
67+
// The endpoint you want to reach the cloud endpoint,
68+
// if tce cloud you should set a tce cvm endpoint.
69+
CvmEndpoint string `mapstructure:"cvm_endpoint" required:"false"`
70+
// The endpoint you want to reach the cloud endpoint,
71+
// if tce cloud you should set a tce vpc endpoint.
72+
VpcEndpoint string `mapstructure:"vpc_endpoint" required:"false"`
6773
}
6874

6975
func (cf *TencentCloudAccessConfig) Client() (*cvm.Client, *vpc.Client, error) {
@@ -82,11 +88,11 @@ func (cf *TencentCloudAccessConfig) Client() (*cvm.Client, *vpc.Client, error) {
8288
return nil, nil, fmt.Errorf("parameter zone must be set")
8389
}
8490

85-
if cvm_client, err = NewCvmClient(cf.SecretId, cf.SecretKey, cf.Region); err != nil {
91+
if cvm_client, err = NewCvmClient(cf.SecretId, cf.SecretKey, cf.Region, cf.CvmEndpoint); err != nil {
8692
return nil, nil, err
8793
}
8894

89-
if vpc_client, err = NewVpcClient(cf.SecretId, cf.SecretKey, cf.Region); err != nil {
95+
if vpc_client, err = NewVpcClient(cf.SecretId, cf.SecretKey, cf.Region, cf.VpcEndpoint); err != nil {
9096
return nil, nil, err
9197
}
9298

@@ -116,6 +122,11 @@ func (cf *TencentCloudAccessConfig) Prepare(ctx *interpolate.Context) []error {
116122
errs = append(errs, err)
117123
}
118124

125+
if (cf.CvmEndpoint != "" && cf.VpcEndpoint == "") ||
126+
(cf.CvmEndpoint == "" && cf.VpcEndpoint != "") {
127+
errs = append(errs, fmt.Errorf("parameter cvm_endpoint and vpc_endpoint must be set simultaneously"))
128+
}
129+
119130
if cf.Region == "" {
120131
errs = append(errs, fmt.Errorf("parameter region must be set"))
121132
} else if !cf.SkipValidation {
@@ -148,6 +159,10 @@ func (cf *TencentCloudAccessConfig) Config() error {
148159
}
149160

150161
func (cf *TencentCloudAccessConfig) validateRegion() error {
162+
// if set cvm endpoint, do not validate region
163+
if cf.CvmEndpoint != "" {
164+
return nil
165+
}
151166
return validRegion(cf.Region)
152167
}
153168

builder/tencentcloud/cvm/builder.hcl2spec.go

Lines changed: 4 additions & 0 deletions
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: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cvm
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67
"regexp"
78
"strings"
89
"time"
@@ -105,11 +106,24 @@ func GetImageByName(ctx context.Context, client *cvm.Client, imageName string) (
105106
}
106107

107108
// NewCvmClient returns a new cvm client
108-
func NewCvmClient(secretId, secretKey, region string) (client *cvm.Client, err error) {
109+
func NewCvmClient(secretId, secretKey, region, endpoint string) (client *cvm.Client, err error) {
109110
cpf := profile.NewClientProfile()
110111
cpf.HttpProfile.ReqMethod = "POST"
111112
cpf.HttpProfile.ReqTimeout = 300
112113
cpf.Language = "en-US"
114+
if endpoint != "" {
115+
var u *url.URL
116+
u, err = url.Parse(endpoint)
117+
if err != nil {
118+
return
119+
}
120+
if u.Scheme != "" {
121+
cpf.HttpProfile.Scheme = u.Scheme
122+
cpf.HttpProfile.Endpoint = u.Host
123+
} else {
124+
cpf.HttpProfile.Endpoint = endpoint
125+
}
126+
}
113127

114128
credential := common.NewCredential(secretId, secretKey)
115129
client, err = cvm.NewClient(credential, region, cpf)
@@ -118,11 +132,24 @@ func NewCvmClient(secretId, secretKey, region string) (client *cvm.Client, err e
118132
}
119133

120134
// NewVpcClient returns a new vpc client
121-
func NewVpcClient(secretId, secretKey, region string) (client *vpc.Client, err error) {
135+
func NewVpcClient(secretId, secretKey, region, endpoint string) (client *vpc.Client, err error) {
122136
cpf := profile.NewClientProfile()
123137
cpf.HttpProfile.ReqMethod = "POST"
124138
cpf.HttpProfile.ReqTimeout = 300
125139
cpf.Language = "en-US"
140+
if endpoint != "" {
141+
var u *url.URL
142+
u, err = url.Parse(endpoint)
143+
if err != nil {
144+
return
145+
}
146+
if u.Scheme != "" {
147+
cpf.HttpProfile.Scheme = u.Scheme
148+
cpf.HttpProfile.Endpoint = u.Host
149+
} else {
150+
cpf.HttpProfile.Endpoint = endpoint
151+
}
152+
}
126153

127154
credential := common.NewCredential(secretId, secretKey)
128155
client, err = vpc.NewClient(credential, region, cpf)

builder/tencentcloud/cvm/step_copy_image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
4949
tencentCloudImages := state.Get("tencentcloudimages").(map[string]string)
5050

5151
for _, region := range req.DestinationRegions {
52-
rc, err := NewCvmClient(config.SecretId, config.SecretKey, *region)
52+
rc, err := NewCvmClient(config.SecretId, config.SecretKey, *region, config.CvmEndpoint)
5353
if err != nil {
5454
return Halt(state, err, "Failed to init client")
5555
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
variable "secret_id" {
2+
type = string
3+
default = "${env("TENCENTCLOUD_SECRET_ID")}"
4+
}
5+
6+
variable "secret_key" {
7+
type = string
8+
default = "${env("TENCENTCLOUD_SECRET_KEY")}"
9+
}
10+
11+
variable "cvm_endpoint" {
12+
type = string
13+
}
14+
variable "vpc_endpoint" {
15+
type = string
16+
}
17+
18+
source "tencentcloud-cvm" "autogenerated_1" {
19+
associate_public_ip_address = true
20+
disk_type = "CLOUD_PREMIUM"
21+
image_name = "PackerTest"
22+
instance_type = "S3.SMALL1"
23+
packer_debug = true
24+
region = "chongqing"
25+
secret_id = "${var.secret_id}"
26+
secret_key = "${var.secret_key}"
27+
cvm_endpoint = "${var.cvm_endpoint}"
28+
vpc_endpoint = "${var.vpc_endpoint}"
29+
source_image_id = "img-95xgn7er"
30+
ssh_username = "root"
31+
zone = "yfm18"
32+
}
33+
34+
build {
35+
sources = ["source.tencentcloud-cvm.autogenerated_1"]
36+
37+
provisioner "shell" {
38+
inline = ["sleep 30"]
39+
}
40+
}

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

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

33
- `skip_region_validation` (bool) - Do not check region and zone when validate.
44

5+
- `cvm_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
6+
if tce cloud you should set a tce cvm endpoint.
7+
8+
- `vpc_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
9+
if tce cloud you should set a tce vpc endpoint.
10+
511
<!-- End of code generated from the comments of the TencentCloudAccessConfig struct in builder/tencentcloud/cvm/access_config.go; -->

docs/builders/cvm.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
3737
for parameter taking.
3838

3939
- `instance_type` (string) - The instance type your cvm will be launched by.
40-
You should reference [Instace Type](https://intl.cloud.tencent.com/document/product/213/11518)
40+
You should reference [Instance Type](https://intl.cloud.tencent.com/document/product/213/11518)
4141
for parameter taking.
4242

4343
- `source_image_id` (string) - The base image id of Image you want to create
@@ -125,6 +125,12 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
125125
- `run_tags` (map of strings) - Tags to apply to the instance that is _launched_ to create the image.
126126
These tags are _not_ applied to the resulting image.
127127

128+
- `cvm_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
129+
if tce cloud you should set a tce cvm endpoint.
130+
131+
- `vpc_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
132+
if tce cloud you should set a tce vpc endpoint.
133+
128134
### Communicator Configuration
129135

130136
In addition to the above options, a communicator can be configured
@@ -183,5 +189,5 @@ Here is a basic example for Tencentcloud.
183189
```
184190

185191
See the
186-
[examples/tencentcloud](https://github.com/hashicorp/packer/tree/master/builder/tencentcloud/examples)
192+
[examples/tencentcloud](https://github.com/hashicorp/packer-plugin-tencentcloud/tree/master/builder/tencentcloud/examples)
187193
folder in the Packer project for more examples.

0 commit comments

Comments
 (0)