Skip to content

fix(cvm): [120953572]support cbs_filter and cbs_configs #3118

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

Merged
merged 4 commits into from
Feb 14, 2025
Merged
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
3 changes: 3 additions & 0 deletions .changelog/3118.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
datasource/tencentcloud_instance_types: support `cbs_filter` and `cbs_configs`
```
36 changes: 36 additions & 0 deletions tencentcloud/services/cbs/service_tencentcloud_cbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,3 +913,39 @@ func (me *CbsService) ApplyDiskBackup(ctx context.Context, diskBackupId, diskId
}
return
}

func (me *CbsService) DescribeDiskConfigQuota(ctx context.Context, cvmInfo map[string]interface{}) (diskConfigSet []*cbs.DiskConfig, errRet error) {
logId := tccommon.GetLogId(ctx)
request := cbs.NewDescribeDiskConfigQuotaRequest()
defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

request.InquiryType = helper.String("INQUIRY_CVM_CONFIG")
request.Zones = helper.Strings([]string{cvmInfo["availability_zone"].(string)})
request.CPU = helper.Int64Uint64(cvmInfo["cpu_core_count"].(int64))
request.Memory = helper.Int64Uint64(cvmInfo["memory_size"].(int64))
request.InstanceFamilies = helper.Strings([]string{cvmInfo["family"].(string)})
request.DiskTypes = helper.Strings(cvmInfo["disk_types"].([]string))
request.DiskChargeType = helper.String(cvmInfo["disk_charge_type"].(string))
request.DiskUsage = helper.String(cvmInfo["disk_usage"].(string))
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
result, e := me.client.UseCbsClient().DescribeDiskConfigQuota(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
diskConfigSet = result.Response.DiskConfigSet
return nil
})
if err != nil {
errRet = err
log.Printf("[CRITAL]%s create cbs DiskBackup failed, reason:%+v", logId, err)
return
}
return
}
167 changes: 167 additions & 0 deletions tencentcloud/services/cvm/data_source_tc_instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
svccbs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cbs"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
Expand Down Expand Up @@ -61,6 +62,40 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
},
},
},
"cbs_filter": {
Type: schema.TypeList,
Optional: true,
Description: "Cbs filter.",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_types": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Hard disk media type. Value range:\n" +
" - CLOUD_BASIC: Represents ordinary Cloud Block Storage;\n" +
" - CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;\n" +
" - CLOUD_SSD: Represents SSD Cloud Block Storage;\n" +
" - CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.",
},
"disk_charge_type": {
Type: schema.TypeString,
Optional: true,
Description: "Payment model. Value range:\n" +
" - PREPAID: Prepaid;\n" +
" - POSTPAID_BY_HOUR: Post-payment.",
},
"disk_usage": {
Type: schema.TypeString,
Optional: true,
Description: "System disk or data disk. Value range:\n" +
" - SYSTEM_DISK: Represents the system disk;\n" +
" - DATA_DISK: Represents the data disk.",
},
},
},
},
"exclude_sold_out": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -120,6 +155,79 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
Computed: true,
Description: "Sell status of the instance.",
},
"cbs_configs": {
Type: schema.TypeList,
Computed: true,
Description: "CBS config. The cbs_configs is populated when the cbs_filter is added.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"available": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the configuration is available.",
},
"disk_charge_type": {
Type: schema.TypeString,
Computed: true,
Description: "Payment model. Value range:\n" +
" - PREPAID: Prepaid;\n" +
" - POSTPAID_BY_HOUR: Post-payment.",
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "The availability zone to which the Cloud Block Storage belongs.",
},
"instance_family": {
Type: schema.TypeString,
Computed: true,
Description: "Instance family.",
},
"disk_type": {
Type: schema.TypeString,
Computed: true,
Description: "Hard disk media type. Value range:\n" +
" - CLOUD_BASIC: Represents ordinary Cloud Block Storage;\n" +
" - CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;\n" +
" - CLOUD_SSD: Represents SSD Cloud Block Storage;\n" +
" - CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.",
},
"step_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum step size change in cloud disk size, in GB.",
},
"extra_performance_range": {
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Description: "Extra performance range.",
},
"device_class": {
Type: schema.TypeString,
Computed: true,
Description: "Device class.",
},
"disk_usage": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud disk type. Value range:\n" +
" - SYSTEM_DISK: Represents the system disk;\n" +
" - DATA_DISK: Represents the data disk.",
},
"min_disk_size": {
Type: schema.TypeInt,
Computed: true,
Description: "The minimum configurable cloud disk size, in GB.",
},
"max_disk_size": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum configurable cloud disk size, in GB.",
},
},
},
},
},
},
},
Expand Down Expand Up @@ -207,6 +315,65 @@ func dataSourceTencentCloudInstanceTypesRead(d *schema.ResourceData, meta interf
}
}

client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
cbsService := svccbs.NewCbsService(client)
cbsFilterParams := make(map[string]interface{})
var hasCbsFilter bool
if dMap, ok := helper.InterfacesHeadMap(d, "cbs_filter"); ok {
if v, ok := dMap["disk_types"].([]interface{}); ok && len(v) > 0 {
cbsFilterParams["disk_types"] = helper.InterfacesStrings(v)
}
if v, ok := dMap["disk_charge_type"].(string); ok && v != "" {
cbsFilterParams["disk_charge_type"] = v
}
if v, ok := dMap["disk_usage"].(string); ok && v != "" {
cbsFilterParams["disk_usage"] = v
}
hasCbsFilter = true
}
if hasCbsFilter {
for idx, t := range typeList {
filterParams := make(map[string]interface{})
for k, v := range cbsFilterParams {
filterParams[k] = v
}

if v, ok := t["availability_zone"].(*string); ok && v != nil {
filterParams["availability_zone"] = *v
}
if v, ok := t["cpu_core_count"].(*int64); ok && v != nil {
filterParams["cpu_core_count"] = *v
}
if v, ok := t["memory_size"].(*int64); ok && v != nil {
filterParams["memory_size"] = *v
}
if v, ok := t["family"].(*string); ok && v != nil {
filterParams["family"] = *v
}
diskConfigSet, err := cbsService.DescribeDiskConfigQuota(ctx, filterParams)
if err != nil {
return err
}
cbsConfigList := make([]interface{}, 0)
for _, diskConfig := range diskConfigSet {
cbsConfigList = append(cbsConfigList, map[string]interface{}{
"available": diskConfig.Available,
"disk_charge_type": diskConfig.DiskChargeType,
"zone": diskConfig.Zone,
"instance_family": diskConfig.InstanceFamily,
"disk_type": diskConfig.DiskType,
"step_size": diskConfig.StepSize,
"extra_performance_range": diskConfig.ExtraPerformanceRange,
"device_class": diskConfig.DeviceClass,
"disk_usage": diskConfig.DiskUsage,
"min_disk_size": diskConfig.MinDiskSize,
"max_disk_size": diskConfig.MaxDiskSize,
})
}
typeList[idx]["cbs_configs"] = cbsConfigList
}
}

d.SetId(helper.DataResourceIdsHash(ids))
err = d.Set("instance_types", typeList)
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions tencentcloud/services/cvm/data_source_tc_instance_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ func TestAccTencentCloudCvmInstanceTypesDataSource_Sell(t *testing.T) {
},
})
}
func TestAccTencentCloudCvmInstanceTypesDataSource_WithCbsFilter(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.AccPreCheck(t)
},
Providers: acctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccCvmInstanceTypesDataSource_WithCbsFilter,
Check: resource.ComposeTestCheckFunc(
acctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_instance_types.with_cbs_filter"),
resource.TestCheckResourceAttrSet("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.#"),
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_type", "CLOUD_SSD"),
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_charge_type", "PREPAID"),
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_usage", "SYSTEM_DISK"),
),
},
},
})
}

const testAccCvmInstanceTypesDataSource_SellCreate = `

