-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_dbbrain_slow_logs.go
302 lines (256 loc) · 7.96 KB
/
data_source_tc_dbbrain_slow_logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package dbbrain
import (
"context"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudDbbrainSlowLogs() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDbbrainSlowLogsRead,
Schema: map[string]*schema.Schema{
"product": {
Required: true,
Type: schema.TypeString,
Description: "Service product type, supported values include: mysql - cloud database MySQL, cynosdb - cloud database CynosDB for MySQL, the default is mysql.",
},
"instance_id": {
Required: true,
Type: schema.TypeString,
Description: "instance Id.",
},
"md5": {
Required: true,
Type: schema.TypeString,
Description: "md5 value of sql template.",
},
"start_time": {
Required: true,
Type: schema.TypeString,
Description: "Start time, such as 2019-09-10 12:13:14.",
},
"end_time": {
Required: true,
Type: schema.TypeString,
Description: "The deadline, such as 2019-09-11 10:13:14, the interval between the deadline and the start time is less than 7 days.",
},
"db": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "database list.",
},
"key": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "keywords.",
},
"user": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "user.",
},
"ip": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "ip.",
},
"time": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "Time-consuming interval, the left and right boundaries of the time-consuming interval correspond to the 0th element and the first element of the array respectively.",
},
"rows": {
Computed: true,
Type: schema.TypeList,
Description: "Slow log details.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"timestamp": {
Type: schema.TypeString,
Computed: true,
Description: "Slow log start time.",
},
"sql_text": {
Type: schema.TypeString,
Computed: true,
Description: "sql statement.",
},
"database": {
Type: schema.TypeString,
Computed: true,
Description: "database.",
},
"user_name": {
Type: schema.TypeString,
Computed: true,
Description: "User sourceNote: This field may return null, indicating that no valid value can be obtained.",
},
"user_host": {
Type: schema.TypeString,
Computed: true,
Description: "Ip sourceNote: This field may return null, indicating that no valid value can be obtained.",
},
"query_time": {
Type: schema.TypeInt,
Computed: true,
Description: "Execution time, in seconds.",
},
"lock_time": {
Type: schema.TypeInt,
Computed: true,
Description: "lock time, in secondsNote: This field may return null, indicating that no valid value can be obtained.",
},
"rows_examined": {
Type: schema.TypeInt,
Computed: true,
Description: "scan linesNote: This field may return null, indicating that no valid value can be obtained.",
},
"rows_sent": {
Type: schema.TypeInt,
Computed: true,
Description: "Return the number of rowsNote: This field may return null, indicating that no valid value can be obtained.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDbbrainSlowLogsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dbbrain_slow_logs.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var (
md5 string
instanceId string
product string
)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("product"); ok {
paramMap["Product"] = helper.String(v.(string))
product = v.(string)
}
if v, ok := d.GetOk("instance_id"); ok {
paramMap["InstanceId"] = helper.String(v.(string))
instanceId = v.(string)
}
if v, ok := d.GetOk("md5"); ok {
paramMap["Md5"] = helper.String(v.(string))
md5 = v.(string)
}
if v, ok := d.GetOk("start_time"); ok {
paramMap["StartTime"] = helper.String(v.(string))
}
if v, ok := d.GetOk("end_time"); ok {
paramMap["EndTime"] = helper.String(v.(string))
}
if v, ok := d.GetOk("db"); ok {
dbSet := v.(*schema.Set).List()
paramMap["Db"] = helper.InterfacesStringsPoint(dbSet)
}
if v, ok := d.GetOk("key"); ok {
keySet := v.(*schema.Set).List()
paramMap["Key"] = helper.InterfacesStringsPoint(keySet)
}
if v, ok := d.GetOk("user"); ok {
userSet := v.(*schema.Set).List()
paramMap["User"] = helper.InterfacesStringsPoint(userSet)
}
if v, ok := d.GetOk("ip"); ok {
ipSet := v.(*schema.Set).List()
paramMap["Ip"] = helper.InterfacesStringsPoint(ipSet)
}
if v, ok := d.GetOk("time"); ok {
timeSet := v.(*schema.Set).List()
tmpList := []interface{}{}
for _, v := range timeSet {
if v != nil {
time := v.(int)
tmpList = append(tmpList, helper.IntInt64(time))
}
}
paramMap["Time"] = tmpList
}
service := DbbrainService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var rows []*dbbrain.SlowLogInfoItem
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeDbbrainSlowLogsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
rows = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(rows))
tmpList := make([]map[string]interface{}, 0, len(rows))
if rows != nil {
for _, slowLogInfoItem := range rows {
slowLogInfoItemMap := map[string]interface{}{}
if slowLogInfoItem.Timestamp != nil {
slowLogInfoItemMap["timestamp"] = slowLogInfoItem.Timestamp
}
if slowLogInfoItem.SqlText != nil {
slowLogInfoItemMap["sql_text"] = slowLogInfoItem.SqlText
}
if slowLogInfoItem.Database != nil {
slowLogInfoItemMap["database"] = slowLogInfoItem.Database
}
if slowLogInfoItem.UserName != nil {
slowLogInfoItemMap["user_name"] = slowLogInfoItem.UserName
}
if slowLogInfoItem.UserHost != nil {
slowLogInfoItemMap["user_host"] = slowLogInfoItem.UserHost
}
if slowLogInfoItem.QueryTime != nil {
slowLogInfoItemMap["query_time"] = slowLogInfoItem.QueryTime
}
if slowLogInfoItem.LockTime != nil {
slowLogInfoItemMap["lock_time"] = slowLogInfoItem.LockTime
}
if slowLogInfoItem.RowsExamined != nil {
slowLogInfoItemMap["rows_examined"] = slowLogInfoItem.RowsExamined
}
if slowLogInfoItem.RowsSent != nil {
slowLogInfoItemMap["rows_sent"] = slowLogInfoItem.RowsSent
}
ids = append(ids, strings.Join([]string{md5, instanceId, product}, tccommon.FILED_SP))
tmpList = append(tmpList, slowLogInfoItemMap)
}
_ = d.Set("rows", tmpList)
}
d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}