/
data_akamai_cloudlets_application_load_balancer_match_rule.go
321 lines (300 loc) · 11.9 KB
/
data_akamai_cloudlets_application_load_balancer_match_rule.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
package cloudlets
import (
"context"
"encoding/json"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/cloudlets"
"github.com/akamai/terraform-provider-akamai/v2/pkg/tools"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceCloudletsApplicationLoadBalancerMatchRule() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudletsLoadBalancerMatchRuleRead,
Schema: map[string]*schema.Schema{
"match_rules": {
Type: schema.TypeList,
Optional: true,
Description: "Defines a set of rules for policy",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the rule",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8192)),
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of Cloudlet the rule is for",
},
"start": {
Type: schema.TypeInt,
Optional: true,
Description: "The start time for this match (in seconds since the epoch)",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(0)),
},
"end": {
Type: schema.TypeInt,
Optional: true,
Description: "The end time for this match (in seconds since the epoch)",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(0)),
},
"id": {
Type: schema.TypeInt,
Optional: true,
Description: "Akamai internal use only",
},
"matches": {
Type: schema.TypeList,
Optional: true,
Description: "Defines a set of match objects",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"match_type": {
Type: schema.TypeString,
Optional: true,
Description: "The type of match used",
ValidateDiagFunc: tools.ValidateStringInSlice([]string{"header", "hostname", "path", "extension", "query",
"cookie", "deviceCharacteristics", "clientip", "continent", "countrycode", "regioncode", "protocol", "method", "proxy", "range"}),
},
"match_value": {
Type: schema.TypeString,
Optional: true,
Description: "Depends on the matchType",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8192)),
},
"match_operator": {
Type: schema.TypeString,
Optional: true,
Description: "Valid entries for this property: contains, exists, and equals",
ValidateDiagFunc: tools.ValidateStringInSlice([]string{"contains", "exists", "equals", ""}),
},
"case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, the match is case sensitive",
},
"negate": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, negates the match",
},
"check_ips": {
Type: schema.TypeString,
Optional: true,
Description: "For clientip, continent, countrycode, proxy, and regioncode match types, the part of the request that determines the IP address to use",
ValidateDiagFunc: tools.ValidateStringInSlice([]string{"CONNECTING_IP", "XFF_HEADERS", "CONNECTING_IP XFF_HEADERS", ""}),
},
"object_match_value": {
Type: schema.TypeSet,
Optional: true,
Description: "An object used when a rule either includes more complex match criteria, like multiple value attributes, or a range match",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "If using a match type that supports name attributes, enter the value in the incoming request to match on. " +
"The following match types support this property: cookie, header, parameter, and query",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8192)),
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "The array type, which can be one of the following: object, range, or simple. " +
"Use the simple option when adding only an array of string-based values",
ValidateDiagFunc: tools.ValidateStringInSlice([]string{"simple", "object", "range"}),
},
"name_case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if the entry for the name property should be evaluated based on case sensitivity",
},
"name_has_wildcard": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if the entry for the name property includes wildcards",
},
"options": {
Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Description: "If using the object type, use this set to list the values to match on (use only with the object type)",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The value attributes in the incoming request to match on",
},
"value_has_wildcard": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if the entries for the value property include wildcards",
},
"value_case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if the entries for the value property should be evaluated based on case sensitivity",
},
"value_escaped": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if provided value should be compared in escaped form",
},
},
},
},
"value": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The value attributes in the incoming request to match on (use only with simple or range type)",
},
},
},
},
},
},
},
"match_url": {
Type: schema.TypeString,
Optional: true,
Description: "If using a URL match, this property is the URL that the Cloudlet uses to match the incoming request",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8192)),
},
"matches_always": {
Type: schema.TypeBool,
Optional: true,
Description: "Is used in some cloudlets to support default rules (rule that is always matched)",
},
"forward_settings": {
Type: schema.TypeSet,
Required: true,
Description: "This property defines data used to construct a new request URL if all conditions are met. " +
"If all of the conditions you set are true, then the Edge Server returns an HTTP response from the rewritten URL",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"origin_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the Conditional Origin requests are forwarded to",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8192)),
},
},
},
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Description: "If set to true, disables a rule so it is not evaluated against incoming requests.",
},
},
},
},
"json": {
Type: schema.TypeString,
Computed: true,
Description: "A match_rules JSON structure generated from the schema",
},
},
}
}
func dataSourceCloudletsLoadBalancerMatchRuleRead(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
matchRules, err := tools.GetListValue("match_rules", d)
if err != nil {
return diag.FromErr(err)
}
err = setMatchRuleSchemaType(matchRules, cloudlets.MatchRuleTypeALB)
if err != nil {
return diag.FromErr(err)
}
rules := make(cloudlets.MatchRules, len(matchRules))
for i, r := range matchRules {
rawRule := r.(map[string]interface{})
// types are guaranteed by the datasource schema -> no need for type assertions
rule := cloudlets.MatchRuleALB{
Name: getStringValue(rawRule, "name"),
Type: cloudlets.MatchRuleTypeALB,
MatchURL: getStringValue(rawRule, "match_url"),
Start: getInt64Value(rawRule, "start"),
End: getInt64Value(rawRule, "end"),
ID: getInt64Value(rawRule, "id"),
MatchesAlways: getBoolValue(rawRule, "matches_always"),
Disabled: getBoolValue(rawRule, "disabled"),
}
rule.Matches, err = parseRuleMatches(rawRule, "matches")
if err != nil {
return diag.Errorf("'match_rules' - %s", err)
}
// Schema guarantees that "forward_settings" will be present and of type *schema.Set
settings, ok := rawRule["forward_settings"].(*schema.Set)
if !ok {
return diag.Errorf("%v: 'forward_settings' should be an *schema.Set", tools.ErrInvalidType)
}
for _, element := range settings.List() {
entries := element.(map[string]interface{})
// Schema guarantees that "origin_id" will be present
rule.ForwardSettings = cloudlets.ForwardSettingsALB{
OriginID: entries["origin_id"].(string),
}
}
rules[i] = rule
}
if err := rules.Validate(); err != nil {
return diag.FromErr(err)
}
rulesJSON, err := json.MarshalIndent(rules, "", " ")
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("json", string(rulesJSON)); err != nil {
return diag.Errorf("%v: %s", tools.ErrValueSet, err.Error())
}
hashID, err := getMatchRulesHashID(rules)
if err != nil {
return diag.FromErr(err)
}
d.SetId(hashID)
return nil
}
func parseRuleMatches(rawRule map[string]interface{}, field string) ([]cloudlets.MatchCriteriaALB, error) {
matches, ok := rawRule[field]
if !ok {
return nil, nil
}
rawMatches := matches.([]interface{})
matchCriteriaALBData := make([]cloudlets.MatchCriteriaALB, len(rawMatches))
for i, rawMatch := range rawMatches {
alb, err := parseMatchCriteriaALB(rawMatch)
if err != nil {
return nil, err
}
matchCriteriaALBData[i] = *alb
}
return matchCriteriaALBData, nil
}
func parseMatchCriteriaALB(match interface{}) (*cloudlets.MatchCriteriaALB, error) {
m := match.(map[string]interface{})
matchCriteriaALB := cloudlets.MatchCriteriaALB{
MatchType: getStringValue(m, "match_type"),
MatchValue: getStringValue(m, "match_value"),
CaseSensitive: getBoolValue(m, "case_sensitive"),
Negate: getBoolValue(m, "negate"),
MatchOperator: cloudlets.MatchOperator(getStringValue(m, "match_operator")),
}
if c, ok := m["check_ips"]; ok {
if checkIPs, ok := c.(cloudlets.CheckIPs); ok {
matchCriteriaALB.CheckIPs = checkIPs
}
}
omv, err := parseObjectMatchValue(m, getObjectMatchValueObjectOrSimpleOrRange)
if err != nil {
return nil, err
}
matchCriteriaALB.ObjectMatchValue = omv
return &matchCriteriaALB, err
}