| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
|
| 12 |
CacheSettingActionCache CacheSettingAction = "cache" |
| 13 |
|
| 14 |
|
| 15 |
CacheSettingActionPass CacheSettingAction = "pass" |
| 16 |
|
| 17 |
|
| 18 |
CacheSettingActionRestart CacheSettingAction = "restart" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type CacheSettingAction string |
| 23 |
|
| 24 |
|
| 25 |
type CacheSetting struct { |
| 26 |
ServiceID string `mapstructure:"service_id"` |
| 27 |
Version int `mapstructure:"version"` |
| 28 |
|
| 29 |
Name string `mapstructure:"name"` |
| 30 |
Action CacheSettingAction `mapstructure:"action"` |
| 31 |
TTL uint `mapstructure:"ttl"` |
| 32 |
StaleTTL uint `mapstructure:"stale_ttl"` |
| 33 |
CacheCondition string `mapstructure:"cache_condition"` |
| 34 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 35 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 36 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
type cacheSettingsByName []*CacheSetting |
| 41 |
|
| 42 |
|
| 43 |
func (s cacheSettingsByName) Len() int { return len(s) } |
| 44 |
func (s cacheSettingsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 45 |
func (s cacheSettingsByName) Less(i, j int) bool { |
| 46 |
return s[i].Name < s[j].Name |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
type ListCacheSettingsInput struct { |
| 51 |
|
| 52 |
Service string |
| 53 |
|
| 54 |
|
| 55 |
Version int |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (c *Client) ListCacheSettings(i *ListCacheSettingsInput) ([]*CacheSetting, error) { |
| 61 |
if i.Service == "" { |
| 62 |
return nil, ErrMissingService |
| 63 |
} |
| 64 |
|
| 65 |
if i.Version == 0 { |
| 66 |
return nil, ErrMissingVersion |
| 67 |
} |
| 68 |
|
| 69 |
path := fmt.Sprintf("/service/%s/version/%d/cache_settings", i.Service, i.Version) |
| 70 |
resp, err := c.Get(path, nil) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
var cs []*CacheSetting |
| 76 |
if err := decodeJSON(&cs, resp.Body); err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
sort.Stable(cacheSettingsByName(cs)) |
| 80 |
return cs, nil |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
type CreateCacheSettingInput struct { |
| 85 |
|
| 86 |
|
| 87 |
Service string |
| 88 |
Version int |
| 89 |
|
| 90 |
Name string `form:"name,omitempty"` |
| 91 |
Action CacheSettingAction `form:"action,omitempty"` |
| 92 |
TTL uint `form:"ttl,omitempty"` |
| 93 |
StaleTTL uint `form:"stale_ttl,omitempty"` |
| 94 |
CacheCondition string `form:"cache_condition,omitempty"` |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (c *Client) CreateCacheSetting(i *CreateCacheSettingInput) (*CacheSetting, error) { |
| 99 |
if i.Service == "" { |
| 100 |
return nil, ErrMissingService |
| 101 |
} |
| 102 |
|
| 103 |
if i.Version == 0 { |
| 104 |
return nil, ErrMissingVersion |
| 105 |
} |
| 106 |
|
| 107 |
path := fmt.Sprintf("/service/%s/version/%d/cache_settings", i.Service, i.Version) |
| 108 |
resp, err := c.PostForm(path, i, nil) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
|
| 113 |
var cs *CacheSetting |
| 114 |
if err := decodeJSON(&cs, resp.Body); err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
return cs, nil |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
type GetCacheSettingInput struct { |
| 122 |
|
| 123 |
|
| 124 |
Service string |
| 125 |
Version int |
| 126 |
|
| 127 |
|
| 128 |
Name string |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func (c *Client) GetCacheSetting(i *GetCacheSettingInput) (*CacheSetting, error) { |
| 134 |
if i.Service == "" { |
| 135 |
return nil, ErrMissingService |
| 136 |
} |
| 137 |
|
| 138 |
if i.Version == 0 { |
| 139 |
return nil, ErrMissingVersion |
| 140 |
} |
| 141 |
|
| 142 |
if i.Name == "" { |
| 143 |
return nil, ErrMissingName |
| 144 |
} |
| 145 |
|
| 146 |
path := fmt.Sprintf("/service/%s/version/%d/cache_settings/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 147 |
resp, err := c.Get(path, nil) |
| 148 |
if err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
|
| 152 |
var cs *CacheSetting |
| 153 |
if err := decodeJSON(&cs, resp.Body); err != nil { |
| 154 |
return nil, err |
| 155 |
} |
| 156 |
return cs, nil |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
type UpdateCacheSettingInput struct { |
| 161 |
|
| 162 |
|
| 163 |
Service string |
| 164 |
Version int |
| 165 |
|
| 166 |
|
| 167 |
Name string |
| 168 |
|
| 169 |
NewName string `form:"name,omitempty"` |
| 170 |
Action CacheSettingAction `form:"action,omitempty"` |
| 171 |
TTL uint `form:"ttl,omitempty"` |
| 172 |
StaleTTL uint `form:"stale_ttl,omitempty"` |
| 173 |
CacheCondition string `form:"cache_condition,omitempty"` |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
func (c *Client) UpdateCacheSetting(i *UpdateCacheSettingInput) (*CacheSetting, error) { |
| 178 |
if i.Service == "" { |
| 179 |
return nil, ErrMissingService |
| 180 |
} |
| 181 |
|
| 182 |
if i.Version == 0 { |
| 183 |
return nil, ErrMissingVersion |
| 184 |
} |
| 185 |
|
| 186 |
if i.Name == "" { |
| 187 |
return nil, ErrMissingName |
| 188 |
} |
| 189 |
|
| 190 |
path := fmt.Sprintf("/service/%s/version/%d/cache_settings/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 191 |
resp, err := c.PutForm(path, i, nil) |
| 192 |
if err != nil { |
| 193 |
return nil, err |
| 194 |
} |
| 195 |
|
| 196 |
var cs *CacheSetting |
| 197 |
if err := decodeJSON(&cs, resp.Body); err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
return cs, nil |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
type DeleteCacheSettingInput struct { |
| 205 |
|
| 206 |
|
| 207 |
Service string |
| 208 |
Version int |
| 209 |
|
| 210 |
|
| 211 |
Name string |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func (c *Client) DeleteCacheSetting(i *DeleteCacheSettingInput) error { |
| 216 |
if i.Service == "" { |
| 217 |
return ErrMissingService |
| 218 |
} |
| 219 |
|
| 220 |
if i.Version == 0 { |
| 221 |
return ErrMissingVersion |
| 222 |
} |
| 223 |
|
| 224 |
if i.Name == "" { |
| 225 |
return ErrMissingName |
| 226 |
} |
| 227 |
|
| 228 |
path := fmt.Sprintf("/service/%s/version/%d/cache_settings/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 229 |
resp, err := c.Delete(path, nil) |
| 230 |
if err != nil { |
| 231 |
return err |
| 232 |
} |
| 233 |
|
| 234 |
var r *statusResp |
| 235 |
if err := decodeJSON(&r, resp.Body); err != nil { |
| 236 |
return err |
| 237 |
} |
| 238 |
if !r.Ok() { |
| 239 |
return fmt.Errorf("Not Ok") |
| 240 |
} |
| 241 |
return nil |
| 242 |
} |