-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_source_tc_waf_peak_points.go
267 lines (231 loc) · 7.89 KB
/
data_source_tc_waf_peak_points.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
package waf
import (
"context"
"strconv"
"time"
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"
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudWafPeakPoints() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudWafPeakPointsRead,
Schema: map[string]*schema.Schema{
"from_time": {
Required: true,
Type: schema.TypeString,
Description: "Begin time.",
},
"to_time": {
Required: true,
Type: schema.TypeString,
Description: "End time.",
},
"domain": {
Optional: true,
Type: schema.TypeString,
Description: "The domain name to be queried. If all domain name data is queried, this parameter is not filled in.",
},
"edition": {
Optional: true,
Type: schema.TypeString,
Description: "Only support sparta-waf and clb-waf. If not passed, there will be no filtering.",
},
"instance_id": {
Optional: true,
Type: schema.TypeString,
Description: "WAF instance ID, if not passed, there will be no filtering.",
},
"metric_name": {
Optional: true,
Type: schema.TypeString,
ValidateFunc: tccommon.ValidateAllowedStringValue(MetricNameList),
Description: "Thirteen values are available: access-Peak qps trend chart; botAccess- bot peak qps trend chart; down-Downstream peak bandwidth trend chart; up-Upstream peak bandwidth trend chart; attack-Trend chart of total number of web attacks; cc-Trend chart of total number of CC attacks; StatusServerError-Trend chart of the number of status codes returned by WAF to the server; StatusClientError-Trend chart of the number of status codes returned by WAF to the client; StatusRedirect-Trend chart of the number of status codes returned by WAF to the client; StatusOk-Trend chart of the number of status codes returned by WAF to the client; UpstreamServerError-Trend chart of the number of status codes returned to WAF by the origin site; UpstreamClientError-Trend chart of the number of status codes returned to WAF by the origin site; UpstreamRedirect-Trend chart of the number of status codes returned to WAF by the origin site.",
},
"points": {
Computed: true,
Type: schema.TypeList,
Description: "point list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"time": {
Type: schema.TypeInt,
Computed: true,
Description: "Second level timestamp.",
},
"access": {
Type: schema.TypeInt,
Computed: true,
Description: "qps.",
},
"bot_access": {
Type: schema.TypeInt,
Computed: true,
Description: "Bot qps.",
},
"attack": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of web attacks.",
},
"cc": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of cc attacks.",
},
"down": {
Type: schema.TypeInt,
Computed: true,
Description: "Peak downlink bandwidth, unit B.",
},
"up": {
Type: schema.TypeInt,
Computed: true,
Description: "Peak uplink bandwidth, unit B.",
},
"status_server_error": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned by WAF to the server.",
},
"status_client_error": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned by WAF to the client.",
},
"status_redirect": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned by WAF to the client.",
},
"status_ok": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned by WAF to the client.",
},
"upstream_server_error": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned to WAF by the origin site.",
},
"upstream_client_error": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned to WAF by the origin site.",
},
"upstream_redirect": {
Type: schema.TypeInt,
Computed: true,
Description: "Trend chart of the number of status codes returned to WAF by the origin site.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudWafPeakPointsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_waf_peak_points.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = WafService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
points []*waf.PeakPointsItem
)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("from_time"); ok {
paramMap["FromTime"] = helper.String(v.(string))
}
if v, ok := d.GetOk("to_time"); ok {
paramMap["ToTime"] = helper.String(v.(string))
}
if v, ok := d.GetOk("domain"); ok {
paramMap["Domain"] = helper.String(v.(string))
}
if v, ok := d.GetOk("edition"); ok {
paramMap["Edition"] = helper.String(v.(string))
}
if v, ok := d.GetOk("instance_id"); ok {
paramMap["InstanceID"] = helper.String(v.(string))
}
if v, ok := d.GetOk("metric_name"); ok {
paramMap["MetricName"] = helper.String(v.(string))
}
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeWafPeakPointsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
points = result
return nil
})
if err != nil {
return err
}
tmpList := make([]map[string]interface{}, 0, len(points))
if points != nil {
for _, point := range points {
dMap := map[string]interface{}{}
if point.Time != nil {
dMap["time"] = point.Time
}
if point.Access != nil {
dMap["access"] = point.Access
}
if point.BotAccess != nil {
dMap["bot_access"] = point.BotAccess
}
if point.Attack != nil {
dMap["attack"] = point.Attack
}
if point.Cc != nil {
dMap["cc"] = point.Cc
}
if point.Down != nil {
dMap["down"] = point.Down
}
if point.Up != nil {
dMap["up"] = point.Up
}
if point.StatusServerError != nil {
dMap["status_server_error"] = point.StatusServerError
}
if point.StatusClientError != nil {
dMap["status_client_error"] = point.StatusClientError
}
if point.StatusRedirect != nil {
dMap["status_redirect"] = point.StatusRedirect
}
if point.StatusOk != nil {
dMap["status_ok"] = point.StatusOk
}
if point.UpstreamServerError != nil {
dMap["upstream_server_error"] = point.UpstreamServerError
}
if point.UpstreamClientError != nil {
dMap["upstream_client_error"] = point.UpstreamClientError
}
if point.UpstreamRedirect != nil {
dMap["upstream_redirect"] = point.UpstreamRedirect
}
tmpList = append(tmpList, dMap)
}
_ = d.Set("points", tmpList)
}
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}