diff --git a/.web-docs/components/builder/cvm/README.md b/.web-docs/components/builder/cvm/README.md index 227bbb61..cf79bb7f 100644 --- a/.web-docs/components/builder/cvm/README.md +++ b/.web-docs/components/builder/cvm/README.md @@ -127,16 +127,14 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can - LOCAL_BASIC: 50 - Other: 50 ~ 1000 (need whitelist if > 50) -- `data_disks` ([]tencentCloudDataDisk) - Add one or more data disks to the instance before creating the image. +- `data_disks` ([]TencentCloudDataDisk) - Add one or more data disks to the instance before creating the image. Note that if the source image has data disk snapshots, this argument will be ignored, and the running instance will use source image data disk settings, in such case, `disk_type` argument will be used as disk type for all data disks, and each data disk size will use the origin value in source image. - The data disks allow for the following argument: - - `disk_type` - Type of the data disk. Valid choices: `CLOUD_BASIC`, `CLOUD_PREMIUM` and `CLOUD_SSD`. - - `disk_size` - Size of the data disk. - - `disk_snapshot_id` - Id of the snapshot for a data disk. + +- `include_data_disks` (boolean) - Whether to include data disks in the resulting image. Defaults to true. - `vpc_id` (string) - Specify vpc your cvm will be launched by. diff --git a/builder/tencentcloud/cvm/builder.hcl2spec.go b/builder/tencentcloud/cvm/builder.hcl2spec.go index 51eadf70..c5fc5896 100644 --- a/builder/tencentcloud/cvm/builder.hcl2spec.go +++ b/builder/tencentcloud/cvm/builder.hcl2spec.go @@ -44,7 +44,8 @@ type FlatConfig struct { InstanceName *string `mapstructure:"instance_name" required:"false" cty:"instance_name" hcl:"instance_name"` DiskType *string `mapstructure:"disk_type" required:"false" cty:"disk_type" hcl:"disk_type"` DiskSize *int64 `mapstructure:"disk_size" required:"false" cty:"disk_size" hcl:"disk_size"` - DataDisks []FlattencentCloudDataDisk `mapstructure:"data_disks" cty:"data_disks" hcl:"data_disks"` + DataDisks []FlatTencentCloudDataDisk `mapstructure:"data_disks" cty:"data_disks" hcl:"data_disks"` + IncludeDataDisks *bool `mapstructure:"include_data_disks" required:"false" cty:"include_data_disks" hcl:"include_data_disks"` VpcId *string `mapstructure:"vpc_id" required:"false" cty:"vpc_id" hcl:"vpc_id"` VpcName *string `mapstructure:"vpc_name" required:"false" cty:"vpc_name" hcl:"vpc_name"` SubnetId *string `mapstructure:"subnet_id" required:"false" cty:"subnet_id" hcl:"subnet_id"` @@ -160,7 +161,8 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "instance_name": &hcldec.AttrSpec{Name: "instance_name", Type: cty.String, Required: false}, "disk_type": &hcldec.AttrSpec{Name: "disk_type", Type: cty.String, Required: false}, "disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.Number, Required: false}, - "data_disks": &hcldec.BlockListSpec{TypeName: "data_disks", Nested: hcldec.ObjectSpec((*FlattencentCloudDataDisk)(nil).HCL2Spec())}, + "data_disks": &hcldec.BlockListSpec{TypeName: "data_disks", Nested: hcldec.ObjectSpec((*FlatTencentCloudDataDisk)(nil).HCL2Spec())}, + "include_data_disks": &hcldec.AttrSpec{Name: "include_data_disks", Type: cty.Bool, Required: false}, "vpc_id": &hcldec.AttrSpec{Name: "vpc_id", Type: cty.String, Required: false}, "vpc_name": &hcldec.AttrSpec{Name: "vpc_name", Type: cty.String, Required: false}, "subnet_id": &hcldec.AttrSpec{Name: "subnet_id", Type: cty.String, Required: false}, diff --git a/builder/tencentcloud/cvm/run_config.go b/builder/tencentcloud/cvm/run_config.go index e4a45e11..86c2b89d 100644 --- a/builder/tencentcloud/cvm/run_config.go +++ b/builder/tencentcloud/cvm/run_config.go @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MPL-2.0 //go:generate packer-sdc struct-markdown -//go:generate packer-sdc mapstructure-to-hcl2 -type tencentCloudDataDisk +//go:generate packer-sdc mapstructure-to-hcl2 -type TencentCloudDataDisk package cvm @@ -18,9 +18,12 @@ import ( "github.com/pkg/errors" ) -type tencentCloudDataDisk struct { - DiskType string `mapstructure:"disk_type"` - DiskSize int64 `mapstructure:"disk_size"` +type TencentCloudDataDisk struct { + // The size of the data disk. + DiskSize int64 `mapstructure:"disk_size" required:"true"` + // The type of disk to use. See https://www.tencentcloud.com/document/api/213/15753#datadisk for valid values. + DiskType string `mapstructure:"disk_type"` + // The snapshot to use for the data disk. SnapshotId string `mapstructure:"disk_snapshot_id"` } @@ -56,11 +59,9 @@ type TencentCloudRunConfig struct { // disk settings, in such case, `disk_type` argument will be used as disk // type for all data disks, and each data disk size will use the origin // value in source image. - // The data disks allow for the following argument: - // - `disk_type` - Type of the data disk. Valid choices: `CLOUD_BASIC`, `CLOUD_PREMIUM` and `CLOUD_SSD`. - // - `disk_size` - Size of the data disk. - // - `disk_snapshot_id` - Id of the snapshot for a data disk. - DataDisks []tencentCloudDataDisk `mapstructure:"data_disks"` + DataDisks []TencentCloudDataDisk `mapstructure:"data_disks"` + // Whether to include data disks in the resulting image. Defaults to true. + IncludeDataDisks config.Trilean `mapstructure:"include_data_disks" required:"false"` // Specify vpc your cvm will be launched by. VpcId string `mapstructure:"vpc_id" required:"false"` // Specify vpc name you will create. if `vpc_id` is not set, Packer will @@ -114,6 +115,11 @@ var ValidCBSType = []string{ } func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error { + // Include data disks by default + if cf.IncludeDataDisks != config.TriFalse { + cf.IncludeDataDisks = config.TriTrue + } + packerId := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()[:8]) if cf.Comm.SSHKeyPairName == "" && cf.Comm.SSHTemporaryKeyPairName == "" && cf.Comm.SSHPrivateKeyFile == "" && cf.Comm.SSHPassword == "" && cf.Comm.WinRMPassword == "" { diff --git a/builder/tencentcloud/cvm/run_config.hcl2spec.go b/builder/tencentcloud/cvm/run_config.hcl2spec.go index 0d0dfe9f..ba40e9f6 100644 --- a/builder/tencentcloud/cvm/run_config.hcl2spec.go +++ b/builder/tencentcloud/cvm/run_config.hcl2spec.go @@ -7,28 +7,28 @@ import ( "github.com/zclconf/go-cty/cty" ) -// FlattencentCloudDataDisk is an auto-generated flat version of tencentCloudDataDisk. +// FlatTencentCloudDataDisk is an auto-generated flat version of TencentCloudDataDisk. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. -type FlattencentCloudDataDisk struct { +type FlatTencentCloudDataDisk struct { + DiskSize *int64 `mapstructure:"disk_size" required:"true" cty:"disk_size" hcl:"disk_size"` DiskType *string `mapstructure:"disk_type" cty:"disk_type" hcl:"disk_type"` - DiskSize *int64 `mapstructure:"disk_size" cty:"disk_size" hcl:"disk_size"` SnapshotId *string `mapstructure:"disk_snapshot_id" cty:"disk_snapshot_id" hcl:"disk_snapshot_id"` } -// FlatMapstructure returns a new FlattencentCloudDataDisk. -// FlattencentCloudDataDisk is an auto-generated flat version of tencentCloudDataDisk. +// FlatMapstructure returns a new FlatTencentCloudDataDisk. +// FlatTencentCloudDataDisk is an auto-generated flat version of TencentCloudDataDisk. // Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. -func (*tencentCloudDataDisk) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { - return new(FlattencentCloudDataDisk) +func (*TencentCloudDataDisk) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { + return new(FlatTencentCloudDataDisk) } -// HCL2Spec returns the hcl spec of a tencentCloudDataDisk. -// This spec is used by HCL to read the fields of tencentCloudDataDisk. -// The decoded values from this spec will then be applied to a FlattencentCloudDataDisk. -func (*FlattencentCloudDataDisk) HCL2Spec() map[string]hcldec.Spec { +// HCL2Spec returns the hcl spec of a TencentCloudDataDisk. +// This spec is used by HCL to read the fields of TencentCloudDataDisk. +// The decoded values from this spec will then be applied to a FlatTencentCloudDataDisk. +func (*FlatTencentCloudDataDisk) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ - "disk_type": &hcldec.AttrSpec{Name: "disk_type", Type: cty.String, Required: false}, "disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.Number, Required: false}, + "disk_type": &hcldec.AttrSpec{Name: "disk_type", Type: cty.String, Required: false}, "disk_snapshot_id": &hcldec.AttrSpec{Name: "disk_snapshot_id", Type: cty.String, Required: false}, } return s diff --git a/builder/tencentcloud/cvm/step_create_image.go b/builder/tencentcloud/cvm/step_create_image.go index b337fbeb..52c2a3ea 100644 --- a/builder/tencentcloud/cvm/step_create_image.go +++ b/builder/tencentcloud/cvm/step_create_image.go @@ -6,7 +6,6 @@ package cvm import ( "context" "fmt" - "github.com/hashicorp/packer-plugin-sdk/multistep" cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" ) @@ -28,12 +27,17 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul req.ImageDescription = &config.ImageDescription req.InstanceId = instance.InstanceId - // TODO: We should allow user to specify which data disk should be - // included into created image. var dataDiskIds []*string - for _, disk := range instance.DataDisks { - dataDiskIds = append(dataDiskIds, disk.DiskId) + // There is no way to correlate instance disk IDs to our own data disk definitions, + // so the best we can do is to either include all disks or include none. + if config.IncludeDataDisks.True() { + for _, disk := range instance.DataDisks { + dataDiskIds = append(dataDiskIds, disk.DiskId) + } + } else { + Message(state, "Not including configured data disks in the resulting image", "") } + if len(dataDiskIds) > 0 { req.DataDiskIds = dataDiskIds } diff --git a/builder/tencentcloud/cvm/step_run_instance.go b/builder/tencentcloud/cvm/step_run_instance.go index 943eabf6..3113b561 100644 --- a/builder/tencentcloud/cvm/step_run_instance.go +++ b/builder/tencentcloud/cvm/step_run_instance.go @@ -31,7 +31,7 @@ type stepRunInstance struct { CamRoleName string AssociatePublicIpAddress bool Tags map[string]string - DataDisks []tencentCloudDataDisk + DataDisks []TencentCloudDataDisk } func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { diff --git a/docs-partials/builder/tencentcloud/cvm/TencentCloudDataDisk-required.mdx b/docs-partials/builder/tencentcloud/cvm/TencentCloudDataDisk-required.mdx new file mode 100644 index 00000000..d8b49809 --- /dev/null +++ b/docs-partials/builder/tencentcloud/cvm/TencentCloudDataDisk-required.mdx @@ -0,0 +1,5 @@ + + +- `disk_size` (int64) - The size of the data disk. + + diff --git a/docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx b/docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx index 7d1fabb9..1ece68ef 100644 --- a/docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx +++ b/docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx @@ -21,16 +21,14 @@ - LOCAL_BASIC: 50 - Other: 50 ~ 1000 (need whitelist if > 50) -- `data_disks` ([]tencentCloudDataDisk) - Add one or more data disks to the instance before creating the image. +- `data_disks` ([]TencentCloudDataDisk) - Add one or more data disks to the instance before creating the image. Note that if the source image has data disk snapshots, this argument will be ignored, and the running instance will use source image data disk settings, in such case, `disk_type` argument will be used as disk type for all data disks, and each data disk size will use the origin value in source image. - The data disks allow for the following argument: - - `disk_type` - Type of the data disk. Valid choices: `CLOUD_BASIC`, `CLOUD_PREMIUM` and `CLOUD_SSD`. - - `disk_size` - Size of the data disk. - - `disk_snapshot_id` - Id of the snapshot for a data disk. + +- `include_data_disks` (boolean) - Whether to include data disks in the resulting image. Defaults to true. - `vpc_id` (string) - Specify vpc your cvm will be launched by. diff --git a/docs-partials/builder/tencentcloud/cvm/tencentCloudDataDisk-not-required.mdx b/docs-partials/builder/tencentcloud/cvm/tencentCloudDataDisk-not-required.mdx index 8cdd0545..6fc6e197 100644 --- a/docs-partials/builder/tencentcloud/cvm/tencentCloudDataDisk-not-required.mdx +++ b/docs-partials/builder/tencentcloud/cvm/tencentCloudDataDisk-not-required.mdx @@ -1,9 +1,7 @@ - + -- `disk_type` (string) - Disk Type +- `disk_type` (string) - The type of disk to use. See https://www.tencentcloud.com/document/api/213/15753#datadisk for valid values. -- `disk_size` (int64) - Disk Size +- `disk_snapshot_id` (string) - The snapshot to use for the data disk. -- `disk_snapshot_id` (string) - Snapshot Id - - +