-
Notifications
You must be signed in to change notification settings - Fork 132
/
block_fastly_service_v1_requestsetting.go
244 lines (220 loc) · 7.03 KB
/
block_fastly_service_v1_requestsetting.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
package fastly
import (
"fmt"
"log"
"strings"
gofastly "github.com/fastly/go-fastly/v2/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type RequestSettingServiceAttributeHandler struct {
*DefaultServiceAttributeHandler
}
func NewServiceRequestSetting(sa ServiceMetadata) ServiceAttributeDefinition {
return &RequestSettingServiceAttributeHandler{
&DefaultServiceAttributeHandler{
key: "request_setting",
serviceMetadata: sa,
},
}
}
func (h *RequestSettingServiceAttributeHandler) Process(d *schema.ResourceData, latestVersion int, conn *gofastly.Client) error {
os, ns := d.GetChange(h.GetKey())
if os == nil {
os = new(schema.Set)
}
if ns == nil {
ns = new(schema.Set)
}
ors := os.(*schema.Set)
nrs := ns.(*schema.Set)
removeRequestSettings := ors.Difference(nrs).List()
addRequestSettings := nrs.Difference(ors).List()
// DELETE old Request Settings configurations
for _, sRaw := range removeRequestSettings {
sf := sRaw.(map[string]interface{})
opts := gofastly.DeleteRequestSettingInput{
ServiceID: d.Id(),
ServiceVersion: latestVersion,
Name: sf["name"].(string),
}
log.Printf("[DEBUG] Fastly Request Setting removal opts: %#v", opts)
err := conn.DeleteRequestSetting(&opts)
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
}
} else if err != nil {
return err
}
}
// POST new/updated Request Setting
for _, sRaw := range addRequestSettings {
opts, err := buildRequestSetting(sRaw.(map[string]interface{}))
if err != nil {
log.Printf("[DEBUG] Error building Requset Setting: %s", err)
return err
}
opts.ServiceID = d.Id()
opts.ServiceVersion = latestVersion
log.Printf("[DEBUG] Create Request Setting Opts: %#v", opts)
_, err = conn.CreateRequestSetting(opts)
if err != nil {
return err
}
}
return nil
}
func (h *RequestSettingServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.ServiceDetail, conn *gofastly.Client) error {
log.Printf("[DEBUG] Refreshing Request Settings for (%s)", d.Id())
rsList, err := conn.ListRequestSettings(&gofastly.ListRequestSettingsInput{
ServiceID: d.Id(),
ServiceVersion: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up Request Settings for (%s), version (%v): %s", d.Id(), s.ActiveVersion.Number, err)
}
rl := flattenRequestSettings(rsList)
if err := d.Set(h.GetKey(), rl); err != nil {
log.Printf("[WARN] Error setting Request Settings for (%s): %s", d.Id(), err)
}
return nil
}
func (h *RequestSettingServiceAttributeHandler) Register(s *schema.Resource) error {
s.Schema[h.GetKey()] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required fields
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique name to refer to this Request Setting",
},
// Optional fields
"request_condition": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Name of a request condition to apply. If there is no condition this setting will always be applied.",
},
"max_stale_age": {
Type: schema.TypeInt,
Optional: true,
Description: "How old an object is allowed to be, in seconds. Default `60`",
},
"force_miss": {
Type: schema.TypeBool,
Optional: true,
Description: "Force a cache miss for the request",
},
"force_ssl": {
Type: schema.TypeBool,
Optional: true,
Description: "Forces the request use SSL",
},
"action": {
Type: schema.TypeString,
Optional: true,
Description: "Allows you to terminate request handling and immediately perform an action",
},
"bypass_busy_wait": {
Type: schema.TypeBool,
Optional: true,
Description: "Disable collapsed forwarding",
},
"hash_keys": {
Type: schema.TypeString,
Optional: true,
Description: "Comma separated list of varnish request object fields that should be in the hash key",
},
"xff": {
Type: schema.TypeString,
Optional: true,
Default: "append",
Description: "X-Forwarded-For options",
},
"timer_support": {
Type: schema.TypeBool,
Optional: true,
Description: "Injects the X-Timer info into the request",
},
"geo_headers": {
Type: schema.TypeBool,
Optional: true,
Description: "Inject Fastly-Geo-Country, Fastly-Geo-City, and Fastly-Geo-Region",
},
"default_host": {
Type: schema.TypeString,
Optional: true,
Description: "the host header",
},
},
},
}
return nil
}
func flattenRequestSettings(rsList []*gofastly.RequestSetting) []map[string]interface{} {
var rl []map[string]interface{}
for _, r := range rsList {
// Convert Request Settings to a map for saving to state.
nrs := map[string]interface{}{
"name": r.Name,
"max_stale_age": r.MaxStaleAge,
"force_miss": r.ForceMiss,
"force_ssl": r.ForceSSL,
"action": r.Action,
"bypass_busy_wait": r.BypassBusyWait,
"hash_keys": r.HashKeys,
"xff": r.XForwardedFor,
"timer_support": r.TimerSupport,
"geo_headers": r.GeoHeaders,
"default_host": r.DefaultHost,
"request_condition": r.RequestCondition,
}
// prune any empty values that come from the default string value in structs
for k, v := range nrs {
if v == "" {
delete(nrs, k)
}
}
rl = append(rl, nrs)
}
return rl
}
func buildRequestSetting(requestSettingMap interface{}) (*gofastly.CreateRequestSettingInput, error) {
df := requestSettingMap.(map[string]interface{})
opts := gofastly.CreateRequestSettingInput{
Name: df["name"].(string),
MaxStaleAge: uint(df["max_stale_age"].(int)),
ForceMiss: gofastly.Compatibool(df["force_miss"].(bool)),
ForceSSL: gofastly.Compatibool(df["force_ssl"].(bool)),
BypassBusyWait: gofastly.Compatibool(df["bypass_busy_wait"].(bool)),
HashKeys: df["hash_keys"].(string),
TimerSupport: gofastly.Compatibool(df["timer_support"].(bool)),
GeoHeaders: gofastly.Compatibool(df["geo_headers"].(bool)),
DefaultHost: df["default_host"].(string),
RequestCondition: df["request_condition"].(string),
}
act := strings.ToLower(df["action"].(string))
switch act {
case "lookup":
opts.Action = gofastly.RequestSettingActionLookup
case "pass":
opts.Action = gofastly.RequestSettingActionPass
}
xff := strings.ToLower(df["xff"].(string))
switch xff {
case "clear":
opts.XForwardedFor = gofastly.RequestSettingXFFClear
case "leave":
opts.XForwardedFor = gofastly.RequestSettingXFFLeave
case "append":
opts.XForwardedFor = gofastly.RequestSettingXFFAppend
case "append_all":
opts.XForwardedFor = gofastly.RequestSettingXFFAppendAll
case "overwrite":
opts.XForwardedFor = gofastly.RequestSettingXFFOverwrite
}
return &opts, nil
}