Debian Codegraph @

fastly/request_setting.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 const (
11 // RequestSettingActionLookup sets request handling to lookup via the cache.
12 RequestSettingActionLookup RequestSettingAction = "lookup"
13
14 // RequestSettingActionPass sets request handling to pass the cache.
15 RequestSettingActionPass RequestSettingAction = "pass"
16 )
17
18 // RequestSettingAction is a type of request setting action.
19 type RequestSettingAction string
20
21 const (
22 // RequestSettingXFFClear clears any X-Forwarded-For headers.
23 RequestSettingXFFClear RequestSettingXFF = "clear"
24
25 // RequestSettingXFFLeave leaves any X-Forwarded-For headers untouched.
26 RequestSettingXFFLeave RequestSettingXFF = "leave"
27
28 // RequestSettingXFFAppend adds Fastly X-Forwarded-For headers.
29 RequestSettingXFFAppend RequestSettingXFF = "append"
30
31 // RequestSettingXFFAppendAll appends all Fastly X-Forwarded-For headers.
32 RequestSettingXFFAppendAll RequestSettingXFF = "append_all"
33
34 // RequestSettingXFFOverwrite clears any X-Forwarded-For headers and replaces
35 // with Fastly ones.
36 RequestSettingXFFOverwrite RequestSettingXFF = "overwrite"
37 )
38
39 // RequestSettingXFF is a type of X-Forwarded-For value to set.
40 type RequestSettingXFF string
41
42 // RequestSetting represents a request setting response from the Fastly API.
43 type RequestSetting struct {
44 ServiceID string `mapstructure:"service_id"`
45 Version int `mapstructure:"version"`
46
47 Name string `mapstructure:"name"`
48 ForceMiss bool `mapstructure:"force_miss"`
49 ForceSSL bool `mapstructure:"force_ssl"`
50 Action RequestSettingAction `mapstructure:"action"`
51 BypassBusyWait bool `mapstructure:"bypass_busy_wait"`
52 MaxStaleAge uint `mapstructure:"max_stale_age"`
53 HashKeys string `mapstructure:"hash_keys"`
54 XForwardedFor RequestSettingXFF `mapstructure:"xff"`
55 TimerSupport bool `mapstructure:"timer_support"`
56 GeoHeaders bool `mapstructure:"geo_headers"`
57 DefaultHost string `mapstructure:"default_host"`
58 RequestCondition string `mapstructure:"request_condition"`
59 CreatedAt *time.Time `mapstructure:"created_at"`
60 UpdatedAt *time.Time `mapstructure:"updated_at"`
61 DeletedAt *time.Time `mapstructure:"deleted_at"`
62 }
63
64 // requestSettingsByName is a sortable list of request settings.
65 type requestSettingsByName []*RequestSetting
66
67 // Len, Swap, and Less implement the sortable interface.
68 func (s requestSettingsByName) Len() int { return len(s) }
69 func (s requestSettingsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
70 func (s requestSettingsByName) Less(i, j int) bool {
71 return s[i].Name < s[j].Name
72 }
73
74 // ListRequestSettingsInput is used as input to the ListRequestSettings
75 // function.
76 type ListRequestSettingsInput struct {
77 // Service is the ID of the service (required).
78 Service string
79
80 // Version is the specific configuration version (required).
81 Version int
82 }
83
84 // ListRequestSettings returns the list of request settings for the
85 // configuration version.
86 func (c *Client) ListRequestSettings(i *ListRequestSettingsInput) ([]*RequestSetting, error) {
87 if i.Service == "" {
88 return nil, ErrMissingService
89 }
90
91 if i.Version == 0 {
92 return nil, ErrMissingVersion
93 }
94
95 path := fmt.Sprintf("/service/%s/version/%d/request_settings", i.Service, i.Version)
96 resp, err := c.Get(path, nil)
97 if err != nil {
98 return nil, err
99 }
100
101 var bs []*RequestSetting
102 if err := decodeJSON(&bs, resp.Body); err != nil {
103 return nil, err
104 }
105 sort.Stable(requestSettingsByName(bs))
106 return bs, nil
107 }
108
109 // CreateRequestSettingInput is used as input to the CreateRequestSetting
110 // function.
111 type CreateRequestSettingInput struct {
112 // Service is the ID of the service. Version is the specific configuration
113 // version. Both fields are required.
114 Service string
115 Version int
116
117 Name string `form:"name,omitempty"`
118 ForceMiss *Compatibool `form:"force_miss,omitempty"`
119 ForceSSL *Compatibool `form:"force_ssl,omitempty"`
120 Action RequestSettingAction `form:"action,omitempty"`
121 BypassBusyWait *Compatibool `form:"bypass_busy_wait,omitempty"`
122 MaxStaleAge uint `form:"max_stale_age,omitempty"`
123 HashKeys string `form:"hash_keys,omitempty"`
124 XForwardedFor RequestSettingXFF `form:"xff,omitempty"`
125 TimerSupport *Compatibool `form:"timer_support,omitempty"`
126 GeoHeaders *Compatibool `form:"geo_headers,omitempty"`
127 DefaultHost string `form:"default_host,omitempty"`
128 RequestCondition string `form:"request_condition,omitempty"`
129 }
130
131 // CreateRequestSetting creates a new Fastly request settings.
132 func (c *Client) CreateRequestSetting(i *CreateRequestSettingInput) (*RequestSetting, error) {
133 if i.Service == "" {
134 return nil, ErrMissingService
135 }
136
137 if i.Version == 0 {
138 return nil, ErrMissingVersion
139 }
140
141 path := fmt.Sprintf("/service/%s/version/%d/request_settings", i.Service, i.Version)
142 resp, err := c.PostForm(path, i, nil)
143 if err != nil {
144 return nil, err
145 }
146
147 var b *RequestSetting
148 if err := decodeJSON(&b, resp.Body); err != nil {
149 return nil, err
150 }
151 return b, nil
152 }
153
154 // GetRequestSettingInput is used as input to the GetRequestSetting function.
155 type GetRequestSettingInput struct {
156 // Service is the ID of the service. Version is the specific configuration
157 // version. Both fields are required.
158 Service string
159 Version int
160
161 // Name is the name of the request settings to fetch.
162 Name string
163 }
164
165 // GetRequestSetting gets the request settings configuration with the given
166 // parameters.
167 func (c *Client) GetRequestSetting(i *GetRequestSettingInput) (*RequestSetting, error) {
168 if i.Service == "" {
169 return nil, ErrMissingService
170 }
171
172 if i.Version == 0 {
173 return nil, ErrMissingVersion
174 }
175
176 if i.Name == "" {
177 return nil, ErrMissingName
178 }
179
180 path := fmt.Sprintf("/service/%s/version/%d/request_settings/%s", i.Service, i.Version, url.PathEscape(i.Name))
181 resp, err := c.Get(path, nil)
182 if err != nil {
183 return nil, err
184 }
185
186 var b *RequestSetting
187 if err := decodeJSON(&b, resp.Body); err != nil {
188 return nil, err
189 }
190 return b, nil
191 }
192
193 // UpdateRequestSettingInput is used as input to the UpdateRequestSetting
194 // function.
195 type UpdateRequestSettingInput struct {
196 // Service is the ID of the service. Version is the specific configuration
197 // version. Both fields are required.
198 Service string
199 Version int
200
201 // Name is the name of the request settings to update.
202 Name string
203
204 NewName string `form:"name,omitempty"`
205 ForceMiss *Compatibool `form:"force_miss,omitempty"`
206 ForceSSL *Compatibool `form:"force_ssl,omitempty"`
207 Action RequestSettingAction `form:"action,omitempty"`
208 BypassBusyWait *Compatibool `form:"bypass_busy_wait,omitempty"`
209 MaxStaleAge uint `form:"max_stale_age,omitempty"`
210 HashKeys string `form:"hash_keys,omitempty"`
211 XForwardedFor RequestSettingXFF `form:"xff,omitempty"`
212 TimerSupport *Compatibool `form:"timer_support,omitempty"`
213 GeoHeaders *Compatibool `form:"geo_headers,omitempty"`
214 DefaultHost string `form:"default_host,omitempty"`
215 RequestCondition string `form:"request_condition,omitempty"`
216 }
217
218 // UpdateRequestSetting updates a specific request settings.
219 func (c *Client) UpdateRequestSetting(i *UpdateRequestSettingInput) (*RequestSetting, error) {
220 if i.Service == "" {
221 return nil, ErrMissingService
222 }
223
224 if i.Version == 0 {
225 return nil, ErrMissingVersion
226 }
227
228 if i.Name == "" {
229 return nil, ErrMissingName
230 }
231
232 path := fmt.Sprintf("/service/%s/version/%d/request_settings/%s", i.Service, i.Version, url.PathEscape(i.Name))
233 resp, err := c.PutForm(path, i, nil)
234 if err != nil {
235 return nil, err
236 }
237
238 var b *RequestSetting
239 if err := decodeJSON(&b, resp.Body); err != nil {
240 return nil, err
241 }
242 return b, nil
243 }
244
245 // DeleteRequestSettingInput is the input parameter to DeleteRequestSetting.
246 type DeleteRequestSettingInput struct {
247 // Service is the ID of the service. Version is the specific configuration
248 // version. Both fields are required.
249 Service string
250 Version int
251
252 // Name is the name of the request settings to delete (required).
253 Name string
254 }
255
256 // DeleteRequestSetting deletes the given request settings version.
257 func (c *Client) DeleteRequestSetting(i *DeleteRequestSettingInput) error {
258 if i.Service == "" {
259 return ErrMissingService
260 }
261
262 if i.Version == 0 {
263 return ErrMissingVersion
264 }
265
266 if i.Name == "" {
267 return ErrMissingName
268 }
269
270 path := fmt.Sprintf("/service/%s/version/%d/request_settings/%s", i.Service, i.Version, url.PathEscape(i.Name))
271 resp, err := c.Delete(path, nil)
272 if err != nil {
273 return err
274 }
275
276 var r *statusResp
277 if err := decodeJSON(&r, resp.Body); err != nil {
278 return err
279 }
280 if !r.Ok() {
281 return fmt.Errorf("Not Ok")
282 }
283 return nil
284 }