-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_clb_listener_rules.go
268 lines (255 loc) · 10.3 KB
/
data_source_tc_clb_listener_rules.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
package clb
import (
"context"
"log"
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"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudClbListenerRules() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudClbListenerRulesRead,
Schema: map[string]*schema.Schema{
"clb_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the CLB to be queried.",
},
"listener_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the CLB listener to be queried.",
},
"rule_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the forwarding rule to be queried.",
},
"domain": {
Type: schema.TypeString,
Optional: true,
Description: "Domain name of the forwarding rule to be queried.",
},
"url": {
Type: schema.TypeString,
Optional: true,
Description: "Url of the forwarding rule to be queried.",
},
"scheduler": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CLB_LISTENER_SCHEDULER),
Description: "Scheduling method of the forwarding rule of thr CLB listener, and available values include `WRR`, `IP HASH` and `LEAST_CONN`. The default is `WRR`.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"rule_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of forward rules of listeners. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"clb_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the CLB.",
},
"listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the listener.",
},
"domain": {
Type: schema.TypeString,
Optional: true,
Description: "Domain of the rule.",
},
"url": {
Type: schema.TypeString,
Optional: true,
Description: "Url of the rule.",
},
"rule_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the rule.",
},
"health_check_switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether health check is enabled.",
},
"health_check_interval_time": {
Type: schema.TypeInt,
Computed: true,
Description: "Interval time of health check. The value range is 2-300 sec, and the default is `5` sec. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"health_check_health_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Health threshold of health check, and the default is `3`. If a success result is returned for the health check three consecutive times, the CVM is identified as healthy. The value range is 2-10. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"health_check_unhealth_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Unhealth threshold of health check, and the default is `3`. If a success result is returned for the health check three consecutive times, the CVM is identified as unhealthy. The value range is 2-10. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"health_check_http_code": {
Type: schema.TypeInt,
Computed: true,
Description: "HTTP Status Code. The default is 31 and value range is 1-31. 1 means the return value '1xx' is health. 2 means the return value '2xx' is health. 4 means the return value '3xx' is health. 8 means the return value 4xx is health. 16 means the return value '5xx' is health. If you want multiple return codes to indicate health, need to add the corresponding values. NOTES: The 'HTTP' health check of the 'TCP' listener only supports specifying one health check status code. NOTES: Only supports listeners of 'HTTP' and 'HTTPS' protocol.",
},
"health_check_http_path": {
Type: schema.TypeString,
Computed: true,
Description: "Path of health check. NOTES: Only supports listeners of 'HTTPS' and 'HTTP' protocol.",
},
"health_check_http_domain": {
Type: schema.TypeString,
Computed: true,
Description: "Domain name of health check. NOTES: Only supports listeners of 'HTTPS' and 'HTTP' protocol.",
},
"health_check_http_method": {
Type: schema.TypeString,
Computed: true,
Description: "Methods of health check. NOTES: Only supports listeners of 'HTTPS' and 'HTTP' protocol. The default is 'HEAD', the available value include 'HEAD' and 'GET'.",
},
"certificate_ssl_mode": {
Type: schema.TypeString,
Computed: true,
Description: "Type of SSL Mode, and available values inclue 'UNIDIRECTIONAL', 'MUTUAL'.NOTES: Only supports listeners of 'HTTPS' and 'TCP_SSL' protocol.",
},
"certificate_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the server certificate. NOTES: Only supports listeners of 'HTTPS' and 'TCP_SSL' protocol.",
},
"certificate_ca_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the client certificate. NOTES: Only supports listeners of 'HTTPS' and 'TCP_SSL' protocol.",
},
"session_expire_time": {
Type: schema.TypeInt,
Computed: true,
Description: "Time of session persistence within the CLB listener. NOTES: Available when scheduler is specified as 'WRR'. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"scheduler": {
Type: schema.TypeString,
Computed: true,
Description: "Scheduling method of the CLB listener, and available values include 'WRR', 'IP_HASH' and 'LEAST_CONN'. The default is 'WRR'. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"http2_switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicate to set HTTP2 protocol or not.",
},
},
},
},
},
}
}
func dataSourceTencentCloudClbListenerRulesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_clb_listener_rules.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
listenerId := d.Get("listener_id").(string)
checkErr := ListenerIdCheck(listenerId)
if checkErr != nil {
return checkErr
}
clbId := d.Get("clb_id").(string)
params := make(map[string]string)
params["clb_id"] = clbId
params["listener_id"] = listenerId
if v, ok := d.GetOk("clb_id"); ok {
params["clb_id"] = v.(string)
}
if v, ok := d.GetOk("scheduler"); ok {
params["scheduler"] = v.(string)
}
if v, ok := d.GetOk("rule_id"); ok {
params["rule_id"] = v.(string)
checkErr := RuleIdCheck(params["rule_id"])
if checkErr != nil {
return checkErr
}
}
if v, ok := d.GetOk("domain"); ok {
params["domain"] = v.(string)
}
if v, ok := d.GetOk("url"); ok {
params["url"] = v.(string)
}
clbService := ClbService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
var rules []*clb.RuleOutput
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
results, e := clbService.DescribeRulesByFilter(ctx, params)
if e != nil {
return tccommon.RetryError(e)
}
rules = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CLB listener rules failed, reason:%+v", logId, err)
return err
}
ruleList := make([]map[string]interface{}, 0, len(rules))
ids := make([]string, 0, len(rules))
for _, rule := range rules {
mapping := map[string]interface{}{
"clb_id": clbId,
"listener_id": listenerId,
"rule_id": rule.LocationId,
"domain": rule.Domain,
"url": rule.Url,
"session_expire_time": rule.SessionExpireTime,
"scheduler": rule.Scheduler,
"http2_switch": rule.Http2,
}
if rule.HealthCheck != nil {
healthCheckSwitch := false
if *rule.HealthCheck.HealthSwitch == int64(1) {
healthCheckSwitch = true
}
mapping["health_check_switch"] = healthCheckSwitch
mapping["health_check_interval_time"] = *rule.HealthCheck.IntervalTime
mapping["health_check_health_num"] = *rule.HealthCheck.HealthNum
mapping["health_check_unhealth_num"] = *rule.HealthCheck.UnHealthNum
mapping["health_check_http_code"] = *rule.HealthCheck.HttpCode
mapping["health_check_http_method"] = *rule.HealthCheck.HttpCheckMethod
mapping["health_check_http_domain"] = *rule.HealthCheck.HttpCheckDomain
mapping["health_check_http_path"] = *rule.HealthCheck.HttpCheckPath
}
if rule.Certificate != nil {
mapping["certificate_ssl_mode"] = *rule.Certificate.SSLMode
mapping["certificate_id"] = *rule.Certificate.CertId
if rule.Certificate.CertCaId != nil {
mapping["certificate_ca_id"] = *rule.Certificate.CertCaId
}
}
ruleList = append(ruleList, mapping)
ids = append(ids, *rule.LocationId)
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("rule_list", ruleList); e != nil {
log.Printf("[CRITAL]%s provider set CLB listener rule list fail, reason:%+v", logId, e)
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), ruleList); e != nil {
return e
}
}
return nil
}