Skip to content

Commit e1bd239

Browse files
authored
Merge pull request #17 from tencentcloudstack/feat/support-image-family
Feat/support image family
2 parents f0edf46 + c916d99 commit e1bd239

File tree

15 files changed

+247
-29
lines changed

15 files changed

+247
-29
lines changed

.web-docs/components/builder/cvm/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
6262
- `tag_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
6363
if tce cloud you should set a tce tag endpoint.
6464

65+
- `org_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
66+
if tce cloud you should set a tce organization endpoint.
67+
6568
- `security_token` (string) - STS access token, can be set through template or by exporting
6669
as environment variable such as `export TENCENTCLOUD_SECURITY_TOKEN=value`.
6770

@@ -108,6 +111,14 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
108111

109112
- `skip_create_image` (bool) - Skip creating an image. When set to true, you don't need to enter target image information, share, copy, etc. The default value is false.
110113

114+
- `is_share_org_members` (bool) - After creating the image,
115+
whether to share it with other accounts in the organization
116+
where the current account is located.
117+
The image can be copied to a maximum of 50 accounts,
118+
with ImageShareAccounts being the priority.
119+
120+
- `image_family` (string) - Image family. Example value: business-daily-update.
121+
111122
<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->
112123

113124

builder/tencentcloud/cvm/access_config.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import (
1212
"strconv"
1313

1414
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
15-
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
16-
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
17-
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
1815
)
1916

