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