Skip to content

Commit ab25f1c

Browse files
authored
Feat/support dbbrain datasource (#1610)
* feat:add tke_available_cluster_versions * add 8 data_source * add changelog * adjust e2e case * adjust logic for log export task * rm audit log download url e2e case
1 parent 1f95188 commit ab25f1c

30 files changed

+3056
-3
lines changed

.changelog/1610.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```release-note:new-data-source
2+
tencentcloud_dbbrain_diag_event
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_dbbrain_diag_events
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_dbbrain_diag_history
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_dbbrain_security_audit_log_download_urls
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_dbbrain_slow_log_time_series_stats
19+
```
20+
21+
```release-note:new-data-source
22+
tencentcloud_dbbrain_slow_log_top_sqls
23+
```
24+
25+
```release-note:new-data-source
26+
tencentcloud_dbbrain_slow_log_user_host_stats
27+
```
28+
29+
```release-note:new-data-source
30+
tencentcloud_dbbrain_slow_log_user_sql_advice
31+
```

tencentcloud/basic_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ const (
870870

871871
// DBBRAIN
872872
const (
873-
defaultDbBrainsagId = "sag-01z37l4g"
873+
defaultDbBrainsagId = "sag-01z37l4g"
874+
defaultDbBrainInstanceId = "cdb-fitq5t9h"
874875
)
875876

876877
// End of DBBRAIN
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Use this data source to query detailed information of dbbrain diag_event
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dbbrain_diag_history" "diag_history" {
8+
instance_id = "%s"
9+
start_time = "%s"
10+
end_time = "%s"
11+
product = "mysql"
12+
}
13+
14+
data "tencentcloud_dbbrain_diag_event" "diag_event" {
15+
instance_id = "%s"
16+
event_id = data.tencentcloud_dbbrain_diag_history.diag_history.events.0.event_id
17+
product = "mysql"
18+
}
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
26+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
27+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
28+
dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527"
29+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
30+
)
31+
32+
func dataSourceTencentCloudDbbrainDiagEvent() *schema.Resource {
33+
return &schema.Resource{
34+
Read: dataSourceTencentCloudDbbrainDiagEventRead,
35+
Schema: map[string]*schema.Schema{
36+
"instance_id": {
37+
Required: true,
38+
Type: schema.TypeString,
39+
Description: "isntance id.",
40+
},
41+
42+
"event_id": {
43+
Optional: true,
44+
Computed: true,
45+
Type: schema.TypeInt,
46+
Description: "Event ID. Obtain it through `Get Instance Diagnosis History DescribeDBDiagHistory`.",
47+
},
48+
49+
"product": {
50+
Optional: true,
51+
Type: schema.TypeString,
52+
Description: "Service product type, supported values include: `mysql` - cloud database MySQL, `cynosdb` - cloud database CynosDB for MySQL, the default is `mysql`.",
53+
},
54+
55+
"diag_item": {
56+
Computed: true,
57+
Type: schema.TypeString,
58+
Description: "diagnostic item.",
59+
},
60+
61+
"diag_type": {
62+
Computed: true,
63+
Type: schema.TypeString,
64+
Description: "Diagnostic type.",
65+
},
66+
67+
"explanation": {
68+
Computed: true,
69+
Type: schema.TypeString,
70+
Description: "Diagnostic event details, output is empty if there is no additional explanatory information.",
71+
},
72+
73+
"outline": {
74+
Computed: true,
75+
Type: schema.TypeString,
76+
Description: "Diagnostic summary.",
77+
},
78+
79+
"problem": {
80+
Computed: true,
81+
Type: schema.TypeString,
82+
Description: "Diagnosed problem.",
83+
},
84+
85+
"severity": {
86+
Computed: true,
87+
Type: schema.TypeInt,
88+
Description: "severity. The severity is divided into 5 levels, according to the degree of impact from high to low: 1: Fatal, 2: Serious, 3: Warning, 4: Prompt, 5: Healthy.",
89+
},
90+
91+
"start_time": {
92+
Computed: true,
93+
Type: schema.TypeString,
94+
Description: "Starting time.",
95+
},
96+
97+
"suggestions": {
98+
Computed: true,
99+
Type: schema.TypeString,
100+
Description: "A diagnostic suggestion, or empty if there is no suggestion.",
101+
},
102+
103+
"metric": {
104+
Computed: true,
105+
Type: schema.TypeString,
106+
Description: "reserved text. Note: This field may return null, indicating that no valid value can be obtained.",
107+
},
108+
109+
"end_time": {
110+
Computed: true,
111+
Type: schema.TypeString,
112+
Description: "End Time.",
113+
},
114+
115+
"result_output_file": {
116+
Type: schema.TypeString,
117+
Optional: true,
118+
Description: "Used to save results.",
119+
},
120+
},
121+
}
122+
}
123+
124+
func dataSourceTencentCloudDbbrainDiagEventRead(d *schema.ResourceData, meta interface{}) error {
125+
defer logElapsed("data_source.tencentcloud_dbbrain_diag_event.read")()
126+
defer inconsistentCheck(d, meta)()
127+
128+
logId := getLogId(contextNil)
129+
130+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
131+
var id string
132+
133+
paramMap := make(map[string]interface{})
134+
if v, ok := d.GetOk("instance_id"); ok {
135+
paramMap["instance_id"] = helper.String(v.(string))
136+
id = v.(string)
137+
}
138+
139+
if v, _ := d.GetOk("event_id"); v != nil {
140+
paramMap["event_id"] = helper.IntInt64(v.(int))
141+
}
142+
143+
if v, ok := d.GetOk("product"); ok {
144+
paramMap["product"] = helper.String(v.(string))
145+
}
146+
147+
var result *dbbrain.DescribeDBDiagEventResponseParams
148+
service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn}
149+
150+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
151+
var e error
152+
result, e = service.DescribeDbbrainDiagEventByFilter(ctx, paramMap)
153+
if e != nil {
154+
return retryError(e)
155+
}
156+
return nil
157+
})
158+
if err != nil {
159+
return err
160+
}
161+
162+
if result != nil {
163+
if result.DiagItem != nil {
164+
_ = d.Set("diag_item", result.DiagItem)
165+
}
166+
167+
if result.DiagType != nil {
168+
_ = d.Set("diag_type", result.DiagType)
169+
}
170+
171+
if result.EventId != nil {
172+
_ = d.Set("event_id", result.EventId)
173+
}
174+
175+
if result.Explanation != nil {
176+
_ = d.Set("explanation", result.Explanation)
177+
}
178+
179+
if result.Outline != nil {
180+
_ = d.Set("outline", result.Outline)
181+
}
182+
183+
if result.Problem != nil {
184+
_ = d.Set("problem", result.Problem)
185+
}
186+
187+
if result.Severity != nil {
188+
_ = d.Set("severity", result.Severity)
189+
}
190+
191+
if result.StartTime != nil {
192+
_ = d.Set("start_time", result.StartTime)
193+
}
194+
195+
if result.Suggestions != nil {
196+
_ = d.Set("suggestions", result.Suggestions)
197+
}
198+
199+
if result.Metric != nil {
200+
_ = d.Set("metric", result.Metric)
201+
}
202+
203+
if result.EndTime != nil {
204+
_ = d.Set("end_time", result.EndTime)
205+
}
206+
207+
}
208+
209+
d.SetId(id)
210+
output, ok := d.GetOk("result_output_file")
211+
if ok && output.(string) != "" {
212+
if e := writeToFile(output.(string), result); e != nil {
213+
return e
214+
}
215+
}
216+
return nil
217+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudDbbrainDiagEventDataSource_basic(t *testing.T) {
12+
t.Parallel()
13+
loc, _ := time.LoadLocation("Asia/Chongqing")
14+
startTime := time.Now().AddDate(0, 0, -1).In(loc).Format("2006-01-02 15:04:05")
15+
endTime := time.Now().In(loc).Format("2006-01-02 15:04:05")
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() {
18+
testAccPreCheck(t)
19+
},
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: fmt.Sprintf(testAccDbbrainDiagEventDataSource, defaultDbBrainInstanceId, startTime, endTime, defaultDbBrainInstanceId),
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dbbrain_diag_event.diag_event"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "diag_item"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "diag_type"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "explanation"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "outline"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "problem"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "severity"),
32+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "start_time"),
33+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "suggestions"),
34+
resource.TestCheckResourceAttrSet("data.tencentcloud_dbbrain_diag_event.diag_event", "end_time"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
const testAccDbbrainDiagEventDataSource = `
42+
43+
data "tencentcloud_dbbrain_diag_history" "diag_history" {
44+
instance_id = "%s"
45+
start_time = "%s"
46+
end_time = "%s"
47+
product = "mysql"
48+
}
49+
50+
data "tencentcloud_dbbrain_diag_event" "diag_event" {
51+
instance_id = "%s"
52+
event_id = data.tencentcloud_dbbrain_diag_history.diag_history.events.0.event_id
53+
product = "mysql"
54+
}
55+
56+
`

0 commit comments

Comments
 (0)