Skip to content

Commit a7c5451

Browse files
cvm: fix tag/field SkipImageValidation conflicts (#97)
* cvm: fix tag/field SkipImageValidation conflicts The `SkipImageValidation' field and its tag, `skip_region_validation`, was duplicated in two structures, `TencentCloudImageConfig`, and `TencentCloudAccessConfig`. This resulted in unexpected behaviour, as setting the attribute in a template will set the config for both substructures for JSON templates, while on HCL templates, only one would be settable. To fix this discrepancy, we mutualise the field as a config argument for the global configuration object, and propagate its value to the appropriate configuration structures. This way, region validation can be skipped for both the AccessConfig, and the ImageConfig, in all cases. * Bump github.com/hashicorp/packer-plugin-sdk@v0.4.0 --------- Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>
1 parent 616f5d3 commit a7c5451

10 files changed

Lines changed: 99 additions & 469 deletions

File tree

builder/tencentcloud/cvm/access_config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ type TencentCloudAccessConfig struct {
6565
// reference Region and Zone
6666
// for parameter taking.
6767
Zone string `mapstructure:"zone" required:"true"`
68-
// Do not check region and zone when validate.
69-
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
7068
// The endpoint you want to reach the cloud endpoint,
7169
// if tce cloud you should set a tce cvm endpoint.
7270
CvmEndpoint string `mapstructure:"cvm_endpoint" required:"false"`
7371
// The endpoint you want to reach the cloud endpoint,
7472
// if tce cloud you should set a tce vpc endpoint.
75-
VpcEndpoint string `mapstructure:"vpc_endpoint" required:"false"`
73+
VpcEndpoint string `mapstructure:"vpc_endpoint" required:"false"`
74+
skipValidation bool
7675
}
7776

7877
func (cf *TencentCloudAccessConfig) Client() (*cvm.Client, *vpc.Client, error) {
@@ -132,7 +131,7 @@ func (cf *TencentCloudAccessConfig) Prepare(ctx *interpolate.Context) []error {
132131

133132
if cf.Region == "" {
134133
errs = append(errs, fmt.Errorf("parameter region must be set"))
135-
} else if !cf.SkipValidation {
134+
} else if !cf.skipValidation {
136135
if err := cf.validateRegion(); err != nil {
137136
errs = append(errs, err)
138137
}

builder/tencentcloud/cvm/access_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestTencentCloudAccessConfig_Prepare(t *testing.T) {
2727
t.Fatal("should raise error: unknown region")
2828
}
2929

30-
cf.SkipValidation = true
30+
cf.skipValidation = true
3131
if err := cf.Prepare(nil); err != nil {
3232
t.Fatalf("shouldn't raise error: %v", err)
3333
}

builder/tencentcloud/cvm/builder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type Config struct {
2727
TencentCloudImageConfig `mapstructure:",squash"`
2828
TencentCloudRunConfig `mapstructure:",squash"`
2929

30+
// Do not check region and zone when validate.
31+
SkipRegionValidation bool `mapstructure:"skip_region_validation" required:"false"`
32+
3033
ctx interpolate.Context
3134
}
3235

@@ -53,6 +56,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
5356
return nil, nil, err
5457
}
5558

59+
// Propagate SkipRegionValidation to Access/Image configs
60+
b.config.TencentCloudAccessConfig.skipValidation = b.config.SkipRegionValidation
61+
b.config.TencentCloudImageConfig.skipValidation = b.config.SkipRegionValidation
62+
5663
// Accumulate any errors
5764
var errs *packersdk.MultiError
5865
errs = packersdk.MultiErrorAppend(errs, b.config.TencentCloudAccessConfig.Prepare(&b.config.ctx)...)

builder/tencentcloud/cvm/builder.hcl2spec.go

Lines changed: 2 additions & 2 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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ type TencentCloudImageConfig struct {
3434
// accounts that will be shared to
3535
// after your image created.
3636
ImageShareAccounts []string `mapstructure:"image_share_accounts" required:"false"`
37-
// Do not check region and zone when validate.
38-
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
3937
// Key/value pair tags that will be applied to the resulting image.
40-
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
38+
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
39+
skipValidation bool
4140
}
4241

4342
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
@@ -65,7 +64,7 @@ func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
6564

6665
regionSet[region] = struct{}{}
6766

68-
if !cf.SkipValidation {
67+
if !cf.skipValidation {
6968
if err := validRegion(region); err != nil {
7069
errs = append(errs, err)
7170
continue

builder/tencentcloud/cvm/image_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestTencentCloudImageConfig_Prepare(t *testing.T) {
3030
t.Fatal("should have err")
3131
}
3232

33-
cf.SkipValidation = true
33+
cf.skipValidation = true
3434
if err := cf.Prepare(nil); err != nil {
3535
t.Fatalf("shouldn't have err:%v", err)
3636
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<!-- Code generated from the comments of the TencentCloudAccessConfig struct in builder/tencentcloud/cvm/access_config.go; DO NOT EDIT MANUALLY -->
22

3-
- `skip_region_validation` (bool) - Do not check region and zone when validate.
4-
53
- `cvm_endpoint` (string) - The endpoint you want to reach the cloud endpoint,
64
if tce cloud you should set a tce cvm endpoint.
75

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
- `image_share_accounts` ([]string) - accounts that will be shared to
1919
after your image created.
2020

21-
- `skip_region_validation` (bool) - Do not check region and zone when validate.
22-
2321
- `image_tags` (map[string]string) - Key/value pair tags that will be applied to the resulting image.
2422

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

go.mod

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/hashicorp/hcl/v2 v2.13.0
7-
github.com/hashicorp/packer-plugin-sdk v0.3.4
7+
github.com/hashicorp/packer-plugin-sdk v0.4.0
88
github.com/pkg/errors v0.9.1
99
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.367
1010
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.366
@@ -13,14 +13,17 @@ require (
1313
)
1414

1515
require (
16-
cloud.google.com/go v0.94.0 // indirect
17-
cloud.google.com/go/storage v1.16.1 // indirect
16+
cloud.google.com/go v0.105.0 // indirect
17+
cloud.google.com/go/compute v1.12.1 // indirect
18+
cloud.google.com/go/compute/metadata v0.1.1 // indirect
19+
cloud.google.com/go/iam v0.6.0 // indirect
20+
cloud.google.com/go/storage v1.27.0 // indirect
1821
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect
1922
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
2023
github.com/agext/levenshtein v1.2.3 // indirect
2124
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
2225
github.com/armon/go-metrics v0.3.9 // indirect
23-
github.com/aws/aws-sdk-go v1.40.34 // indirect
26+
github.com/aws/aws-sdk-go v1.44.114 // indirect
2427
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
2528
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
2629
github.com/dylanmei/iso8601 v0.1.0 // indirect
@@ -30,23 +33,24 @@ require (
3033
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3134
github.com/golang/protobuf v1.5.2 // indirect
3235
github.com/golang/snappy v0.0.4 // indirect
33-
github.com/google/go-cmp v0.5.6 // indirect
36+
github.com/google/go-cmp v0.5.9 // indirect
3437
github.com/google/uuid v1.3.0 // indirect
35-
github.com/googleapis/gax-go/v2 v2.1.0 // indirect
38+
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
39+
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
3640
github.com/hashicorp/consul/api v1.10.1 // indirect
3741
github.com/hashicorp/errwrap v1.1.0 // indirect
3842
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
39-
github.com/hashicorp/go-getter/gcs/v2 v2.1.0 // indirect
40-
github.com/hashicorp/go-getter/s3/v2 v2.1.0 // indirect
41-
github.com/hashicorp/go-getter/v2 v2.1.0 // indirect
43+
github.com/hashicorp/go-getter/gcs/v2 v2.2.0 // indirect
44+
github.com/hashicorp/go-getter/s3/v2 v2.2.0 // indirect
45+
github.com/hashicorp/go-getter/v2 v2.2.0 // indirect
4246
github.com/hashicorp/go-hclog v0.16.2 // indirect
4347
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
4448
github.com/hashicorp/go-multierror v1.1.1 // indirect
4549
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
4650
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
4751
github.com/hashicorp/go-safetemp v1.0.0 // indirect
4852
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
49-
github.com/hashicorp/go-version v1.3.0 // indirect
53+
github.com/hashicorp/go-version v1.6.0 // indirect
5054
github.com/hashicorp/golang-lru v0.5.4 // indirect
5155
github.com/hashicorp/hcl v1.0.0 // indirect
5256
github.com/hashicorp/serf v0.9.5 // indirect
@@ -55,6 +59,7 @@ require (
5559
github.com/hashicorp/yamux v0.0.0-20210826001029-26ff87cf9493 // indirect
5660
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
5761
github.com/jmespath/go-jmespath v0.4.0 // indirect
62+
github.com/klauspost/compress v1.11.2 // indirect
5863
github.com/kr/fs v0.1.0 // indirect
5964
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
6065
github.com/masterzen/winrm v0.0.0-20210623064412-3b76017826b0 // indirect
@@ -76,17 +81,18 @@ require (
7681
github.com/ulikunitz/xz v0.5.10 // indirect
7782
go.opencensus.io v0.23.0 // indirect
7883
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
79-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
80-
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
81-
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
82-
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
83-
golang.org/x/text v0.3.7 // indirect
84+
golang.org/x/net v0.8.0 // indirect
85+
golang.org/x/oauth2 v0.1.0 // indirect
86+
golang.org/x/sys v0.6.0 // indirect
87+
golang.org/x/term v0.6.0 // indirect
88+
golang.org/x/text v0.8.0 // indirect
8489
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
85-
google.golang.org/api v0.56.0 // indirect
90+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
91+
google.golang.org/api v0.101.0 // indirect
8692
google.golang.org/appengine v1.6.7 // indirect
87-
google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 // indirect
88-
google.golang.org/grpc v1.40.0 // indirect
89-
google.golang.org/protobuf v1.27.1 // indirect
93+
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
94+
google.golang.org/grpc v1.50.1 // indirect
95+
google.golang.org/protobuf v1.28.1 // indirect
9096
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
9197
gopkg.in/yaml.v2 v2.3.0 // indirect
9298
)

0 commit comments

Comments
 (0)