-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_dbbrain_top_space_tables.go
231 lines (194 loc) · 6.64 KB
/
data_source_tc_dbbrain_top_space_tables.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
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 DataSourceTencentCloudDbbrainTopSpaceTables() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDbbrainTopSpaceTablesRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
Type: schema.TypeString,
Description: "instance id.",
},
"limit": {
Optional: true,
Type: schema.TypeInt,
Default: 20,
Description: "The number of Top tables returned, the maximum value is 100, and the default is 20.",
},
"sort_by": {
Optional: true,
Type: schema.TypeString,
Description: "The sorting field used to filter the Top table. The optional fields include DataLength, IndexLength, TotalLength, DataFree, FragRatio, TableRows, and PhysicalFileSize (only supported by ApsaraDB for MySQL instances). The default for ApsaraDB for MySQL instances is PhysicalFileSize, and the default for other product instances is TotalLength.",
},
"product": {
Optional: 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.",
},
"top_space_tables": {
Computed: true,
Type: schema.TypeList,
Description: "The list of Top tablespace statistics returned.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"table_name": {
Type: schema.TypeString,
Computed: true,
Description: "table name.",
},
"table_schema": {
Type: schema.TypeString,
Computed: true,
Description: "database name.",
},
"engine": {
Type: schema.TypeString,
Computed: true,
Description: "Storage engine for database tables.",
},
"data_length": {
Type: schema.TypeFloat,
Computed: true,
Description: "data space (MB).",
},
"index_length": {
Type: schema.TypeFloat,
Computed: true,
Description: "Index space (MB).",
},
"data_free": {
Type: schema.TypeFloat,
Computed: true,
Description: "Fragmentation space (MB).",
},
"total_length": {
Type: schema.TypeFloat,
Computed: true,
Description: "Total space used (MB).",
},
"frag_ratio": {
Type: schema.TypeFloat,
Computed: true,
Description: "Fragmentation rate (%).",
},
"table_rows": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of lines.",
},
"physical_file_size": {
Type: schema.TypeFloat,
Computed: true,
Description: "The independent physical file size (MB) corresponding to the table.",
},
},
},
},
"timestamp": {
Computed: true,
Type: schema.TypeInt,
Description: "The timestamp (in seconds) of collecting tablespace data.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDbbrainTopSpaceTablesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dbbrain_top_space_tables.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, _ := d.GetOk("limit"); v != nil {
paramMap["Limit"] = helper.IntInt64(v.(int))
}
if v, ok := d.GetOk("sort_by"); ok {
paramMap["SortBy"] = 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 topSpaceTables []*dbbrain.TableSpaceData
var timestamp *int64
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, ts, e := service.DescribeDbbrainTopSpaceTablesByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
topSpaceTables = result
timestamp = ts
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(topSpaceTables))
tmpList := make([]map[string]interface{}, 0, len(topSpaceTables))
if topSpaceTables != nil {
for _, tableSpaceData := range topSpaceTables {
tableSpaceDataMap := map[string]interface{}{}
if tableSpaceData.TableName != nil {
tableSpaceDataMap["table_name"] = tableSpaceData.TableName
}
if tableSpaceData.TableSchema != nil {
tableSpaceDataMap["table_schema"] = tableSpaceData.TableSchema
}
if tableSpaceData.Engine != nil {
tableSpaceDataMap["engine"] = tableSpaceData.Engine
}
if tableSpaceData.DataLength != nil {
tableSpaceDataMap["data_length"] = tableSpaceData.DataLength
}
if tableSpaceData.IndexLength != nil {
tableSpaceDataMap["index_length"] = tableSpaceData.IndexLength
}
if tableSpaceData.DataFree != nil {
tableSpaceDataMap["data_free"] = tableSpaceData.DataFree
}
if tableSpaceData.TotalLength != nil {
tableSpaceDataMap["total_length"] = tableSpaceData.TotalLength
}
if tableSpaceData.FragRatio != nil {
tableSpaceDataMap["frag_ratio"] = tableSpaceData.FragRatio
}
if tableSpaceData.TableRows != nil {
tableSpaceDataMap["table_rows"] = tableSpaceData.TableRows
}
if tableSpaceData.PhysicalFileSize != nil {
tableSpaceDataMap["physical_file_size"] = tableSpaceData.PhysicalFileSize
}
ids = append(ids, strings.Join([]string{instanceId, *tableSpaceData.TableSchema, *tableSpaceData.TableName}, tccommon.FILED_SP))
tmpList = append(tmpList, tableSpaceDataMap)
}
_ = d.Set("top_space_tables", tmpList)
}
if timestamp != nil {
_ = d.Set("timestamp", timestamp)
}
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
}