| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
|
| 12 |
RequestSettingActionLookup RequestSettingAction = "lookup" |
| 13 |
|
| 14 |
|
| 15 |
RequestSettingActionPass RequestSettingAction = "pass" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type RequestSettingAction string |
| 20 |
|
| 21 |
const ( |
| 22 |
|
| 23 |
RequestSettingXFFClear RequestSettingXFF = "clear" |
| 24 |
|
| 25 |
|
| 26 |
RequestSettingXFFLeave RequestSettingXFF = "leave" |
| 27 |
|
| 28 |
|
| 29 |
RequestSettingXFFAppend RequestSettingXFF = "append" |
| 30 |
|
| 31 |
|
| 32 |
RequestSettingXFFAppendAll RequestSettingXFF = "append_all" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
RequestSettingXFFOverwrite RequestSettingXFF = "overwrite" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
type RequestSettingXFF string |
| 41 |
|
| 42 |
|
| 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 |
|
| 65 |
type requestSettingsByName []*RequestSetting |
| 66 |
|
| 67 |
|
| 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 |
|
| 75 |
|
| 76 |
type ListRequestSettingsInput struct { |
| 77 |
|
| 78 |
Service string |
| 79 |
|
| 80 |
|
| 81 |
Version int |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 110 |
|
| 111 |
type CreateRequestSettingInput struct { |
| 112 |
|
| 113 |
|
| 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 |
|
| 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 |
|
| 155 |
type GetRequestSettingInput struct { |
| 156 |
|
| 157 |
|
| 158 |
Service string |
| 159 |
Version int |
| 160 |
|
| 161 |
|
| 162 |
Name string |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 194 |
|
| 195 |
type UpdateRequestSettingInput struct { |
| 196 |
|
| 197 |
|
| 198 |
Service string |
| 199 |
Version int |
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 246 |
type DeleteRequestSettingInput struct { |
| 247 |
|
| 248 |
|
| 249 |
Service string |
| 250 |
Version int |
| 251 |
|
| 252 |
|
| 253 |
Name string |
| 254 |
} |
| 255 |
|
| 256 |
|
| 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 |
} |