-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_waf_domains.go
538 lines (471 loc) · 16.5 KB
/
data_source_tc_waf_domains.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package waf
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"
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudWafDomains() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudWafDomainsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Optional: true,
Type: schema.TypeString,
Description: "Unique ID of Instance.",
},
"domain": {
Optional: true,
Type: schema.TypeString,
Description: "Domain name.",
},
"domains": {
Computed: true,
Type: schema.TypeList,
Description: "Domain info list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Computed: true,
Description: "Domain name.",
},
"domain_id": {
Type: schema.TypeString,
Computed: true,
Description: "Domain unique ID.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance unique ID.",
},
"cname": {
Type: schema.TypeString,
Computed: true,
Description: "Cname address, used for dns access.",
},
"edition": {
Type: schema.TypeString,
Computed: true,
Description: "Instance type, sparta-waf represents SAAS WAF, clb-waf represents CLB WAF.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "Region.",
},
"instance_name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance name.",
},
"cls_status": {
Type: schema.TypeInt,
Computed: true,
Description: "Whether to enable access logs, 1 enable, 0 disable.",
},
"flow_mode": {
Type: schema.TypeInt,
Computed: true,
Description: "CLBWAF traffic mode, 1 cleaning mode, 0 mirroring mode.",
},
"status": {
Type: schema.TypeInt,
Computed: true,
Description: "Waf switch,0 off 1 on.",
},
"mode": {
Type: schema.TypeInt,
Computed: true,
Description: "Rule defense mode, 0 observation mode, 1 interception mode.",
},
"engine": {
Type: schema.TypeInt,
Computed: true,
Description: "Rule and AI Defense Mode, 10 Rule Engine Observation&&AI Engine Shutdown Mode 11 Rule Engine Observation&&AI Engine Observation Mode 12 Rule Engine Observation&&AI Engine Interception Mode 20 Rule Engine Interception&&AI Engine Shutdown Mode 21 Rule Engine Interception&&AI Engine Observation Mode 22 Rule Engine Interception&&AI Engine Interception Mode.",
},
"cc_list": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "Waf sandbox export addresses, should be added to the whitelist by the upstreams.",
},
"rs_list": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "Waf engine export addresses, should be added to the whitelist by the upstreams.",
},
"ports": {
Type: schema.TypeList,
Computed: true,
Description: "Listening ports.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nginx_server_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Nginx server ID.",
},
"port": {
Type: schema.TypeString,
Computed: true,
Description: "Listening port.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "The listening protocol of listening port.",
},
"upstream_port": {
Type: schema.TypeString,
Computed: true,
Description: "The upstream port for listening port.",
},
"upstream_protocol": {
Type: schema.TypeString,
Computed: true,
Description: "The upstream protocol for listening port.",
},
},
},
},
"load_balancer_set": {
Type: schema.TypeList,
Computed: true,
Description: "List of bound LB.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "Listener unique IDNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"listener_name": {
Type: schema.TypeString,
Computed: true,
Description: "Listener nameNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"load_balancer_id": {
Type: schema.TypeString,
Computed: true,
Description: "LoadBalancer IDNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"load_balancer_name": {
Type: schema.TypeString,
Computed: true,
Description: "LoadBalancer nameNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "Listener protocolNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "RegionNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"vip": {
Type: schema.TypeString,
Computed: true,
Description: "LoadBalancer ipNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"vport": {
Type: schema.TypeInt,
Computed: true,
Description: "Listener portNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "Loadbalancer zoneNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"numerical_vpc_id": {
Type: schema.TypeInt,
Computed: true,
Description: "VPCID for load balancer, public network is -1, and internal network is filled in according to actual conditionsNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"load_balancer_type": {
Type: schema.TypeString,
Computed: true,
Description: "Loadbalancer typeNote: This field may return null, indicating that a valid value cannot be obtained.",
},
},
},
},
"app_id": {
Type: schema.TypeInt,
Computed: true,
Description: "User appid.",
},
"state": {
Type: schema.TypeInt,
Computed: true,
Description: "Clbwaf domain name listener status, 0 operation successful, 4 binding LB, 6 unbinding LB, 7 unbinding LB failed, 8 binding LB failed, 10 internal error.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Create time.",
},
"ipv6_status": {
Type: schema.TypeInt,
Computed: true,
Description: "Ipv6 switch status, 0 off, 1 on.",
},
"bot_status": {
Type: schema.TypeInt,
Computed: true,
Description: "BOT switch status, 0 off, 1 on.",
},
"level": {
Type: schema.TypeInt,
Computed: true,
Description: "Instance level.",
},
"post_cls_status": {
Type: schema.TypeInt,
Computed: true,
Description: "Whether to enable the delivery CLS function, 0 off, 1 on.",
},
"post_ckafka_status": {
Type: schema.TypeInt,
Computed: true,
Description: "Whether to enable the delivery of CKafka function, 0 off, 1 on.",
},
"cdc_clusters": {
Type: schema.TypeString,
Computed: true,
Description: "Cdc clustersNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"api_status": {
Type: schema.TypeInt,
Computed: true,
Description: "API security switch status, 0 off, 1 onNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"alb_type": {
Type: schema.TypeString,
Computed: true,
Description: "Traffic Source: clb represents Tencent Cloud clb, apisix represents apisix gateway, tsegw represents Tencent Cloud API gateway, default clbNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"sg_state": {
Type: schema.TypeInt,
Computed: true,
Description: "Security group status, 0 does not display, 1 non Tencent cloud source site, 2 security group binding failed, 3 security group changedNote: This field may return null, indicating that a valid value cannot be obtained.",
},
"sg_detail": {
Type: schema.TypeString,
Computed: true,
Description: "Detailed explanation of security group statusNote: This field may return null, indicating that a valid value cannot be obtained.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudWafDomainsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_waf_domains.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()}
domains []*waf.DomainInfo
instanceID string
domain string
)
if v, ok := d.GetOk("instance_id"); ok {
instanceID = v.(string)
}
if v, ok := d.GetOk("domain"); ok {
domain = v.(string)
}
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeWafDomainsByFilter(ctx, instanceID, domain)
if e != nil {
return tccommon.RetryError(e)
}
domains = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(domains))
tmpList := make([]map[string]interface{}, 0, len(domains))
if domains != nil {
for _, domainInfo := range domains {
domainInfoMap := map[string]interface{}{}
if domainInfo.Domain != nil {
domainInfoMap["domain"] = domainInfo.Domain
}
if domainInfo.DomainId != nil {
domainInfoMap["domain_id"] = domainInfo.DomainId
}
if domainInfo.InstanceId != nil {
domainInfoMap["instance_id"] = domainInfo.InstanceId
}
if domainInfo.Cname != nil {
domainInfoMap["cname"] = domainInfo.Cname
}
if domainInfo.Edition != nil {
domainInfoMap["edition"] = domainInfo.Edition
}
if domainInfo.Region != nil {
domainInfoMap["region"] = domainInfo.Region
}
if domainInfo.InstanceName != nil {
domainInfoMap["instance_name"] = domainInfo.InstanceName
}
if domainInfo.ClsStatus != nil {
domainInfoMap["cls_status"] = domainInfo.ClsStatus
}
if domainInfo.FlowMode != nil {
domainInfoMap["flow_mode"] = domainInfo.FlowMode
}
if domainInfo.Status != nil {
domainInfoMap["status"] = domainInfo.Status
}
if domainInfo.Mode != nil {
domainInfoMap["mode"] = domainInfo.Mode
}
if domainInfo.Engine != nil {
domainInfoMap["engine"] = domainInfo.Engine
}
if domainInfo.CCList != nil {
domainInfoMap["cc_list"] = domainInfo.CCList
}
if domainInfo.RsList != nil {
domainInfoMap["rs_list"] = domainInfo.RsList
}
if domainInfo.Ports != nil {
portsList := []interface{}{}
for _, ports := range domainInfo.Ports {
portsMap := map[string]interface{}{}
if ports.NginxServerId != nil {
portsMap["nginx_server_id"] = ports.NginxServerId
}
if ports.Port != nil {
portsMap["port"] = ports.Port
}
if ports.Protocol != nil {
portsMap["protocol"] = ports.Protocol
}
if ports.UpstreamPort != nil {
portsMap["upstream_port"] = ports.UpstreamPort
}
if ports.UpstreamProtocol != nil {
portsMap["upstream_protocol"] = ports.UpstreamProtocol
}
portsList = append(portsList, portsMap)
}
domainInfoMap["ports"] = portsList
}
if domainInfo.LoadBalancerSet != nil {
loadBalancerSetList := []interface{}{}
for _, loadBalancerSet := range domainInfo.LoadBalancerSet {
loadBalancerSetMap := map[string]interface{}{}
if loadBalancerSet.ListenerId != nil {
loadBalancerSetMap["listener_id"] = loadBalancerSet.ListenerId
}
if loadBalancerSet.ListenerName != nil {
loadBalancerSetMap["listener_name"] = loadBalancerSet.ListenerName
}
if loadBalancerSet.LoadBalancerId != nil {
loadBalancerSetMap["load_balancer_id"] = loadBalancerSet.LoadBalancerId
}
if loadBalancerSet.LoadBalancerName != nil {
loadBalancerSetMap["load_balancer_name"] = loadBalancerSet.LoadBalancerName
}
if loadBalancerSet.Protocol != nil {
loadBalancerSetMap["protocol"] = loadBalancerSet.Protocol
}
if loadBalancerSet.Region != nil {
loadBalancerSetMap["region"] = loadBalancerSet.Region
}
if loadBalancerSet.Vip != nil {
loadBalancerSetMap["vip"] = loadBalancerSet.Vip
}
if loadBalancerSet.Vport != nil {
loadBalancerSetMap["vport"] = loadBalancerSet.Vport
}
if loadBalancerSet.Zone != nil {
loadBalancerSetMap["zone"] = loadBalancerSet.Zone
}
if loadBalancerSet.NumericalVpcId != nil {
loadBalancerSetMap["numerical_vpc_id"] = loadBalancerSet.NumericalVpcId
}
if loadBalancerSet.LoadBalancerType != nil {
loadBalancerSetMap["load_balancer_type"] = loadBalancerSet.LoadBalancerType
}
loadBalancerSetList = append(loadBalancerSetList, loadBalancerSetMap)
}
domainInfoMap["load_balancer_set"] = loadBalancerSetList
}
if domainInfo.AppId != nil {
domainInfoMap["app_id"] = domainInfo.AppId
}
if domainInfo.State != nil {
domainInfoMap["state"] = domainInfo.State
}
if domainInfo.CreateTime != nil {
domainInfoMap["create_time"] = domainInfo.CreateTime
}
if domainInfo.Ipv6Status != nil {
domainInfoMap["ipv6_status"] = domainInfo.Ipv6Status
}
if domainInfo.BotStatus != nil {
domainInfoMap["bot_status"] = domainInfo.BotStatus
}
if domainInfo.Level != nil {
domainInfoMap["level"] = domainInfo.Level
}
if domainInfo.PostCLSStatus != nil {
domainInfoMap["post_cls_status"] = domainInfo.PostCLSStatus
}
if domainInfo.PostCKafkaStatus != nil {
domainInfoMap["post_ckafka_status"] = domainInfo.PostCKafkaStatus
}
if domainInfo.CdcClusters != nil {
domainInfoMap["cdc_clusters"] = domainInfo.CdcClusters
}
if domainInfo.ApiStatus != nil {
domainInfoMap["api_status"] = domainInfo.ApiStatus
}
if domainInfo.AlbType != nil {
domainInfoMap["alb_type"] = domainInfo.AlbType
}
if domainInfo.SgState != nil {
domainInfoMap["sg_state"] = domainInfo.SgState
}
if domainInfo.SgDetail != nil {
domainInfoMap["sg_detail"] = domainInfo.SgDetail
}
ids = append(ids, *domainInfo.DomainId)
tmpList = append(tmpList, domainInfoMap)
}
_ = d.Set("domains", 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
}