Skip to content

Commit 156134a

Browse files
tongyimingmikatong
and
mikatong
authored
fix(cvm): [120953572]support cbs_filter and cbs_configs (#3118)
* support cbs_filter and cbs_configs * add changelog * add changelog * update doc --------- Co-authored-by: mikatong <[email protected]>
1 parent 8d1e50a commit 156134a

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

.changelog/3118.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
datasource/tencentcloud_instance_types: support `cbs_filter` and `cbs_configs`
3+
```

tencentcloud/services/cbs/service_tencentcloud_cbs.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,3 +913,39 @@ func (me *CbsService) ApplyDiskBackup(ctx context.Context, diskBackupId, diskId
913913
}
914914
return
915915
}
916+
917+
func (me *CbsService) DescribeDiskConfigQuota(ctx context.Context, cvmInfo map[string]interface{}) (diskConfigSet []*cbs.DiskConfig, errRet error) {
918+
logId := tccommon.GetLogId(ctx)
919+
request := cbs.NewDescribeDiskConfigQuotaRequest()
920+
defer func() {
921+
if errRet != nil {
922+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
923+
}
924+
}()
925+
926+
request.InquiryType = helper.String("INQUIRY_CVM_CONFIG")
927+
request.Zones = helper.Strings([]string{cvmInfo["availability_zone"].(string)})
928+
request.CPU = helper.Int64Uint64(cvmInfo["cpu_core_count"].(int64))
929+
request.Memory = helper.Int64Uint64(cvmInfo["memory_size"].(int64))
930+
request.InstanceFamilies = helper.Strings([]string{cvmInfo["family"].(string)})
931+
request.DiskTypes = helper.Strings(cvmInfo["disk_types"].([]string))
932+
request.DiskChargeType = helper.String(cvmInfo["disk_charge_type"].(string))
933+
request.DiskUsage = helper.String(cvmInfo["disk_usage"].(string))
934+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
935+
ratelimit.Check(request.GetAction())
936+
result, e := me.client.UseCbsClient().DescribeDiskConfigQuota(request)
937+
if e != nil {
938+
return tccommon.RetryError(e)
939+
} else {
940+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
941+
}
942+
diskConfigSet = result.Response.DiskConfigSet
943+
return nil
944+
})
945+
if err != nil {
946+
errRet = err
947+
log.Printf("[CRITAL]%s create cbs DiskBackup failed, reason:%+v", logId, err)
948+
return
949+
}
950+
return
951+
}

tencentcloud/services/cvm/data_source_tc_instance_types.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
12+
svccbs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cbs"
1213

1314
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
1415
)
@@ -61,6 +62,40 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
6162
},
6263
},
6364
},
65+
"cbs_filter": {
66+
Type: schema.TypeList,
67+
Optional: true,
68+
Description: "Cbs filter.",
69+
MaxItems: 1,
70+
Elem: &schema.Resource{
71+
Schema: map[string]*schema.Schema{
72+
"disk_types": {
73+
Type: schema.TypeList,
74+
Optional: true,
75+
Elem: &schema.Schema{Type: schema.TypeString},
76+
Description: "Hard disk media type. Value range:\n" +
77+
" - CLOUD_BASIC: Represents ordinary Cloud Block Storage;\n" +
78+
" - CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;\n" +
79+
" - CLOUD_SSD: Represents SSD Cloud Block Storage;\n" +
80+
" - CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.",
81+
},
82+
"disk_charge_type": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
Description: "Payment model. Value range:\n" +
86+
" - PREPAID: Prepaid;\n" +
87+
" - POSTPAID_BY_HOUR: Post-payment.",
88+
},
89+
"disk_usage": {
90+
Type: schema.TypeString,
91+
Optional: true,
92+
Description: "System disk or data disk. Value range:\n" +
93+
" - SYSTEM_DISK: Represents the system disk;\n" +
94+
" - DATA_DISK: Represents the data disk.",
95+
},
96+
},
97+
},
98+
},
6499
"exclude_sold_out": {
65100
Type: schema.TypeBool,
66101
Optional: true,
@@ -120,6 +155,79 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
120155
Computed: true,
121156
Description: "Sell status of the instance.",
122157
},
158+
"cbs_configs": {
159+
Type: schema.TypeList,
160+
Computed: true,
161+
Description: "CBS config. The cbs_configs is populated when the cbs_filter is added.",
162+
Elem: &schema.Resource{
163+
Schema: map[string]*schema.Schema{
164+
"available": {
165+
Type: schema.TypeBool,
166+
Computed: true,
167+
Description: "Whether the configuration is available.",
168+
},
169+
"disk_charge_type": {
170+
Type: schema.TypeString,
171+
Computed: true,
172+
Description: "Payment model. Value range:\n" +
173+
" - PREPAID: Prepaid;\n" +
174+
" - POSTPAID_BY_HOUR: Post-payment.",
175+
},
176+
"zone": {
177+
Type: schema.TypeString,
178+
Computed: true,
179+
Description: "The availability zone to which the Cloud Block Storage belongs.",
180+
},
181+
"instance_family": {
182+
Type: schema.TypeString,
183+
Computed: true,
184+
Description: "Instance family.",
185+
},
186+
"disk_type": {
187+
Type: schema.TypeString,
188+
Computed: true,
189+
Description: "Hard disk media type. Value range:\n" +
190+
" - CLOUD_BASIC: Represents ordinary Cloud Block Storage;\n" +
191+
" - CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;\n" +
192+
" - CLOUD_SSD: Represents SSD Cloud Block Storage;\n" +
193+
" - CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.",
194+
},
195+
"step_size": {
196+
Type: schema.TypeInt,
197+
Computed: true,
198+
Description: "Minimum step size change in cloud disk size, in GB.",
199+
},
200+
"extra_performance_range": {
201+
Computed: true,
202+
Type: schema.TypeList,
203+
Elem: &schema.Schema{Type: schema.TypeInt},
204+
Description: "Extra performance range.",
205+
},
206+
"device_class": {
207+
Type: schema.TypeString,
208+
Computed: true,
209+
Description: "Device class.",
210+
},
211+
"disk_usage": {
212+
Type: schema.TypeString,
213+
Computed: true,
214+
Description: "Cloud disk type. Value range:\n" +
215+
" - SYSTEM_DISK: Represents the system disk;\n" +
216+
" - DATA_DISK: Represents the data disk.",
217+
},
218+
"min_disk_size": {
219+
Type: schema.TypeInt,
220+
Computed: true,
221+
Description: "The minimum configurable cloud disk size, in GB.",
222+
},
223+
"max_disk_size": {
224+
Type: schema.TypeInt,
225+
Computed: true,
226+
Description: "The maximum configurable cloud disk size, in GB.",
227+
},
228+
},
229+
},
230+
},
123231
},
124232
},
125233
},
@@ -207,6 +315,65 @@ func dataSourceTencentCloudInstanceTypesRead(d *schema.ResourceData, meta interf
207315
}
208316
}
209317

318+
client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
319+
cbsService := svccbs.NewCbsService(client)
320+
cbsFilterParams := make(map[string]interface{})
321+
var hasCbsFilter bool
322+
if dMap, ok := helper.InterfacesHeadMap(d, "cbs_filter"); ok {
323+
if v, ok := dMap["disk_types"].([]interface{}); ok && len(v) > 0 {
324+
cbsFilterParams["disk_types"] = helper.InterfacesStrings(v)
325+
}
326+
if v, ok := dMap["disk_charge_type"].(string); ok && v != "" {
327+
cbsFilterParams["disk_charge_type"] = v
328+
}
329+
if v, ok := dMap["disk_usage"].(string); ok && v != "" {
330+
cbsFilterParams["disk_usage"] = v
331+
}
332+
hasCbsFilter = true
333+
}
334+
if hasCbsFilter {
335+
for idx, t := range typeList {
336+
filterParams := make(map[string]interface{})
337+
for k, v := range cbsFilterParams {
338+
filterParams[k] = v
339+
}
340+
341+
if v, ok := t["availability_zone"].(*string); ok && v != nil {
342+
filterParams["availability_zone"] = *v
343+
}
344+
if v, ok := t["cpu_core_count"].(*int64); ok && v != nil {
345+
filterParams["cpu_core_count"] = *v
346+
}
347+
if v, ok := t["memory_size"].(*int64); ok && v != nil {
348+
filterParams["memory_size"] = *v
349+
}
350+
if v, ok := t["family"].(*string); ok && v != nil {
351+
filterParams["family"] = *v
352+
}
353+
diskConfigSet, err := cbsService.DescribeDiskConfigQuota(ctx, filterParams)
354+
if err != nil {
355+
return err
356+
}
357+
cbsConfigList := make([]interface{}, 0)
358+
for _, diskConfig := range diskConfigSet {
359+
cbsConfigList = append(cbsConfigList, map[string]interface{}{
360+
"available": diskConfig.Available,
361+
"disk_charge_type": diskConfig.DiskChargeType,
362+
"zone": diskConfig.Zone,
363+
"instance_family": diskConfig.InstanceFamily,
364+
"disk_type": diskConfig.DiskType,
365+
"step_size": diskConfig.StepSize,
366+
"extra_performance_range": diskConfig.ExtraPerformanceRange,
367+
"device_class": diskConfig.DeviceClass,
368+
"disk_usage": diskConfig.DiskUsage,
369+
"min_disk_size": diskConfig.MinDiskSize,
370+
"max_disk_size": diskConfig.MaxDiskSize,
371+
})
372+
}
373+
typeList[idx]["cbs_configs"] = cbsConfigList
374+
}
375+
}
376+
210377
d.SetId(helper.DataResourceIdsHash(ids))
211378
err = d.Set("instance_types", typeList)
212379
if err != nil {

tencentcloud/services/cvm/data_source_tc_instance_types_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ func TestAccTencentCloudCvmInstanceTypesDataSource_Sell(t *testing.T) {
5050
},
5151
})
5252
}
53+
func TestAccTencentCloudCvmInstanceTypesDataSource_WithCbsFilter(t *testing.T) {
54+
t.Parallel()
55+
resource.Test(t, resource.TestCase{
56+
PreCheck: func() {
57+
acctest.AccPreCheck(t)
58+
},
59+
Providers: acctest.AccProviders,
60+
Steps: []resource.TestStep{
61+
{
62+
Config: testAccCvmInstanceTypesDataSource_WithCbsFilter,
63+
Check: resource.ComposeTestCheckFunc(
64+
acctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_instance_types.with_cbs_filter"),
65+
resource.TestCheckResourceAttrSet("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.#"),
66+
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_type", "CLOUD_SSD"),
67+
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_charge_type", "PREPAID"),
68+
resource.TestCheckResourceAttr("data.tencentcloud_instance_types.with_cbs_filter", "instance_types.0.cbs_configs.0.disk_usage", "SYSTEM_DISK"),
69+
),
70+
},
71+
},
72+
})
73+
}
5374

5475
const testAccCvmInstanceTypesDataSource_SellCreate = `
5576
@@ -69,3 +90,26 @@ data "tencentcloud_instance_types" "example" {
6990
}
7091
7192
`
93+
94+
const testAccCvmInstanceTypesDataSource_WithCbsFilter = `
95+
96+
data "tencentcloud_instance_types" "with_cbs_filter" {
97+
cpu_core_count = 2
98+
memory_size = 2
99+
exclude_sold_out = true
100+
101+
filter {
102+
name = "instance-family"
103+
values = ["S6"]
104+
}
105+
filter {
106+
name = "zone"
107+
values = ["ap-guangzhou-6"]
108+
}
109+
cbs_filter {
110+
disk_types = ["CLOUD_SSD"]
111+
disk_charge_type = "PREPAID"
112+
disk_usage = "SYSTEM_DISK"
113+
}
114+
}
115+
`

website/docs/d/instance_types.html.markdown

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,28 @@ data "tencentcloud_instance_types" "example" {
4646
The following arguments are supported:
4747

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

57+
The `cbs_filter` object supports the following:
58+
59+
* `disk_charge_type` - (Optional, String) Payment model. Value range:
60+
- PREPAID: Prepaid;
61+
- POSTPAID_BY_HOUR: Post-payment.
62+
* `disk_types` - (Optional, List) Hard disk media type. Value range:
63+
- CLOUD_BASIC: Represents ordinary Cloud Block Storage;
64+
- CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;
65+
- CLOUD_SSD: Represents SSD Cloud Block Storage;
66+
- CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.
67+
* `disk_usage` - (Optional, String) System disk or data disk. Value range:
68+
- SYSTEM_DISK: Represents the system disk;
69+
- DATA_DISK: Represents the data disk.
70+
5671
The `filter` object supports the following:
5772

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

6580
* `instance_types` - An information list of cvm instance. Each element contains the following attributes:
6681
* `availability_zone` - The available zone that the CVM instance locates at.
82+
* `cbs_configs` - CBS config. The cbs_configs is populated when the cbs_filter is added.
83+
* `available` - Whether the configuration is available.
84+
* `device_class` - Device class.
85+
* `disk_charge_type` - Payment model. Value range:
86+
- PREPAID: Prepaid;
87+
- POSTPAID_BY_HOUR: Post-payment.
88+
* `disk_type` - Hard disk media type. Value range:
89+
- CLOUD_BASIC: Represents ordinary Cloud Block Storage;
90+
- CLOUD_PREMIUM: Represents high-performance Cloud Block Storage;
91+
- CLOUD_SSD: Represents SSD Cloud Block Storage;
92+
- CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage.
93+
* `disk_usage` - Cloud disk type. Value range:
94+
- SYSTEM_DISK: Represents the system disk;
95+
- DATA_DISK: Represents the data disk.
96+
* `extra_performance_range` - Extra performance range.
97+
* `instance_family` - Instance family.
98+
* `max_disk_size` - The maximum configurable cloud disk size, in GB.
99+
* `min_disk_size` - The minimum configurable cloud disk size, in GB.
100+
* `step_size` - Minimum step size change in cloud disk size, in GB.
101+
* `zone` - The availability zone to which the Cloud Block Storage belongs.
67102
* `cpu_core_count` - The number of CPU cores of the instance.
68103
* `family` - Type series of the instance.
69104
* `gpu_core_count` - The number of GPU cores of the instance.

0 commit comments

Comments
 (0)