-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_dbbrain_slow_log_user_host_stats.go
162 lines (132 loc) · 4.29 KB
/
data_source_tc_dbbrain_slow_log_user_host_stats.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
package dbbrain
import (
"context"
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 DataSourceTencentCloudDbbrainSlowLogUserHostStats() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDbbrainSlowLogUserHostStatsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
Type: schema.TypeString,
Description: "instance id.",
},
"start_time": {
Required: true,
Type: schema.TypeString,
Description: "Start time of the query range, time format such as: 2019-09-10 12:13:14.",
},
"end_time": {
Required: true,
Type: schema.TypeString,
Description: "EndTime time of the query range, time format such as: 2019-09-10 12:13:14.",
},
"product": {
Optional: true,
Type: schema.TypeString,
Description: "Types of service products, supported values:`mysql` - Cloud Database MySQL; `cynosdb` - Cloud Database TDSQL-C for MySQL, defaults to `mysql`.",
},
"md5": {
Optional: true,
Type: schema.TypeString,
Description: "MD5 value of SOL template.",
},
"items": {
Computed: true,
Type: schema.TypeList,
Description: "Detailed list of the slow log proportion for each source address.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_host": {
Type: schema.TypeString,
Computed: true,
Description: "source address.",
},
"ratio": {
Type: schema.TypeFloat,
Computed: true,
Description: "The ratio of the number of slow logs of the source address to the total, in %.",
},
"count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of slow logs for this source address.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDbbrainSlowLogUserHostStatsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dbbrain_slow_log_user_host_stats.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var id string
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("instance_id"); ok {
paramMap["instance_id"] = helper.String(v.(string))
id = v.(string)
}
if v, ok := d.GetOk("start_time"); ok {
paramMap["start_time"] = helper.String(v.(string))
}
if v, ok := d.GetOk("end_time"); ok {
paramMap["end_time"] = helper.String(v.(string))
}
if v, ok := d.GetOk("product"); ok {
paramMap["product"] = helper.String(v.(string))
}
if v, ok := d.GetOk("md5"); ok {
paramMap["md5"] = helper.String(v.(string))
}
service := DbbrainService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var items []*dbbrain.SlowLogHost
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeDbbrainSlowLogUserHostStatsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
items = result
return nil
})
if err != nil {
return err
}
tmpList := make([]map[string]interface{}, 0, len(items))
if items != nil {
for _, slowLogHost := range items {
slowLogHostMap := map[string]interface{}{}
if slowLogHost.UserHost != nil {
slowLogHostMap["user_host"] = slowLogHost.UserHost
}
if slowLogHost.Ratio != nil {
slowLogHostMap["ratio"] = slowLogHost.Ratio
}
if slowLogHost.Count != nil {
slowLogHostMap["count"] = slowLogHost.Count
}
tmpList = append(tmpList, slowLogHostMap)
}
_ = d.Set("items", tmpList)
}
d.SetId(id)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}