Skip to content

Commit ef3c8ff

Browse files
authored
Feat/cdwch datasource support (#2382)
* add cdwch datasource * update antiddos datasource back param * add changelog 2382.txt
1 parent 3bda399 commit ef3c8ff

18 files changed

+1061
-44
lines changed

.changelog/2382.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-data-source
2+
tencentcloud_clickhouse_spec
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_clickhouse_instance_shards
7+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Use this data source to query detailed information of clickhouse instance_shards
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_clickhouse_instance_shards" "instance_shards" {
7+
instance_id = "cdwch-datuhk3z"
8+
}
9+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Use this data source to query detailed information of clickhouse spec
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_clickhouse_spec" "spec" {
7+
zone = "ap-guangzhou-7"
8+
pay_mode = "PREPAID"
9+
is_elastic = false
10+
}
11+
```

tencentcloud/data_source_tc_antiddos_basic_device_status.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,39 @@ func dataSourceTencentCloudAntiddosBasicDeviceStatusRead(d *schema.ResourceData,
127127
tmpList := make([]map[string]interface{}, 0)
128128

129129
if basicDeviceStatus.Data != nil {
130-
for _, keyValue := range basicDeviceStatus.Data {
130+
data := basicDeviceStatus.Data
131+
dataTmpList := make([]map[string]interface{}, 0, len(data))
132+
133+
for _, keyValue := range data {
131134
keyValueMap := map[string]interface{}{}
132135
if keyValue.Key != nil {
133136
keyValueMap["key"] = keyValue.Key
134137
}
135138
if keyValue.Value != nil {
136139
keyValueMap["value"] = keyValue.Value
137140
}
138-
tmpList = append(tmpList, keyValueMap)
141+
dataTmpList = append(dataTmpList, keyValueMap)
139142
}
140-
_ = d.Set("data", tmpList)
143+
tmpList = append(tmpList, dataTmpList...)
144+
_ = d.Set("data", dataTmpList)
141145
}
142146

143147
if basicDeviceStatus.CLBData != nil {
144-
for _, keyValue := range basicDeviceStatus.CLBData {
148+
clbData := basicDeviceStatus.CLBData
149+
clbDataTmpList := make([]map[string]interface{}, 0, len(clbData))
150+
151+
for _, keyValue := range clbData {
145152
keyValueMap := map[string]interface{}{}
146153
if keyValue.Key != nil {
147154
keyValueMap["key"] = keyValue.Key
148155
}
149156
if keyValue.Value != nil {
150157
keyValueMap["value"] = keyValue.Value
151158
}
152-
tmpList = append(tmpList, keyValueMap)
159+
clbDataTmpList = append(clbDataTmpList, keyValueMap)
153160
}
154-
_ = d.Set("clb_data", tmpList)
161+
tmpList = append(tmpList, clbDataTmpList...)
162+
_ = d.Set("clb_data", clbDataTmpList)
155163
}
156164

157165
d.SetId(helper.BuildToken())

tencentcloud/data_source_tc_antiddos_list_listener.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ func dataSourceTencentCloudAntiddosListListenerRead(d *schema.ResourceData, meta
276276
tmpList := make([]map[string]interface{}, 0)
277277

278278
if listListener.Layer4Listeners != nil {
279-
for _, layer4Rule := range listListener.Layer4Listeners {
279+
layer4Listeners := listListener.Layer4Listeners
280+
layer4ListenersTmpList := make([]map[string]interface{}, 0, len(layer4Listeners))
281+
282+
for _, layer4Rule := range layer4Listeners {
280283
layer4RuleMap := map[string]interface{}{}
281284

282285
if layer4Rule.BackendPort != nil {
@@ -360,14 +363,17 @@ func dataSourceTencentCloudAntiddosListListenerRead(d *schema.ResourceData, meta
360363
layer4RuleMap["instance_detail_rule"] = instanceDetailRuleList
361364
}
362365

363-
tmpList = append(tmpList, layer4RuleMap)
366+
layer4ListenersTmpList = append(layer4ListenersTmpList, layer4RuleMap)
364367
}
365-
366-
_ = d.Set("layer4_listeners", tmpList)
368+
tmpList = append(tmpList, layer4ListenersTmpList...)
369+
_ = d.Set("layer4_listeners", layer4ListenersTmpList)
367370
}
368371

369372
if listListener.Layer7Listeners != nil {
370-
for _, layer7Rule := range listListener.Layer7Listeners {
373+
layer7Listeners := listListener.Layer7Listeners
374+
layer7ListenersTmpList := make([]map[string]interface{}, 0, len(layer7Listeners))
375+
376+
for _, layer7Rule := range layer7Listeners {
371377
layer7RuleMap := map[string]interface{}{}
372378

373379
if layer7Rule.Domain != nil {
@@ -470,10 +476,10 @@ func dataSourceTencentCloudAntiddosListListenerRead(d *schema.ResourceData, meta
470476
layer7RuleMap["vport"] = layer7Rule.Vport
471477
}
472478

473-
tmpList = append(tmpList, layer7RuleMap)
479+
layer7ListenersTmpList = append(layer7ListenersTmpList, layer7RuleMap)
474480
}
475-
476-
_ = d.Set("layer7_listeners", tmpList)
481+
tmpList = append(tmpList, layer7ListenersTmpList...)
482+
_ = d.Set("layer7_listeners", layer7ListenersTmpList)
477483
}
478484

479485
d.SetId(helper.BuildToken())
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
cdwch "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch/v20200915"
9+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
10+
)
11+
12+
func dataSourceTencentCloudClickhouseInstanceShards() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceTencentCloudClickhouseInstanceShardsRead,
15+
Schema: map[string]*schema.Schema{
16+
"instance_id": {
17+
Required: true,
18+
Type: schema.TypeString,
19+
Description: "Cluster instance ID.",
20+
},
21+
22+
"instance_shards_list": {
23+
Computed: true,
24+
Type: schema.TypeString,
25+
Description: "Instance shard information.",
26+
},
27+
28+
"result_output_file": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
Description: "Used to save results.",
32+
},
33+
},
34+
}
35+
}
36+
37+
func dataSourceTencentCloudClickhouseInstanceShardsRead(d *schema.ResourceData, meta interface{}) error {
38+
defer logElapsed("data_source.tencentcloud_clickhouse_instance_shards.read")()
39+
defer inconsistentCheck(d, meta)()
40+
41+
logId := getLogId(contextNil)
42+
43+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
44+
45+
paramMap := make(map[string]interface{})
46+
if v, ok := d.GetOk("instance_id"); ok {
47+
paramMap["InstanceId"] = helper.String(v.(string))
48+
}
49+
50+
service := CdwchService{client: meta.(*TencentCloudClient).apiV3Conn}
51+
52+
var instanceShards *cdwch.DescribeInstanceShardsResponseParams
53+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
54+
result, e := service.DescribeClickhouseInstanceShardsByFilter(ctx, paramMap)
55+
if e != nil {
56+
return retryError(e)
57+
}
58+
instanceShards = result
59+
return nil
60+
})
61+
if err != nil {
62+
return err
63+
}
64+
65+
if instanceShards.InstanceShardsList != nil {
66+
_ = d.Set("instance_shards_list", instanceShards.InstanceShardsList)
67+
}
68+
69+
d.SetId(helper.BuildToken())
70+
71+
output, ok := d.GetOk("result_output_file")
72+
if ok && output.(string) != "" {
73+
if e := writeToFile(output.(string), d); e != nil {
74+
return e
75+
}
76+
}
77+
return nil
78+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudClickhouseInstanceShardsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccClickhouseInstanceShardsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_clickhouse_instance_shards.instance_shards")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccClickhouseInstanceShardsDataSource = `
26+
data "tencentcloud_clickhouse_instance_shards" "instance_shards" {
27+
instance_id = "cdwch-datuhk3z"
28+
}
29+
`

0 commit comments

Comments
 (0)