Skip to content

Commit 38905ef

Browse files
authored
support dcdb data resource (#1882)
* support dcdb data resource * 1.add changelog. 2.e2e case. * fmt
1 parent c012574 commit 38905ef

40 files changed

+4412
-0
lines changed

.changelog/1882.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
```release-note:new-data-source
2+
tencentcloud_dcdb_file_download_url
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_dcdb_log_files
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_dcdb_instance_node_info
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_dcdb_orders
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_dcdb_price
19+
```
20+
21+
```release-note:new-data-source
22+
tencentcloud_dcdb_project_security_groups
23+
```
24+
25+
```release-note:new-data-source
26+
tencentcloud_dcdb_projects
27+
```
28+
29+
```release-note:new-data-source
30+
tencentcloud_dcdb_renewal_price
31+
```
32+
33+
```release-note:new-data-source
34+
tencentcloud_dcdb_sale_info
35+
```
36+
37+
```release-note:new-data-source
38+
tencentcloud_dcdb_shard_spec
39+
```
40+
41+
```release-note:new-data-source
42+
tencentcloud_dcdb_slow_logs
43+
```
44+
45+
```release-note:new-data-source
46+
tencentcloud_dcdb_upgrade_price
47+
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Use this data source to query detailed information of dcdb file_download_url
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dcdb_file_download_url" "file_download_url" {
8+
instance_id = local.dcdb_id
9+
shard_id = "shard-1b5r04az"
10+
file_path = "/cos_backup/test.txt"
11+
}
12+
```
13+
*/
14+
package tencentcloud
15+
16+
import (
17+
"context"
18+
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudDcdbFileDownloadUrl() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudDcdbFileDownloadUrlRead,
27+
Schema: map[string]*schema.Schema{
28+
"instance_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "Instance ID.",
32+
},
33+
34+
"shard_id": {
35+
Required: true,
36+
Type: schema.TypeString,
37+
Description: "Instance Shard ID.",
38+
},
39+
40+
"file_path": {
41+
Required: true,
42+
Type: schema.TypeString,
43+
Description: "Unsigned file path.",
44+
},
45+
46+
"pre_signed_url": {
47+
Computed: true,
48+
Type: schema.TypeString,
49+
Description: "Signed download URL.",
50+
},
51+
52+
"result_output_file": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
Description: "Used to save results.",
56+
},
57+
},
58+
}
59+
}
60+
61+
func dataSourceTencentCloudDcdbFileDownloadUrlRead(d *schema.ResourceData, meta interface{}) error {
62+
defer logElapsed("data_source.tencentcloud_dcdb_file_download_url.read")()
63+
defer inconsistentCheck(d, meta)()
64+
65+
logId := getLogId(contextNil)
66+
67+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
68+
var (
69+
preSignedUrl *string
70+
instanceId string
71+
)
72+
73+
paramMap := make(map[string]interface{})
74+
if v, ok := d.GetOk("instance_id"); ok {
75+
paramMap["InstanceId"] = helper.String(v.(string))
76+
instanceId = v.(string)
77+
}
78+
79+
if v, ok := d.GetOk("shard_id"); ok {
80+
paramMap["ShardId"] = helper.String(v.(string))
81+
}
82+
83+
if v, ok := d.GetOk("file_path"); ok {
84+
paramMap["FilePath"] = helper.String(v.(string))
85+
}
86+
87+
service := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
88+
89+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
90+
result, e := service.DescribeDcdbFileDownloadUrlByFilter(ctx, paramMap)
91+
if e != nil {
92+
return retryError(e)
93+
}
94+
preSignedUrl = result
95+
return nil
96+
})
97+
if err != nil {
98+
return err
99+
}
100+
101+
if preSignedUrl != nil {
102+
_ = d.Set("pre_signed_url", preSignedUrl)
103+
}
104+
105+
d.SetId(instanceId)
106+
output, ok := d.GetOk("result_output_file")
107+
if ok && output.(string) != "" {
108+
if e := writeToFile(output.(string), preSignedUrl); e != nil {
109+
return e
110+
}
111+
}
112+
return nil
113+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudDcdbNeedFixFileDownloadUrlDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDcdbFileDownloadUrlDataSource,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dcdb_file_download_url.file_download_url"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_log_files.log_files", "shard_id", "shard-1b5r04az"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_log_files.log_files", "file_path"),
23+
// resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_log_files.log_files", "pre_signed_url"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccDcdbFileDownloadUrlDataSource = CommonPresetDcdb + `
31+
32+
data "tencentcloud_dcdb_file_download_url" "file_download_url" {
33+
instance_id = local.dcdb_id
34+
shard_id = "shard-1b5r04az"
35+
file_path = "/cos_backup/test.txt"
36+
}
37+
`
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Use this data source to query detailed information of dcdb instance_node_info
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dcdb_instance_node_info" "instance_node_info" {
8+
instance_id = local.dcdb_id
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudDcdbInstanceNodeInfo() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudDcdbInstanceNodeInfoRead,
26+
Schema: map[string]*schema.Schema{
27+
"instance_id": {
28+
Required: true,
29+
Type: schema.TypeString,
30+
Description: "Instance ID, such as tdsqlshard-6ltok4u9.",
31+
},
32+
33+
"nodes_info": {
34+
Computed: true,
35+
Type: schema.TypeList,
36+
Description: "Node information.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"node_id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "Node ID.",
43+
},
44+
"role": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "Node role. Valid values: `master`, `slave`.",
48+
},
49+
"shard_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "Instance shard ID.",
53+
},
54+
},
55+
},
56+
},
57+
58+
"result_output_file": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
Description: "Used to save results.",
62+
},
63+
},
64+
}
65+
}
66+
67+
func dataSourceTencentCloudDcdbInstanceNodeInfoRead(d *schema.ResourceData, meta interface{}) error {
68+
defer logElapsed("data_source.tencentcloud_dcdb_instance_node_info.read")()
69+
defer inconsistentCheck(d, meta)()
70+
71+
logId := getLogId(contextNil)
72+
73+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
74+
var (
75+
instanceId string
76+
)
77+
78+
paramMap := make(map[string]interface{})
79+
if v, ok := d.GetOk("instance_id"); ok {
80+
paramMap["InstanceId"] = helper.String(v.(string))
81+
instanceId = v.(string)
82+
}
83+
84+
service := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
85+
86+
var nodesInfo []*dcdb.BriefNodeInfo
87+
88+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
89+
result, e := service.DescribeDcdbInstanceNodeInfoByFilter(ctx, paramMap)
90+
if e != nil {
91+
return retryError(e)
92+
}
93+
nodesInfo = result
94+
return nil
95+
})
96+
if err != nil {
97+
return err
98+
}
99+
100+
tmpList := make([]map[string]interface{}, 0, len(nodesInfo))
101+
102+
if nodesInfo != nil {
103+
for _, briefNodeInfo := range nodesInfo {
104+
briefNodeInfoMap := map[string]interface{}{}
105+
106+
if briefNodeInfo.NodeId != nil {
107+
briefNodeInfoMap["node_id"] = briefNodeInfo.NodeId
108+
}
109+
110+
if briefNodeInfo.Role != nil {
111+
briefNodeInfoMap["role"] = briefNodeInfo.Role
112+
}
113+
114+
if briefNodeInfo.ShardId != nil {
115+
briefNodeInfoMap["shard_id"] = briefNodeInfo.ShardId
116+
}
117+
118+
tmpList = append(tmpList, briefNodeInfoMap)
119+
}
120+
121+
_ = d.Set("nodes_info", tmpList)
122+
}
123+
124+
d.SetId(instanceId)
125+
output, ok := d.GetOk("result_output_file")
126+
if ok && output.(string) != "" {
127+
if e := writeToFile(output.(string), tmpList); e != nil {
128+
return e
129+
}
130+
}
131+
return nil
132+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudDcdbInstanceNodeInfoDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDcdbInstanceNodeInfoDataSource,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dcdb_instance_node_info.instance_node_info"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_instance_node_info.instance_node_info", "nodes_info.#"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccDcdbInstanceNodeInfoDataSource = CommonPresetDcdb + `
29+
30+
data "tencentcloud_dcdb_instance_node_info" "instance_node_info" {
31+
instance_id = local.dcdb_id
32+
}
33+
34+
`

0 commit comments

Comments
 (0)