-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_dayu_l7_rules.go
245 lines (227 loc) · 7.63 KB
/
data_source_tc_dayu_l7_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
package dayu
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"
dayu "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu/v20180709"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudDayuL7Rules() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDayuL7RulesRead,
Schema: map[string]*schema.Schema{
"resource_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(DAYU_RESOURCE_TYPE_HTTPS),
Description: "Type of the resource that the layer 7 rule works for, valid value is `bgpip`.",
},
"resource_id": {
Type: schema.TypeString,
Required: true,
Description: "Id of the resource that the layer 7 rule works for.",
},
"rule_id": {
Type: schema.TypeString,
Optional: true,
Description: "Id of the layer 7 rule to be queried.",
},
"domain": {
Type: schema.TypeString,
Optional: true,
Description: "Domain of the layer 7 rule to be queried.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of layer 7 rules. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Computed: true,
Description: "Domain that the 7 layer rule works for.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "Protocol of the rule.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the rule.",
},
"switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicate the rule will take effect or not.",
},
"source_type": {
Type: schema.TypeInt,
Computed: true,
Description: "Source type, 1 for source of host, 2 for source of ip.",
},
"source_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "Source ip or domain.",
},
Description: "Source list of the rule.",
},
"ssl_id": {
Type: schema.TypeString,
Computed: true,
Description: "SSL id.",
},
"health_check_switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether health check is enabled.",
},
"health_check_interval": {
Type: schema.TypeInt,
Computed: true,
Description: "Interval time of health check.",
},
"health_check_health_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Health threshold of health check.",
},
"health_check_unhealth_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Unhealthy threshold of health check.",
},
"health_check_code": {
Type: schema.TypeInt,
Computed: true,
Description: "HTTP Status Code. `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.",
},
"health_check_path": {
Type: schema.TypeString,
Computed: true,
Description: "Path of health check.",
},
"health_check_method": {
Type: schema.TypeString,
Computed: true,
Description: "Methods of health check.",
},
"rule_id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of the 7 layer rule.",
},
"status": {
Type: schema.TypeInt,
Computed: true,
Description: "Status of the rule. `0` for create/modify success, `2` for create/modify fail, `3` for delete success, `5` for waiting to be created/modified, `7` for waiting to be deleted and `8` for waiting to get SSL id.",
},
"threshold": {
Type: schema.TypeInt,
Computed: true,
Description: "Threshold of the rule.",
},
},
},
},
},
}
}
func dataSourceTencentCloudDayuL7RulesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dayu_l7_rules.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := DayuService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
resourceType := d.Get("resource_type").(string)
resourceId := d.Get("resource_id").(string)
ruleId := d.Get("rule_id").(string)
domain := d.Get("domain").(string)
protocol := ""
rules := make([]*dayu.L7RuleEntry, 0)
healths := make([]*dayu.L7RuleHealth, 0)
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, hResult, _, err := service.DescribeL7Rules(ctx, resourceType, resourceId, domain, ruleId, protocol)
if err != nil {
return tccommon.RetryError(err)
}
rules = result
healths = hResult
return nil
})
if err != nil {
return err
}
list := make([]map[string]interface{}, 0, len(rules))
ids := make([]string, 0, len(rules))
for k, rule := range rules {
listItem := make(map[string]interface{})
listItem["name"] = *rule.RuleName
listItem["domain"] = *rule.Domain
listItem["ssl_id"] = *rule.SSLId
listItem["rule_id"] = *rule.RuleId
listItem["protocol"] = *rule.Protocol
listItem["source_type"] = int(*rule.SourceType)
listItem["status"] = int(*rule.Status)
listItem["threshold"] = int(*rule.CCThreshold)
if *rule.Protocol == DAYU_L7_RULE_PROTOCOL_HTTPS {
listItem["switch"] = *rule.CCEnable > 0
} else {
listItem["switch"] = *rule.CCStatus > 0
}
sourceList := make([]*string, 0, len(rule.SourceList))
for _, v := range rule.SourceList {
sourceList = append(sourceList, v.Source)
}
listItem["source_list"] = helper.StringsInterfaces(sourceList)
if k < len(healths) {
health := healths[k]
if health.Enable != nil {
listItem["health_check_switch"] = *health.Enable > 0
}
if health.Url != nil {
listItem["health_check_path"] = *health.Url
}
if health.StatusCode != nil {
listItem["health_check_code"] = int(*health.StatusCode)
}
if health.Interval != nil {
listItem["health_check_interval"] = int(*health.Interval)
}
if health.KickNum != nil {
listItem["health_check_unhealth_num"] = int(*health.KickNum)
}
if health.AliveNum != nil {
listItem["health_check_health_num"] = int(*health.AliveNum)
}
if health.Method != nil {
listItem["health_check_method"] = *health.Method
}
}
list = append(list, listItem)
ids = append(ids, listItem["rule_id"].(string))
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("list", list); e != nil {
log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error())
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
return tccommon.WriteToFile(output.(string), list)
}
return nil
}