/
header.go
323 lines (284 loc) · 10.5 KB
/
header.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
322
323
package fastly
import (
"fmt"
"net/url"
"sort"
"time"
)
const (
// HeaderActionSet is a header action that sets or resets a header.
HeaderActionSet HeaderAction = "set"
// HeaderActionAppend is a header action that appends to an existing header.
HeaderActionAppend HeaderAction = "append"
// HeaderActionDelete is a header action that deletes a header.
HeaderActionDelete HeaderAction = "delete"
// HeaderActionRegex is a header action that performs a single regex
// replacement on a header.
HeaderActionRegex HeaderAction = "regex"
// HeaderActionRegexRepeat is a header action that performs a global regex
// replacement on a header.
HeaderActionRegexRepeat HeaderAction = "regex_repeat"
)
// HeaderAction is a type of header action.
type HeaderAction string
// HeaderActionPtr returns pointer to HeaderAction.
func HeaderActionPtr(t HeaderAction) *HeaderAction {
ha := HeaderAction(t)
return &ha
}
const (
// HeaderTypeRequest is a header type that performs on the request before
// lookups.
HeaderTypeRequest HeaderType = "request"
// HeaderTypeFetch is a header type that performs on the request to the origin
// server.
HeaderTypeFetch HeaderType = "fetch"
// HeaderTypeCache is a header type that performs on the response before it's
// store in the cache.
HeaderTypeCache HeaderType = "cache"
// HeaderTypeResponse is a header type that performs on the response before
// delivering to the client.
HeaderTypeResponse HeaderType = "response"
)
// HeaderType is a type of header.
type HeaderType string
// HeaderTypePtr returns pointer to HeaderType.
func HeaderTypePtr(t HeaderType) *HeaderType {
ht := HeaderType(t)
return &ht
}
// Header represents a header response from the Fastly API.
type Header struct {
Action HeaderAction `mapstructure:"action"`
CacheCondition string `mapstructure:"cache_condition"`
CreatedAt *time.Time `mapstructure:"created_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
Destination string `mapstructure:"dst"`
IgnoreIfSet bool `mapstructure:"ignore_if_set"`
Name string `mapstructure:"name"`
Priority int `mapstructure:"priority"`
Regex string `mapstructure:"regex"`
RequestCondition string `mapstructure:"request_condition"`
ResponseCondition string `mapstructure:"response_condition"`
ServiceID string `mapstructure:"service_id"`
ServiceVersion int `mapstructure:"version"`
Source string `mapstructure:"src"`
Substitution string `mapstructure:"substitution"`
Type HeaderType `mapstructure:"type"`
UpdatedAt *time.Time `mapstructure:"updated_at"`
}
// headersByName is a sortable list of headers.
type headersByName []*Header
// Len implement the sortable interface.
func (s headersByName) Len() int {
return len(s)
}
// Swap implement the sortable interface.
func (s headersByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less implement the sortable interface.
func (s headersByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
// ListHeadersInput is used as input to the ListHeaders function.
type ListHeadersInput struct {
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// ListHeaders retrieves all resources.
func (c *Client) ListHeaders(i *ListHeadersInput) ([]*Header, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header", i.ServiceID, i.ServiceVersion)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var bs []*Header
if err := decodeBodyMap(resp.Body, &bs); err != nil {
return nil, err
}
sort.Stable(headersByName(bs))
return bs, nil
}
// CreateHeaderInput is used as input to the CreateHeader function.
type CreateHeaderInput struct {
// Action accepts a string value (set, append, delete, regex, regex_repeat).
Action *HeaderAction `url:"action,omitempty"`
// CacheCondition is the name of the cache condition controlling when this configuration applies.
CacheCondition *string `url:"cache_condition,omitempty"`
// Destination is the header to set.
Destination *string `url:"dst,omitempty"`
// IgnoreIfSet prevents adding the header if it is added already. Only applies to 'set' action.
IgnoreIfSet *Compatibool `url:"ignore_if_set,omitempty"`
// Name is a handle to refer to this Header object.
Name *string `url:"name,omitempty"`
// Priority determines execution order. Lower numbers execute first.
Priority *int `url:"priority,omitempty"`
// Regex is the regular expression to use. Only applies to regex and regex_repeat actions.
Regex *string `url:"regex,omitempty"`
// RequestCondition is the condition which, if met, will select this configuration during a request.
RequestCondition *string `url:"request_condition,omitempty"`
// ResponseCondition is an optional name of a response condition to apply.
ResponseCondition *string `url:"response_condition,omitempty"`
// ServiceID is the ID of the service (required).
ServiceID string `url:"-"`
// ServiceVersion is the specific configuration version (required).
ServiceVersion int `url:"-"`
// Source is a variable to be used as a source for the header content. Does not apply to delete action.
Source *string `url:"src,omitempty"`
// Substitution is a value to substitute in place of regular expression. Only applies to regex and regex_repeat actions.
Substitution *string `url:"substitution,omitempty"`
// Type is a type of header (request, cache, response).
Type *HeaderType `url:"type,omitempty"`
}
// CreateHeader creates a new resource.
func (c *Client) CreateHeader(i *CreateHeaderInput) (*Header, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header", i.ServiceID, i.ServiceVersion)
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *Header
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}
// GetHeaderInput is used as input to the GetHeader function.
type GetHeaderInput struct {
// Name is the name of the header to fetch (required).
Name string
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// GetHeader retrieves the specified resource.
func (c *Client) GetHeader(i *GetHeaderInput) (*Header, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *Header
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}
// UpdateHeaderInput is used as input to the UpdateHeader function.
type UpdateHeaderInput struct {
// Action accepts a string value (set, append, delete, regex, regex_repeat).
Action *HeaderAction `url:"action,omitempty"`
// CacheCondition is the name of the cache condition controlling when this configuration applies.
CacheCondition *string `url:"cache_condition,omitempty"`
// Destination is the header to set.
Destination *string `url:"dst,omitempty"`
// IgnoreIfSet prevents adding the header if it is added already. Only applies to 'set' action.
IgnoreIfSet *Compatibool `url:"ignore_if_set,omitempty"`
// Name is the name of the header to update (required).
Name string `url:"-"`
// NewName is the new name for the resource.
NewName *string `url:"name,omitempty"`
// Priority determines execution order. Lower numbers execute first.
Priority *int `url:"priority,omitempty"`
// Regex is the regular expression to use. Only applies to regex and regex_repeat actions.
Regex *string `url:"regex,omitempty"`
// RequestCondition is the condition which, if met, will select this configuration during a request.
RequestCondition *string `url:"request_condition,omitempty"`
// ResponseCondition is an optional name of a response condition to apply.
ResponseCondition *string `url:"response_condition,omitempty"`
// ServiceID is the ID of the service (required).
ServiceID string `url:"-"`
// ServiceVersion is the specific configuration version (required).
ServiceVersion int `url:"-"`
// Source is a variable to be used as a source for the header content. Does not apply to delete action.
Source *string `url:"src,omitempty"`
// Substitution is a value to substitute in place of regular expression. Only applies to regex and regex_repeat actions.
Substitution *string `url:"substitution,omitempty"`
// Type is a type of header (request, cache, response).
Type *HeaderType `url:"type,omitempty"`
}
// UpdateHeader updates the specified resource.
func (c *Client) UpdateHeader(i *UpdateHeaderInput) (*Header, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.PutForm(path, i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *Header
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}
// DeleteHeaderInput is the input parameter to DeleteHeader.
type DeleteHeaderInput struct {
// Name is the name of the header to delete (required).
Name string
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// DeleteHeader deletes the specified resource.
func (c *Client) DeleteHeader(i *DeleteHeaderInput) error {
if i.Name == "" {
return ErrMissingName
}
if i.ServiceID == "" {
return ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.Delete(path, nil)
if err != nil {
return err
}
defer resp.Body.Close()
var r *statusResp
if err := decodeBodyMap(resp.Body, &r); err != nil {
return err
}
if !r.Ok() {
return ErrNotOK
}
return nil
}