Skip to content

Commit 9fb6a63

Browse files
authored
feat: cbs datasource - extend filters (#1047)
* feat: cbs datasource - extend filters * fix: cbs data - remove tags test of datasource
1 parent be02812 commit 9fb6a63

File tree

5 files changed

+146
-12
lines changed

5 files changed

+146
-12
lines changed

tencentcloud/data_source_tc_cbs_storages.go

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ data "tencentcloud_cbs_storages" "storages" {
99
result_output_file = "mytestpath"
1010
}
1111
```
12+
13+
The following snippet shows the new supported query params
14+
15+
```hcl
16+
data "tencentcloud_cbs_storages" "whats_new" {
17+
charge_type = ["POSTPAID_BY_HOUR", "PREPAID"]
18+
portable = true
19+
storage_state = ["ATTACHED"]
20+
instance_ips = ["10.0.0.2"]
21+
instance_name = ["my-instance"]
22+
tag_keys = ["foo"]
23+
tag_values = ["bar", "baz"]
24+
}
25+
```
26+
1227
*/
1328
package tencentcloud
1429

@@ -58,6 +73,47 @@ func dataSourceTencentCloudCbsStorages() *schema.Resource {
5873
Optional: true,
5974
Description: "Filter by cloud disk type (`SYSTEM_DISK`: system disk | `DATA_DISK`: data disk).",
6075
},
76+
"charge_type": {
77+
Type: schema.TypeList,
78+
Optional: true,
79+
Description: "List filter by disk charge type (`POSTPAID_BY_HOUR` | `PREPAID`).",
80+
Elem: &schema.Schema{Type: schema.TypeString},
81+
},
82+
"portable": {
83+
Type: schema.TypeBool,
84+
Optional: true,
85+
Description: "Filter by whether the disk is portable (Boolean `true` or `false`).",
86+
},
87+
"storage_state": {
88+
Type: schema.TypeList,
89+
Optional: true,
90+
Description: "List filter by disk state (`UNATTACHED` | `ATTACHING` | `ATTACHED` | `DETACHING` | `EXPANDING` | `ROLLBACKING` | `TORECYCLE`).",
91+
Elem: &schema.Schema{Type: schema.TypeString},
92+
},
93+
"instance_ips": {
94+
Type: schema.TypeList,
95+
Optional: true,
96+
Description: "List filter by attached instance public or private IPs.",
97+
Elem: &schema.Schema{Type: schema.TypeString},
98+
},
99+
"instance_name": {
100+
Type: schema.TypeList,
101+
Optional: true,
102+
Description: "List filter by attached instance name.",
103+
Elem: &schema.Schema{Type: schema.TypeString},
104+
},
105+
"tag_keys": {
106+
Type: schema.TypeList,
107+
Optional: true,
108+
Description: "List filter by tag keys.",
109+
Elem: &schema.Schema{Type: schema.TypeString},
110+
},
111+
"tag_values": {
112+
Type: schema.TypeList,
113+
Optional: true,
114+
Description: "List filter by tag values.",
115+
Elem: &schema.Schema{Type: schema.TypeString},
116+
},
61117
"result_output_file": {
62118
Type: schema.TypeString,
63119
Optional: true,
@@ -162,7 +218,7 @@ func dataSourceTencentCloudCbsStoragesRead(d *schema.ResourceData, meta interfac
162218
logId := getLogId(contextNil)
163219
ctx := context.WithValue(context.TODO(), logIdKey, logId)
164220

165-
params := make(map[string]string)
221+
params := make(map[string]interface{})
166222
if v, ok := d.GetOk("storage_id"); ok {
167223
params["disk-id"] = v.(string)
168224
}
@@ -182,6 +238,34 @@ func dataSourceTencentCloudCbsStoragesRead(d *schema.ResourceData, meta interfac
182238
params["disk-usage"] = v.(string)
183239
}
184240

241+
if v, ok := d.GetOk("charge_type"); ok {
242+
params["disk-charge-type"] = helper.InterfacesStringsPoint(v.([]interface{}))
243+
}
244+
245+
if v, ok := d.GetOk("portable"); ok {
246+
if v.(bool) {
247+
params["portable"] = "TRUE"
248+
} else {
249+
params["portable"] = "FALSE"
250+
}
251+
}
252+
253+
if v, ok := d.GetOk("storage_state"); ok {
254+
params["disk-state"] = helper.InterfacesStringsPoint(v.([]interface{}))
255+
}
256+
if v, ok := d.GetOk("instance_ips"); ok {
257+
params["instance-ip-address"] = helper.InterfacesStringsPoint(v.([]interface{}))
258+
}
259+
if v, ok := d.GetOk("instance_name"); ok {
260+
params["instance-name"] = helper.InterfacesStringsPoint(v.([]interface{}))
261+
}
262+
if v, ok := d.GetOk("tag_keys"); ok {
263+
params["tag-key"] = helper.InterfacesStringsPoint(v.([]interface{}))
264+
}
265+
if v, ok := d.GetOk("tag_values"); ok {
266+
params["tag-value"] = helper.InterfacesStringsPoint(v.([]interface{}))
267+
}
268+
185269
cbsService := CbsService{
186270
client: meta.(*TencentCloudClient).apiV3Conn,
187271
}

tencentcloud/data_source_tc_cbs_storages_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
77
)
88

9-
func TestAccTencentCloudCbsStoragesDataSource(t *testing.T) {
9+
func TestAccTencentCloudCbsStoragesDataSourceId(t *testing.T) {
1010
t.Parallel()
1111

1212
resource.Test(t, resource.TestCase{
@@ -29,14 +29,31 @@ func TestAccTencentCloudCbsStoragesDataSource(t *testing.T) {
2929
resource.TestCheckResourceAttr("data.tencentcloud_cbs_storages.storages", "storage_list.0.attached", "false"),
3030
resource.TestCheckResourceAttrSet("data.tencentcloud_cbs_storages.storages", "storage_list.0.create_time"),
3131
resource.TestCheckResourceAttrSet("data.tencentcloud_cbs_storages.storages", "storage_list.0.status"),
32-
resource.TestCheckResourceAttr("data.tencentcloud_cbs_storages.storages", "storage_list.0.tags.test", "tf"),
3332
resource.TestCheckResourceAttrSet("data.tencentcloud_cbs_storages.storages", "storage_list.0.charge_type"),
3433
),
3534
},
3635
},
3736
})
3837
}
3938

39+
func TestAccTencentCloudCbsStoragesDataSourceNewParams(t *testing.T) {
40+
t.Parallel()
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
CheckDestroy: testAccCheckCbsStorageDestroy,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: testAccCbsStoragesDataSourceNewParams,
49+
Check: resource.ComposeAggregateTestCheckFunc(
50+
resource.TestCheckResourceAttrSet("data.tencentcloud_cbs_storages.storages", "storage_list.#"),
51+
),
52+
},
53+
},
54+
})
55+
}
56+
4057
const testAccCbsStoragesDataSource = `
4158
resource "tencentcloud_cbs_storage" "storage" {
4259
storage_type = "CLOUD_PREMIUM"
@@ -45,13 +62,22 @@ resource "tencentcloud_cbs_storage" "storage" {
4562
availability_zone = "ap-guangzhou-3"
4663
project_id = 0
4764
encrypt = false
48-
49-
tags = {
50-
test = "tf"
51-
}
5265
}
5366
5467
data "tencentcloud_cbs_storages" "storages" {
5568
storage_id = tencentcloud_cbs_storage.storage.id
5669
}
5770
`
71+
72+
const testAccCbsStoragesDataSourceNewParams = `
73+
data "tencentcloud_cbs_storages" "storages" {
74+
storage_name = "disk-foo"
75+
charge_type = ["POSTPAID_BY_HOUR", "PREPAID"]
76+
portable = true
77+
storage_state = ["ATTACHED"]
78+
instance_ips = ["10.0.0.2"]
79+
instance_name = ["my-instance"]
80+
tag_keys = ["foo"]
81+
tag_values = ["bar", "baz"]
82+
}
83+
`

tencentcloud/resource_tc_cbs_storage_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ func TestAccTencentCloudCbsStorage_full(t *testing.T) {
123123
}
124124

125125
func TestAccTencentCloudCbsStorage_prepaid(t *testing.T) {
126-
t.Parallel()
127126

128127
resource.Test(t, resource.TestCase{
129128
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
@@ -153,7 +152,6 @@ func TestAccTencentCloudCbsStorage_prepaid(t *testing.T) {
153152
}
154153

155154
func TestAccTencentCloudCbsStorage_upgrade(t *testing.T) {
156-
t.Parallel()
157155

158156
resource.Test(t, resource.TestCase{
159157
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },

tencentcloud/service_tencentcloud_cbs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ func (me *CbsService) DescribeDiskList(ctx context.Context, diskIds []*string) (
4848
return
4949
}
5050

51-
func (me *CbsService) DescribeDisksByFilter(ctx context.Context, params map[string]string) (disks []*cbs.Disk, errRet error) {
51+
func (me *CbsService) DescribeDisksByFilter(ctx context.Context, params map[string]interface{}) (disks []*cbs.Disk, errRet error) {
5252
logId := getLogId(ctx)
5353
request := cbs.NewDescribeDisksRequest()
5454
request.Filters = make([]*cbs.Filter, 0, len(params))
5555
for k, v := range params {
5656
filter := &cbs.Filter{
57-
Name: helper.String(k),
58-
Values: []*string{helper.String(v)},
57+
Name: helper.String(k),
58+
}
59+
switch v.(type) {
60+
case string:
61+
filter.Values = []*string{helper.String(v.(string))}
62+
case []*string:
63+
filter.Values = v.([]*string)
5964
}
6065
request.Filters = append(request.Filters, filter)
6166
}

website/docs/d/cbs_storages.html.markdown

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,38 @@ data "tencentcloud_cbs_storages" "storages" {
2020
}
2121
```
2222

23+
The following snippet shows the new supported query params
24+
25+
```hcl
26+
data "tencentcloud_cbs_storages" "whats_new" {
27+
charge_type = ["POSTPAID_BY_HOUR", "PREPAID"]
28+
portable = true
29+
storage_state = ["ATTACHED"]
30+
instance_ips = ["10.0.0.2"]
31+
instance_name = ["my-instance"]
32+
tag_keys = ["foo"]
33+
tag_values = ["bar", "baz"]
34+
}
35+
```
36+
2337
## Argument Reference
2438

2539
The following arguments are supported:
2640

2741
* `availability_zone` - (Optional) The available zone that the CBS instance locates at.
42+
* `charge_type` - (Optional) List filter by disk charge type (`POSTPAID_BY_HOUR` | `PREPAID`).
43+
* `instance_ips` - (Optional) List filter by attached instance public or private IPs.
44+
* `instance_name` - (Optional) List filter by attached instance name.
45+
* `portable` - (Optional) Filter by whether the disk is portable (Boolean `true` or `false`).
2846
* `project_id` - (Optional) ID of the project with which the CBS is associated.
2947
* `result_output_file` - (Optional) Used to save results.
3048
* `storage_id` - (Optional) ID of the CBS to be queried.
3149
* `storage_name` - (Optional) Name of the CBS to be queried.
50+
* `storage_state` - (Optional) List filter by disk state (`UNATTACHED` | `ATTACHING` | `ATTACHED` | `DETACHING` | `EXPANDING` | `ROLLBACKING` | `TORECYCLE`).
3251
* `storage_type` - (Optional) Filter by cloud disk media type (`CLOUD_BASIC`: HDD cloud disk | `CLOUD_PREMIUM`: Premium Cloud Storage | `CLOUD_SSD`: SSD cloud disk).
3352
* `storage_usage` - (Optional) Filter by cloud disk type (`SYSTEM_DISK`: system disk | `DATA_DISK`: data disk).
53+
* `tag_keys` - (Optional) List filter by tag keys.
54+
* `tag_values` - (Optional) List filter by tag values.
3455

3556
## Attributes Reference
3657

0 commit comments

Comments
 (0)