Skip to content

Commit 00c615a

Browse files
authored
support dbbrain data source (#1776)
* support dbbrain data source * add changelog and update doc * fix golangci-lint issues
1 parent 838cb4d commit 00c615a

13 files changed

+1099
-0
lines changed

.changelog/1776.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:new-data-source
2+
tencentcloud_dbbrain_slow_logs
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_dbbrain_sql_templates
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_dbbrain_db_space_status
11+
```
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
Use this data source to query detailed information of dbbrain db_space_status
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dbbrain_db_space_status" "db_space_status" {
8+
instance_id = "%s"
9+
range_days = 7
10+
product = "mysql"
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+
dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func dataSourceTencentCloudDbbrainDbSpaceStatus() *schema.Resource {
26+
return &schema.Resource{
27+
Read: dataSourceTencentCloudDbbrainDbSpaceStatusRead,
28+
Schema: map[string]*schema.Schema{
29+
"instance_id": {
30+
Required: true,
31+
Type: schema.TypeString,
32+
Description: "instance id.",
33+
},
34+
35+
"range_days": {
36+
Optional: true,
37+
Type: schema.TypeInt,
38+
Description: "The number of days in the time period, the deadline is the current day, and the default is 7 days.",
39+
},
40+
41+
"product": {
42+
Optional: true,
43+
Type: schema.TypeString,
44+
Description: "Service product type, supported values include: mysql - cloud database MySQL, cynosdb - cloud database CynosDB for MySQL, the default is mysql.",
45+
},
46+
47+
"growth": {
48+
Computed: true,
49+
Type: schema.TypeInt,
50+
Description: "Disk growth (MB).",
51+
},
52+
53+
"remain": {
54+
Computed: true,
55+
Type: schema.TypeInt,
56+
Description: "Disk remaining (MB).",
57+
},
58+
59+
"total": {
60+
Computed: true,
61+
Type: schema.TypeInt,
62+
Description: "Total disk size (MB).",
63+
},
64+
65+
"available_days": {
66+
Computed: true,
67+
Type: schema.TypeInt,
68+
Description: "Estimated number of days available.",
69+
},
70+
71+
"result_output_file": {
72+
Type: schema.TypeString,
73+
Optional: true,
74+
Description: "Used to save results.",
75+
},
76+
},
77+
}
78+
}
79+
80+
func dataSourceTencentCloudDbbrainDbSpaceStatusRead(d *schema.ResourceData, meta interface{}) error {
81+
defer logElapsed("data_source.tencentcloud_dbbrain_db_space_status.read")()
82+
defer inconsistentCheck(d, meta)()
83+
84+
logId := getLogId(contextNil)
85+
86+
var instanceId string
87+
88+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
89+
90+
paramMap := make(map[string]interface{})
91+
if v, ok := d.GetOk("instance_id"); ok {
92+
paramMap["InstanceId"] = helper.String(v.(string))
93+
instanceId = v.(string)
94+
}
95+
96+
if v, _ := d.GetOk("range_days"); v != nil {
97+
paramMap["RangeDays"] = helper.IntInt64(v.(int))
98+
}
99+
100+
if v, ok := d.GetOk("product"); ok {
101+
paramMap["Product"] = helper.String(v.(string))
102+
}
103+
104+
service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn}
105+
106+
var rows *dbbrain.DescribeDBSpaceStatusResponseParams
107+
108+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
109+
result, e := service.DescribeDbbrainDbSpaceStatusByFilter(ctx, paramMap)
110+
if e != nil {
111+
return retryError(e)
112+
}
113+
rows = result
114+
return nil
115+
})
116+
if err != nil {
117+
return err
118+
}
119+
120+
tmpList := []map[string]interface{}{}
121+
122+
if rows != nil {
123+
124+
if rows.Growth != nil {
125+
_ = d.Set("growth", rows.Growth)
126+
}
127+
128+
if rows.Remain != nil {
129+
_ = d.Set("remain", rows.Remain)
130+
}
131+
132+
if rows.Total != nil {
133+
_ = d.Set("total", rows.Total)
134+
}
135+
136+
if rows.AvailableDays != nil {
137+
_ = d.Set("available_days", rows.AvailableDays)
138+
}
139+
tmpList = append(tmpList, map[string]interface{}{
140+
"growth": rows.Growth,
141+
"remain": rows.Remain,
142+
"total": rows.Total,
143+
"available_days": rows.AvailableDays,
144+
})
145+
146+
}
147+
148+
d.SetId(helper.DataResourceIdHash(instanceId))
149+
output, ok := d.GetOk("result_output_file")
150+
if ok && output.(string) != "" {
151+
if e := writeToFile(output.(string), tmpList); e != nil {
152+
return e
153+
}
154+
}
155+
return nil
156+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccTencentCloudDbbrainDbSpaceStatusDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: fmt.Sprintf(testAccDbbrainDbSpaceStatusDataSource, defaultDbBrainInstanceId),
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dbbrain_db_space_status.db_space_status"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_dbbrain_db_space_status.db_space_status", "instance_id", defaultDbBrainInstanceId),
23+
resource.TestCheckResourceAttr("data.tencentcloud_dbbrain_db_space_status.db_space_status", "range_days", "7"),
24+
resource.TestCheckResourceAttr("data.tencentcloud_dbbrain_db_space_status.db_space_status", "product", "mysql"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_db_space_status.db_space_status", "growth"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_db_space_status.db_space_status", "remain"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_db_space_status.db_space_status", "total"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_db_space_status.db_space_status", "available_days"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
const testAccDbbrainDbSpaceStatusDataSource = `
36+
37+
data "tencentcloud_dbbrain_db_space_status" "db_space_status" {
38+
instance_id = "%s"
39+
range_days = 7
40+
product = "mysql"
41+
}
42+
43+
`

0 commit comments

Comments
 (0)