Skip to content

Commit dcc7e3b

Browse files
authored
Merge pull request #18 from tencentcloudstack/dev/source_image_family
support source image family
2 parents e1bd239 + 2c63ddc commit dcc7e3b

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,16 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
128128
Default value is `false`.
129129

130130
- `source_image_id` (string) - The base image id of Image you want to create
131+
You can also specify `source_image_family`. If both `source_image` and `source_image_family` are specified, `source_image` takes precedence.
131132
your customized image from.
132133

133134
- `source_image_name` (string) - The base image name of Image you want to create your
134-
customized image from.Conflict with SourceImageId.
135+
customized image from.Conflict with SourceImageId and SourceImageName.
136+
137+
- `source_image_family` (string) - The source image family to use to create the new image from.
138+
The image family always returns its latest image that is not deprecated.
139+
Conflict with SourceImageId and SourceImageName. It takes effect when SourceImageId and SourceImageName are empty.
140+
Example value: business-daily-update.
135141

136142
- `instance_charge_type` (string) - Charge type of cvm, values can be `POSTPAID_BY_HOUR` (default) `SPOTPAID`
137143

builder/tencentcloud/cvm/builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
101101
&stepPreValidate{
102102
b.config.SkipCreateImage,
103103
},
104+
&stepCheckSourceImageFamily{
105+
b.config.SourceImageId,
106+
b.config.SourceImageName,
107+
},
104108
&stepCheckSourceImage{
105109
b.config.SourceImageId,
106110
},

builder/tencentcloud/cvm/builder.hcl2spec.go

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

builder/tencentcloud/cvm/run_config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ type TencentCloudRunConfig struct {
2929
// Default value is `false`.
3030
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address" required:"false"`
3131
// The base image id of Image you want to create
32+
// You can also specify `source_image_family`. If both `source_image` and `source_image_family` are specified, `source_image` takes precedence.
3233
// your customized image from.
3334
SourceImageId string `mapstructure:"source_image_id" required:"false"`
3435
// The base image name of Image you want to create your
35-
// customized image from.Conflict with SourceImageId.
36+
// customized image from.Conflict with SourceImageId and SourceImageName.
3637
SourceImageName string `mapstructure:"source_image_name" required:"false"`
38+
// The source image family to use to create the new image from.
39+
// The image family always returns its latest image that is not deprecated.
40+
// Conflict with SourceImageId and SourceImageName. It takes effect when SourceImageId and SourceImageName are empty.
41+
// Example value: business-daily-update.
42+
SourceImageFamily string `mapstructure:"source_image_family" required:"false"`
3743
// Charge type of cvm, values can be `POSTPAID_BY_HOUR` (default) `SPOTPAID`
3844
InstanceChargeType string `mapstructure:"instance_charge_type" required:"false"`
3945
// The instance type your cvm will be launched by.
@@ -133,8 +139,8 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
133139
errs = append(errs, errors.New("zone must be specified"))
134140
}
135141

136-
if cf.SourceImageId == "" && cf.SourceImageName == "" {
137-
errs = append(errs, errors.New("source_image_id or source_image_name must be specified"))
142+
if cf.SourceImageId == "" && cf.SourceImageName == "" && cf.SourceImageFamily == "" {
143+
errs = append(errs, errors.New("source_image_id or source_image_name or source_image_family must be specified"))
138144
}
139145

140146
if cf.SourceImageId != "" && !CheckResourceIdFormat("img", cf.SourceImageId) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package cvm
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
12+
)
13+
14+
type stepCheckSourceImageFamily struct {
15+
sourceImageId string
16+
sourceImageName string
17+
}
18+
19+
func (s *stepCheckSourceImageFamily) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
20+
if s.sourceImageId != "" || s.sourceImageName != "" {
21+
return multistep.ActionContinue
22+
}
23+
config := state.Get("config").(*Config)
24+
client := state.Get("cvm_client").(*cvm.Client)
25+
26+
Say(state, config.SourceImageFamily, "Try to check the source image and get the latest valid image of the image family")
27+
28+
req := cvm.NewDescribeImageFromFamilyRequest()
29+
req.ImageFamily = &config.SourceImageFamily
30+
31+
var resp *cvm.DescribeImageFromFamilyResponse
32+
err := Retry(ctx, func(ctx context.Context) error {
33+
var err error
34+
resp, err = client.DescribeImageFromFamily(req)
35+
return err
36+
})
37+
if err != nil {
38+
return Halt(state, err, "Failed to get source image info from the image family")
39+
}
40+
41+
if resp != nil && resp.Response != nil && resp.Response.Image != nil {
42+
image := resp.Response.Image
43+
if image.ImageId != nil && !*image.ImageDeprecated {
44+
state.Put("source_image", image)
45+
Message(state, fmt.Sprintf("Get the latest image from the image family, id: %v", *image.ImageId), "Image found")
46+
return multistep.ActionContinue
47+
}
48+
} else {
49+
return Halt(state, fmt.Errorf("failed to get source image: %v", resp.ToJsonString()), "No image family found")
50+
}
51+
52+
return Halt(state, fmt.Errorf("No image found under current instance_type(%s) restriction", config.InstanceType), "")
53+
}
54+
55+
func (s *stepCheckSourceImageFamily) Cleanup(bag multistep.StateBag) {}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
Default value is `false`.
55

66
- `source_image_id` (string) - The base image id of Image you want to create
7+
You can also specify `source_image_family`. If both `source_image` and `source_image_family` are specified, `source_image` takes precedence.
78
your customized image from.
89

910
- `source_image_name` (string) - The base image name of Image you want to create your
10-
customized image from.Conflict with SourceImageId.
11+
customized image from.Conflict with SourceImageId and SourceImageName.
12+
13+
- `source_image_family` (string) - The source image family to use to create the new image from.
14+
The image family always returns its latest image that is not deprecated.
15+
Conflict with SourceImageId and SourceImageName. It takes effect when SourceImageId and SourceImageName are empty.
16+
Example value: business-daily-update.
1117

1218
- `instance_charge_type` (string) - Charge type of cvm, values can be `POSTPAID_BY_HOUR` (default) `SPOTPAID`
1319

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,12 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1175 h1:w0z
314314
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1175/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
315315
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1200 h1:n6elge0PuoOtHt67BhlQka5Y7ChPbCtp23zYDFw56V0=
316316
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1200/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
317+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.31 h1:PKa4c2BLYbW5LUOWGNXt20+rV9L8JnLqBXZjnOXsHKQ=
318+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.31/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
317319
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1072 h1:BO6eEqw2CxeP73AnOfkq99mJAPccP+w3exmVuBDMvAc=
318320
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1072/go.mod h1:Ot4h9BuqIbyOPotq8cIT3vCCMDdn6LXkc37jU2teHnQ=
321+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.1.31 h1:M6v6WE88puzkxap8QgVSHM3u2Pe80E8uwps8U08FOOk=
322+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.1.31/go.mod h1:oMQNF1IsVtrOHdBONFRCWz0T5zqmxzM6JDBPl8ub6EM=
319323
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.1200 h1:hYOvCfgPKpH2OS6+6ZOT+h21CfduIbGfz7RE+Ey1H14=
320324
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.1200/go.mod h1:qYzdOsPWtOJ18uQ/4QupBTF98ariELEH4dx93GiBeuY=
321325
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.797 h1:Z9rTZBoR4arEXA9gYLu8AQnMInG1scb+WnlIWczLH2A=

0 commit comments

Comments
 (0)