| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
|
| 12 |
HeaderActionSet HeaderAction = "set" |
| 13 |
|
| 14 |
|
| 15 |
HeaderActionAppend HeaderAction = "append" |
| 16 |
|
| 17 |
|
| 18 |
HeaderActionDelete HeaderAction = "delete" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
HeaderActionRegex HeaderAction = "regex" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
HeaderActionRegexRepeat HeaderAction = "regex_repeat" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
type HeaderAction string |
| 31 |
|
| 32 |
const ( |
| 33 |
|
| 34 |
|
| 35 |
HeaderTypeRequest HeaderType = "request" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
HeaderTypeFetch HeaderType = "fetch" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
HeaderTypeCache HeaderType = "cache" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
HeaderTypeResponse HeaderType = "response" |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
type HeaderType string |
| 52 |
|
| 53 |
|
| 54 |
type Header struct { |
| 55 |
ServiceID string `mapstructure:"service_id"` |
| 56 |
Version int `mapstructure:"version"` |
| 57 |
|
| 58 |
Name string `mapstructure:"name"` |
| 59 |
Action HeaderAction `mapstructure:"action"` |
| 60 |
IgnoreIfSet bool `mapstructure:"ignore_if_set"` |
| 61 |
Type HeaderType `mapstructure:"type"` |
| 62 |
Destination string `mapstructure:"dst"` |
| 63 |
Source string `mapstructure:"src"` |
| 64 |
Regex string `mapstructure:"regex"` |
| 65 |
Substitution string `mapstructure:"substitution"` |
| 66 |
Priority uint `mapstructure:"priority"` |
| 67 |
RequestCondition string `mapstructure:"request_condition"` |
| 68 |
CacheCondition string `mapstructure:"cache_condition"` |
| 69 |
ResponseCondition string `mapstructure:"response_condition"` |
| 70 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 71 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 72 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
type headersByName []*Header |
| 77 |
|
| 78 |
|
| 79 |
func (s headersByName) Len() int { return len(s) } |
| 80 |
func (s headersByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 81 |
func (s headersByName) Less(i, j int) bool { |
| 82 |
return s[i].Name < s[j].Name |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
type ListHeadersInput struct { |
| 87 |
|
| 88 |
Service string |
| 89 |
|
| 90 |
|
| 91 |
Version int |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (c *Client) ListHeaders(i *ListHeadersInput) ([]*Header, error) { |
| 96 |
if i.Service == "" { |
| 97 |
return nil, ErrMissingService |
| 98 |
} |
| 99 |
|
| 100 |
if i.Version == 0 { |
| 101 |
return nil, ErrMissingVersion |
| 102 |
} |
| 103 |
|
| 104 |
path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version) |
| 105 |
resp, err := c.Get(path, nil) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
|
| 110 |
var bs []*Header |
| 111 |
if err := decodeJSON(&bs, resp.Body); err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
sort.Stable(headersByName(bs)) |
| 115 |
return bs, nil |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
type CreateHeaderInput struct { |
| 120 |
|
| 121 |
|
| 122 |
Service string |
| 123 |
Version int |
| 124 |
|
| 125 |
Name string `form:"name,omitempty"` |
| 126 |
Action HeaderAction `form:"action,omitempty"` |
| 127 |
IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"` |
| 128 |
Type HeaderType `form:"type,omitempty"` |
| 129 |
Destination string `form:"dst,omitempty"` |
| 130 |
Source string `form:"src,omitempty"` |
| 131 |
Regex string `form:"regex,omitempty"` |
| 132 |
Substitution string `form:"substitution,omitempty"` |
| 133 |
Priority uint `form:"priority,omitempty"` |
| 134 |
RequestCondition string `form:"request_condition,omitempty"` |
| 135 |
CacheCondition string `form:"cache_condition,omitempty"` |
| 136 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func (c *Client) CreateHeader(i *CreateHeaderInput) (*Header, error) { |
| 141 |
if i.Service == "" { |
| 142 |
return nil, ErrMissingService |
| 143 |
} |
| 144 |
|
| 145 |
if i.Version == 0 { |
| 146 |
return nil, ErrMissingVersion |
| 147 |
} |
| 148 |
|
| 149 |
path := fmt.Sprintf("/service/%s/version/%d/header", i.Service, i.Version) |
| 150 |
resp, err := c.PostForm(path, i, nil) |
| 151 |
if err != nil { |
| 152 |
return nil, err |
| 153 |
} |
| 154 |
|
| 155 |
var b *Header |
| 156 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
return b, nil |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
type GetHeaderInput struct { |
| 164 |
|
| 165 |
|
| 166 |
Service string |
| 167 |
Version int |
| 168 |
|
| 169 |
|
| 170 |
Name string |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func (c *Client) GetHeader(i *GetHeaderInput) (*Header, error) { |
| 175 |
if i.Service == "" { |
| 176 |
return nil, ErrMissingService |
| 177 |
} |
| 178 |
|
| 179 |
if i.Version == 0 { |
| 180 |
return nil, ErrMissingVersion |
| 181 |
} |
| 182 |
|
| 183 |
if i.Name == "" { |
| 184 |
return nil, ErrMissingName |
| 185 |
} |
| 186 |
|
| 187 |
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 188 |
resp, err := c.Get(path, nil) |
| 189 |
if err != nil { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
|
| 193 |
var b *Header |
| 194 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 195 |
return nil, err |
| 196 |
} |
| 197 |
return b, nil |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
type UpdateHeaderInput struct { |
| 202 |
|
| 203 |
|
| 204 |
Service string |
| 205 |
Version int |
| 206 |
|
| 207 |
|
| 208 |
Name string |
| 209 |
|
| 210 |
NewName string `form:"name,omitempty"` |
| 211 |
Action HeaderAction `form:"action,omitempty"` |
| 212 |
IgnoreIfSet *Compatibool `form:"ignore_if_set,omitempty"` |
| 213 |
Type HeaderType `form:"type,omitempty"` |
| 214 |
Destination string `form:"dst,omitempty"` |
| 215 |
Source string `form:"src,omitempty"` |
| 216 |
Regex string `form:"regex,omitempty"` |
| 217 |
Substitution string `form:"substitution,omitempty"` |
| 218 |
Priority uint `form:"priority,omitempty"` |
| 219 |
RequestCondition string `form:"request_condition,omitempty"` |
| 220 |
CacheCondition string `form:"cache_condition,omitempty"` |
| 221 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (c *Client) UpdateHeader(i *UpdateHeaderInput) (*Header, error) { |
| 226 |
if i.Service == "" { |
| 227 |
return nil, ErrMissingService |
| 228 |
} |
| 229 |
|
| 230 |
if i.Version == 0 { |
| 231 |
return nil, ErrMissingVersion |
| 232 |
} |
| 233 |
|
| 234 |
if i.Name == "" { |
| 235 |
return nil, ErrMissingName |
| 236 |
} |
| 237 |
|
| 238 |
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 239 |
resp, err := c.PutForm(path, i, nil) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
|
| 244 |
var b *Header |
| 245 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
return b, nil |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
type DeleteHeaderInput struct { |
| 253 |
|
| 254 |
|
| 255 |
Service string |
| 256 |
Version int |
| 257 |
|
| 258 |
|
| 259 |
Name string |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
func (c *Client) DeleteHeader(i *DeleteHeaderInput) error { |
| 264 |
if i.Service == "" { |
| 265 |
return ErrMissingService |
| 266 |
} |
| 267 |
|
| 268 |
if i.Version == 0 { |
| 269 |
return ErrMissingVersion |
| 270 |
} |
| 271 |
|
| 272 |
if i.Name == "" { |
| 273 |
return ErrMissingName |
| 274 |
} |
| 275 |
|
| 276 |
path := fmt.Sprintf("/service/%s/version/%d/header/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 277 |
resp, err := c.Delete(path, nil) |
| 278 |
if err != nil { |
| 279 |
return err |
| 280 |
} |
| 281 |
|
| 282 |
var r *statusResp |
| 283 |
if err := decodeJSON(&r, resp.Body); err != nil { |
| 284 |
return err |
| 285 |
} |
| 286 |
if !r.Ok() { |
| 287 |
return fmt.Errorf("Not Ok") |
| 288 |
} |
| 289 |
return nil |
| 290 |
} |