-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_dbbrain_mysql_process_list.go
262 lines (217 loc) · 6.88 KB
/
data_source_tc_dbbrain_mysql_process_list.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
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 DataSourceTencentCloudDbbrainMysqlProcessList() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDbbrainMysqlProcessListRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
Type: schema.TypeString,
Description: "instance id.",
},
"id": {
Optional: true,
Type: schema.TypeInt,
Description: "thread ID, used to filter the thread list.",
},
"user": {
Optional: true,
Type: schema.TypeString,
Description: "The operating account name of the thread, used to filter the thread list.",
},
"host": {
Optional: true,
Type: schema.TypeString,
Description: "The operating host address of the thread, used to filter the thread list.",
},
"db": {
Optional: true,
Type: schema.TypeString,
Description: "The threads operations database, used to filter the thread list.",
},
"state": {
Optional: true,
Type: schema.TypeString,
Description: "The operational state of the thread, used to filter the thread list.",
},
"command": {
Optional: true,
Type: schema.TypeString,
Description: "The execution type of the thread, used to filter the thread list.",
},
"time": {
Optional: true,
Type: schema.TypeInt,
Description: "The minimum value of the operation duration of a thread, in seconds, used to filter the list of threads whose operation duration is longer than this value.",
},
"info": {
Optional: true,
Type: schema.TypeString,
Description: "The threads operation statement is used to filter the thread list.",
},
"product": {
Optional: true,
Type: schema.TypeString,
Description: "Service product type, supported values: `mysql` - cloud database MySQL; `cynosdb` - cloud database TDSQL-C for MySQL, the default is `mysql`.",
},
"process_list": {
Computed: true,
Type: schema.TypeList,
Description: "Live thread list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "thread ID.",
},
"user": {
Type: schema.TypeString,
Computed: true,
Description: "The operating account name of the thread.",
},
"host": {
Type: schema.TypeString,
Computed: true,
Description: "The operating host address of the thread.",
},
"db": {
Type: schema.TypeString,
Computed: true,
Description: "The thread that operates the database.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The operational state of the thread.",
},
"command": {
Type: schema.TypeString,
Computed: true,
Description: "The execution type of the thread.",
},
"time": {
Type: schema.TypeString,
Computed: true,
Description: "The operation duration of the thread, in seconds.",
},
"info": {
Type: schema.TypeString,
Computed: true,
Description: "The operation statement for the thread.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDbbrainMysqlProcessListRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dbbrain_mysql_process_list.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var (
instanceId string
)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("instance_id"); ok {
paramMap["InstanceId"] = helper.String(v.(string))
instanceId = v.(string)
}
if v, ok := d.GetOkExists("id"); ok {
paramMap["ID"] = helper.IntUint64(v.(int))
}
if v, ok := d.GetOk("user"); ok {
paramMap["User"] = helper.String(v.(string))
}
if v, ok := d.GetOk("host"); ok {
paramMap["Host"] = helper.String(v.(string))
}
if v, ok := d.GetOk("db"); ok {
paramMap["DB"] = helper.String(v.(string))
}
if v, ok := d.GetOk("state"); ok {
paramMap["State"] = helper.String(v.(string))
}
if v, ok := d.GetOk("command"); ok {
paramMap["Command"] = helper.String(v.(string))
}
if v, ok := d.GetOkExists("time"); ok {
paramMap["Time"] = helper.IntUint64(v.(int))
}
if v, ok := d.GetOk("info"); ok {
paramMap["Info"] = helper.String(v.(string))
}
if v, ok := d.GetOk("product"); ok {
paramMap["Product"] = helper.String(v.(string))
}
service := DbbrainService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var processList []*dbbrain.MySqlProcess
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeDbbrainMysqlProcessListByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
processList = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(processList))
tmpList := make([]map[string]interface{}, 0, len(processList))
if processList != nil {
for _, mySqlProcess := range processList {
mySqlProcessMap := map[string]interface{}{}
if mySqlProcess.ID != nil {
mySqlProcessMap["id"] = mySqlProcess.ID
}
if mySqlProcess.User != nil {
mySqlProcessMap["user"] = mySqlProcess.User
}
if mySqlProcess.Host != nil {
mySqlProcessMap["host"] = mySqlProcess.Host
}
if mySqlProcess.DB != nil {
mySqlProcessMap["db"] = mySqlProcess.DB
}
if mySqlProcess.State != nil {
mySqlProcessMap["state"] = mySqlProcess.State
}
if mySqlProcess.Command != nil {
mySqlProcessMap["command"] = mySqlProcess.Command
}
if mySqlProcess.Time != nil {
mySqlProcessMap["time"] = mySqlProcess.Time
}
if mySqlProcess.Info != nil {
mySqlProcessMap["info"] = mySqlProcess.Info
}
ids = append(ids, strings.Join([]string{instanceId, *mySqlProcess.ID}, tccommon.FILED_SP))
tmpList = append(tmpList, mySqlProcessMap)
}
_ = d.Set("process_list", 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
}