Skip to content

Commit a5919e4

Browse files
committed
fix: Support SkipCreateImage field to skip creating an image
1 parent 9d7f28f commit a5919e4

File tree

8 files changed

+45
-11
lines changed

8 files changed

+45
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
101101

102102
- `image_tags` (map[string]string) - Key/value pair tags that will be applied to the resulting image.
103103

104+
- `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.
105+
104106
<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->
105107

106108

builder/tencentcloud/cvm/builder.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
9090
// Build the steps
9191
var steps []multistep.Step
9292
steps = []multistep.Step{
93-
&stepPreValidate{},
93+
&stepPreValidate{
94+
b.config.SkipCreateImage,
95+
},
9496
&stepCheckSourceImage{
9597
b.config.SourceImageId,
9698
},
@@ -145,13 +147,16 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
145147
// We need this step to detach keypair from instance, otherwise
146148
// it always fails to delete the key.
147149
&stepDetachTempKeyPair{},
148-
&stepCreateImage{},
150+
&stepCreateImage{
151+
SkipCreateImage: b.config.SkipCreateImage,
152+
},
149153
&stepShareImage{
150154
b.config.ImageShareAccounts,
151155
},
152156
&stepCopyImage{
153-
DesinationRegions: b.config.ImageCopyRegions,
154-
SourceRegion: b.config.Region,
157+
DestinationRegions: b.config.ImageCopyRegions,
158+
SourceRegion: b.config.Region,
159+
SkipCreateImage: b.config.SkipCreateImage,
155160
},
156161
}
157162

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/image_config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ type TencentCloudImageConfig struct {
3333
// Key/value pair tags that will be applied to the resulting image.
3434
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
3535
skipValidation bool
36+
// 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.
37+
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
3638
}
3739

3840
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
3941
var errs []error
4042

43+
if cf.SkipCreateImage {
44+
return nil
45+
}
46+
4147
if cf.ImageName == "" {
4248
errs = append(errs, fmt.Errorf("image_name must be specified"))
4349
} else if utf8.RuneCountInString(cf.ImageName) > 60 {

builder/tencentcloud/cvm/step_copy_image.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ import (
1414
)
1515

1616
type stepCopyImage struct {
17-
DesinationRegions []string
18-
SourceRegion string
17+
DestinationRegions []string
18+
SourceRegion string
19+
SkipCreateImage bool
1920
}
2021

2122
func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
22-
if len(s.DesinationRegions) == 0 || (len(s.DesinationRegions) == 1 && s.DesinationRegions[0] == s.SourceRegion) {
23+
if s.SkipCreateImage {
24+
return multistep.ActionContinue
25+
}
26+
if len(s.DestinationRegions) == 0 || (len(s.DestinationRegions) == 1 && s.DestinationRegions[0] == s.SourceRegion) {
2327
return multistep.ActionContinue
2428
}
2529

@@ -28,12 +32,12 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
2832

2933
imageId := state.Get("image").(*cvm.Image).ImageId
3034

31-
Say(state, strings.Join(s.DesinationRegions, ","), "Trying to copy image to")
35+
Say(state, strings.Join(s.DestinationRegions, ","), "Trying to copy image to")
3236

3337
req := cvm.NewSyncImagesRequest()
3438
req.ImageIds = []*string{imageId}
35-
copyRegions := make([]*string, 0, len(s.DesinationRegions))
36-
for _, region := range s.DesinationRegions {
39+
copyRegions := make([]*string, 0, len(s.DestinationRegions))
40+
for _, region := range s.DestinationRegions {
3741
if region != s.SourceRegion {
3842
copyRegions = append(copyRegions, common.StringPtr(region))
3943
}

builder/tencentcloud/cvm/step_create_image.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
type stepCreateImage struct {
15-
imageId string
15+
imageId string
16+
SkipCreateImage bool
1617
}
1718

1819
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@@ -21,6 +22,12 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
2122
config := state.Get("config").(*Config)
2223
instance := state.Get("instance").(*cvm.Instance)
2324

25+
// Optionally skip this step
26+
if s.SkipCreateImage {
27+
Say(state, "Skipping image creation step", "")
28+
return multistep.ActionContinue
29+
}
30+
2431
Say(state, config.ImageName, "Trying to create a new image")
2532

2633
req := cvm.NewCreateImageRequest()

builder/tencentcloud/cvm/step_pre_validate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ import (
1212
)
1313

1414
type stepPreValidate struct {
15+
SkipCreateImage bool
1516
}
1617

1718
func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
19+
// No need to validate the image name if we're not creating an image
20+
if s.SkipCreateImage {
21+
return multistep.ActionContinue
22+
}
23+
1824
config := state.Get("config").(*Config)
1925
client := state.Get("cvm_client").(*cvm.Client)
2026

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

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

1616
- `image_tags` (map[string]string) - Key/value pair tags that will be applied to the resulting image.
1717

18+
- `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.
19+
1820
<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->

0 commit comments

Comments
 (0)