8000 fix: Support SkipCreateImage field to skip creating an image by gitmkn · Pull Request #11 · tencentcloudstack/packer-plugin-tencentcloud · GitHub
[go: up one dir, main page]

Skip to content

fix: Support SkipCreateImage field to skip creating an image #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .web-docs/components/builder/cvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can

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

- `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.

<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->


Expand Down
13 changes: 9 additions & 4 deletions builder/tencentcloud/cvm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
// Build the steps
var steps []multistep.Step
steps = []multistep.Step{
&stepPreValidate{},
&stepPreValidate{
b.config.SkipCreateImage,
},
&stepCheckSourceImage{
b.config.SourceImageId,
},
Expand Down Expand Up @@ -145,13 +147,16 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
// We need this step to detach keypair from instance, otherwise
// it always fails to delete th 8000 e key.
&stepDetachTempKeyPair{},
&stepCreateImage{},
&stepCreateImage{
SkipCreateImage: b.config.SkipCreateImage,
},
&stepShareImage{
b.config.ImageShareAccounts,
},
&stepCopyImage{
DesinationRegions: b.config.ImageCopyRegions,
SourceRegion: b.config.Region,
DestinationRegions: b.config.ImageCopyRegions,
SourceRegion: b.config.Region,
SkipCreateImage: b.config.SkipCreateImage,
},
}

Expand Down
2 changes: 2 additions & 0 deletions builder/tencentcloud/cvm/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions builder/tencentcloud/cvm/image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ type TencentCloudImageConfig struct {
// Key/value pair tags that will be applied to the resulting image.
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
skipValidation 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.
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
}

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

if cf.SkipCreateImage {
return nil
}

if cf.ImageName == "" {
errs = append(errs, fmt.Errorf("image_name must be specified"))
} else if utf8.RuneCountInString(cf.ImageName) > 60 {
Expand Down
16 changes: 10 additions & 6 deletions builder/tencentcloud/cvm/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
)

type stepCopyImage struct {
DesinationRegions []string
SourceRegion string
DestinationRegions []string
SourceRegion string
SkipCreateImage bool
}

func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
if len(s.DesinationRegions) == 0 || (len(s.DesinationRegions) == 1 && s.DesinationRegions[0] == s.SourceRegion) {
if s.SkipCreateImage {
return multistep.ActionContinue
}
if len(s.DestinationRegions) == 0 || (len(s.DestinationRegions) == 1 && s.DestinationRegions[0] == s.SourceRegion) {
return multistep.ActionContinue
}

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

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

Say(state, strings.Join(s.DesinationRegions, ","), "Trying to copy image to")
Say(state, strings.Join(s.DestinationRegions, ","), "Trying to copy image to")

req := cvm.NewSyncImagesRequest()
req.ImageIds = []*string{imageId}
copyRegions := make([]*string, 0, len(s.DesinationRegions))
for _, region := range s.DesinationRegions {
copyRegions := make([]*string, 0, len(s.DestinationRegions))
for _, region := range s.DestinationRegions {
if region != s.SourceRegion {
copyRegions = append(copyRegions, common.StringPtr(region))
}
Expand Down
9 changes: 8 additions & 1 deletion builder/tencentcloud/cvm/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type stepCreateImage struct {
imageId string
imageId string
SkipCreateImage bool
}

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

// Optionally skip this step
if s.SkipCreateImage {
Say(state, "Skipping image creation step", "")
return multistep.ActionContinue
}

Say(state, config.ImageName, "Trying to create a new image")

req := cvm.NewCreateImageRequest()
Expand Down
6 changes: 6 additions & 0 deletions builder/tencentcloud/cvm/step_pre_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import (
)

type stepPreValidate struct {
SkipCreateImage bool
}

func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
// No need to validate the image name if we're not creating an image
if s.SkipCreateImage {
return multistep.ActionContinue
}

config := state.Get("config").(*Config)
client := state.Get("cvm_client").(*cvm.Client)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

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

- `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.

<!-- End of code generated from the comments of the TencentCloudImageConfig struct in builder/tencentcloud/cvm/image_config.go; -->
Loading
0