Expand All @@ -69,3 +90,26 @@ data "tencentcloud_instance_types" "example" {
}

`

const testAccCvmInstanceTypesDataSource_WithCbsFilter = `

data "tencentcloud_instance_types" "with_cbs_filter" {
cpu_core_count = 2
memory_size = 2
exclude_sold_out = true

filter {
name = "instance-family"
values = ["S6"]
}
filter {
name = "zone"
values = ["ap-guangzhou-6"]
}
cbs_filter {
disk_types = ["CLOUD_SSD"]
disk_charge_type = "PREPAID"
disk_usage = "SYSTEM_DISK"
}
}
`
35 changes: 35 additions & 0 deletions website/docs/d/instance_types.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,28 @@ data "tencentcloud_instance_types" "example" {
The following arguments are supported:

* `availability_zone` - (Optional, String) The available zone that the CVM instance locates at. This field is conflict with `filter`.
* `cbs_filter` - (Optional, List) Cbs filter.
* `cpu_core_count` - (Optional, Int) The number of CPU cores of the instance.
* `exclude_sold_out` - (Optional, Bool) Indicate to filter instances types that is sold out or not, default is false.
* `filter` - (Optional, Set) One or more name/value pairs to filter. This field is conflict with `availability_zone`.
* `gpu_core_count` - (Optional, Int) The number of GPU cores of the instance.
* `memory_size` - (Optional, Int) Instance memory capacity, unit in GB.
* `result_output_file` - (Optional, String) Used to save results.

The `cbs_filter` object supports the following:

* `disk_charge_type` - (Optional, String) Payment model. Value range:
- PREPAID: Prepaid;
- POSTPAID_BY_HOUR: Post-payment.
* `disk_types` - (Optional, List) Hard disk media type. Value range:
- CLOUD_BASIC: Represents ordinary Cloud Block Storage;
- CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;
- CLOUD_SSD: Represents SSD Cloud Block Storage;
- CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.
* `disk_usage` - (Optional, String) System disk or data disk. Value range:
- SYSTEM_DISK: Represents the system disk;
- DATA_DISK: Represents the data disk.

The `filter` object supports the following:

* `name` - (Required, String) The filter name. Valid values: `zone`, `instance-family` and `instance-charge-type`.
Expand All @@ -64,6 +79,26 @@ In addition to all arguments above, the following attributes are exported:

* `instance_types` - An information list of cvm instance. Each element contains the following attributes:
* `availability_zone` - The available zone that the CVM instance locates at.
* `cbs_configs` - CBS config. The cbs_configs is populated when the cbs_filter is added.
* `available` - Whether the configuration is available.
* `device_class` - Device class.
* `disk_charge_type` - Payment model. Value range:
- PREPAID: Prepaid;
- POSTPAID_BY_HOUR: Post-payment.
* `disk_type` - Hard disk media type. Value range:
- CLOUD_BASIC: Represents ordinary Cloud Block Storage;
- CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;
- CLOUD_SSD: Represents SSD Cloud Block Storage;
- CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.
* `disk_usage` - Cloud disk type. Value range:
- SYSTEM_DISK: Represents the system disk;
- DATA_DISK: Represents the data disk.
* `extra_performance_range` - Extra performance range.
* `instance_family` - Instance family.
* `max_disk_size` - The maximum configurable cloud disk size, in GB.
* `min_disk_size` - The minimum configurable cloud disk size, in GB.
* `step_size` - Minimum step size change in cloud disk size, in GB.
* `zone` - The availability zone to which the Cloud Block Storage belongs.
* `cpu_core_count` - The number of CPU cores of the instance.
* `family` - Type series of the instance.
* `gpu_core_count` - The number of GPU cores of the instance.
Expand Down
Loading