2017
const (
@@ -85,6 +82,9 @@ type TencentCloudAccessConfig struct {
8582
// The endpoint you want to reach the cloud endpoint,
8683
// if tce cloud you should set a tce tag endpoint.
8784
TagEndpoint string `mapstructure:"tag_endpoint" required:"false"`
85+
// The endpoint you want to reach the cloud endpoint,
86+
// if tce cloud you should set a tce organization endpoint.
87+
OrgEndpoint string `mapstructure:"org_endpoint" required:"false"`
8888
// The region validation can be skipped if this value is true, the default
8989
// value is false.
9090
skipValidation bool
@@ -125,31 +125,42 @@ type TencentCloudAccessRole struct {
125125
SessionDuration int `mapstructure:"session_duration" required:"false"`
126126
}
127127

128-
func (cf *TencentCloudAccessConfig) Client() (*cvm.Client, *vpc.Client, *tag.Client, error) {
128+
func (cf *TencentCloudAccessConfig) Client() (map[string]interface{}, error) {
129129
var (
130-
err error
131-
cvm_client *cvm.Client
132-
vpc_client *vpc.Client
133-
tag_client *tag.Client
130+
err error
131+
// cvm_client *cvm.Client
132+
// vpc_client *vpc.Client
133+
// tag_client *tag.Client
134+
// org_client *org.Client
135+
// cam_client *cam.Client
134136
)
135137

136138
if err = cf.validateRegion(); err != nil {
137-
return nil, nil, nil, err
139+
return nil, err
140+
}
141+
142+
clientMap := map[string]interface{}{}
143+
if clientMap["cvm_client"], err = NewCvmClient(cf); err != nil {
144+
return nil, err
145+
}
146+
147+
if clientMap["vpc_client"], err = NewVpcClient(cf); err != nil {
148+
return nil, err
138149
}
139150

140-
if cvm_client, err = NewCvmClient(cf); err != nil {
141-
return nil, nil, nil, err
151+
if clientMap["tag_client"], err = NewTagClient(cf); err != nil {
152+
return nil, err
142153
}
143154

144-
if vpc_client, err = NewVpcClient(cf); err != nil {
145-
return nil, nil, nil, err
155+
if clientMap["org_client"], err = NewOrgClient(cf); err != nil {
156+
return nil, err
146157
}
147158

148-
if tag_client, err = NewTagClient(cf); err != nil {
149-
return nil, nil, nil, err
159+
if clientMap["cam_client"], err = NewCamClient(cf); err != nil {
160+
return nil, err
150161
}
151162

152-
return cvm_client, vpc_client, tag_client, nil
163+
return clientMap, nil
153164
}
154165

155166
func (cf *TencentCloudAccessConfig) Prepare(ctx *interpolate.Context) []error {

builder/tencentcloud/cvm/builder.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import (
1717
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1818
"github.com/hashicorp/packer-plugin-sdk/template/config"
1919
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
20+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
21+
vm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
22+
org "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
23+
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
24+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
2025
)
2126

2227
const BuilderId = "tencent.cloud"
@@ -75,16 +80,18 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
7580
}
7681

7782
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
78-
cvmClient, vpcClient, tagClient, err := b.config.Client()
83+
clientMap, err := b.config.Client()
7984
if err != nil {
8085
return nil, err
8186
}
8287

8388
state := new(multistep.BasicStateBag)
8489
state.Put("config", &b.config)
85-
state.Put("cvm_client", cvmClient)
86-
state.Put("vpc_client", vpcClient)
87-
state.Put("tag_client", tagClient)
90+
state.Put("cvm_client", clientMap["cvm_client"].(*vm.Client))
91+
state.Put("vpc_client", clientMap["vpc_client"].(*vpc.Client))
92+
state.Put("tag_client", clientMap["tag_client"].(*tag.Client))
93+
state.Put("org_client", clientMap["org_client"].(*org.Client))
94+
state.Put("cam_client", clientMap["cam_client"].(*cam.Client))
8895
state.Put("hook", hook)
8996
state.Put("ui", ui)
9097

@@ -155,6 +162,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
155162
},
156163
&stepShareImage{
157164
b.config.ImageShareAccounts,
165+
b.config.IsShareOrgMembers,
158166
},
159167
&stepCopyImage{
160168
DestinationRegions: b.config.ImageCopyRegions,
@@ -177,7 +185,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
177185
artifact := &Artifact{
178186
TencentCloudImages: state.Get("tencentcloudimages").(map[string]string),
179187
BuilderIdValue: BuilderId,
180-
Client: cvmClient,
188+
Client: clientMap["cvm_client"].(*vm.Client),
181189
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
182190
}
183191

builder/tencentcloud/cvm/builder.hcl2spec.go

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

builder/tencentcloud/cvm/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
package cvm
55

66
import (
7+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
78
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
89
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
910
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
11+
org "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
1012
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
1113
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
1214
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
@@ -22,6 +24,8 @@ type TencentCloudClient struct {
2224
cvmConn *cvm.Client
2325
stsConn *sts.Client
2426
tagConn *tag.Client
27+
orgConn *org.Client
28+
camConn *cam.Client
2529
}
2630

2731
func (me *TencentCloudClient) UseVpcClient(cpf *profile.ClientProfile) *vpc.Client {
@@ -65,3 +69,24 @@ func (me *TencentCloudClient) UseTagClient(cpf *profile.ClientProfile) *tag.Clie
6569

6670
return me.tagConn
6771
}
72+
73+
func (me *TencentCloudClient) UseOrgClient(cpf *profile.ClientProfile) *org.Client {
74+
if me.orgConn != nil {
75+
return me.orgConn
76+
}
77+
78+
me.orgConn, _ = org.NewClient(me.Credential, me.Region, cpf)
79+
80+
return me.orgConn
81+
}
82+
83+
func (me *TencentCloudClient) UseCamClient() *cam.Client {
84+
if me.camConn != nil {
85+
return me.camConn
86+
}
87+
88+
cpf := me.ClientProfile
89+
me.camConn, _ = cam.NewClient(me.Credential, me.Region, cpf)
90+
91+
return me.camConn
92+
}

builder/tencentcloud/cvm/common.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"github.com/hashicorp/packer-plugin-sdk/multistep"
1515
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1616
"github.com/hashicorp/packer-plugin-sdk/retry"
17+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
1718
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1819
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1920
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
2021
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
22+
org "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
2123
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
2224
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
2325
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
@@ -181,6 +183,35 @@ func NewTagClient(cf *TencentCloudAccessConfig) (client *tag.Client, err error)
181183
return
182184
}
183185

186+
// NewVpcClient returns a new organization client
187+
func NewOrgClient(cf *TencentCloudAccessConfig) (client *org.Client, err error) {
188+
apiV3Conn, err := packerConfigClient(cf)
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
orgClientProfile, err := newClientProfile(cf.OrgEndpoint)
194+
if err != nil {
195+
return nil, err
196+
}
197+
198+
client = apiV3Conn.UseOrgClient(orgClientProfile)
199+
200+
return
201+
}
202+
203+
// NewCamClient returns a new cam client
204+
func NewCamClient(cf *TencentCloudAccessConfig) (client *cam.Client, err error) {
205+
apiV3Conn, err := packerConfigClient(cf)
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
client = apiV3Conn.UseCamClient()
211+
212+
return
213+
}
214+
184215
// CheckResourceIdFormat check resource id format
185216
func CheckResourceIdFormat(resource string, id string) bool {
186217
regex := regexp.MustCompile(fmt.Sprintf("%s-[0-9a-z]{8}$", resource))

builder/tencentcloud/cvm/image_config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ type TencentCloudImageConfig struct {
3737
skipValidation bool
3838
// Skip creating an image. When set to true, you don't need to enter target image information, share, copy, etc. The default value is false.
3939
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
40+
// After creating the image,
41+
// whether to share it with other accounts in the organization
42+
// where the current account is located.
43+
// The image can be copied to a maximum of 50 accounts,
44+
// with ImageShareAccounts being the priority.
45+
IsShareOrgMembers bool `mapstructure:"is_share_org_members" required:"false"`
46+
// Image family. Example value: business-daily-update.
47+
ImageFamily string `mapstructure:"image_family" required:"false"`
4048
}
4149

4250
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {

builder/tencentcloud/cvm/step_create_image.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
3535
req := cvm.NewCreateImageRequest()
3636
req.ImageName = &config.ImageName
3737
req.ImageDescription = &config.ImageDescription
38+
req.ImageFamily = &config.ImageFamily
3839
req.InstanceId = instance.InstanceId
3940

4041
// TODO: We should allow user to specify which data disk should be

0 commit comments

Comments
 (0)