| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Backend struct { |
| 12 |
ServiceID string `mapstructure:"service_id"` |
| 13 |
Version int `mapstructure:"version"` |
| 14 |
|
| 15 |
Name string `mapstructure:"name"` |
| 16 |
Comment string `mapstructure:"comment"` |
| 17 |
Address string `mapstructure:"address"` |
| 18 |
Port uint `mapstructure:"port"` |
| 19 |
OverrideHost string `mapstructure:"override_host"` |
| 20 |
ConnectTimeout uint `mapstructure:"connect_timeout"` |
| 21 |
MaxConn uint `mapstructure:"max_conn"` |
| 22 |
ErrorThreshold uint `mapstructure:"error_threshold"` |
| 23 |
FirstByteTimeout uint `mapstructure:"first_byte_timeout"` |
| 24 |
BetweenBytesTimeout uint `mapstructure:"between_bytes_timeout"` |
| 25 |
AutoLoadbalance bool `mapstructure:"auto_loadbalance"` |
| 26 |
Weight uint `mapstructure:"weight"` |
| 27 |
RequestCondition string `mapstructure:"request_condition"` |
| 28 |
HealthCheck string `mapstructure:"healthcheck"` |
| 29 |
Hostname string `mapstructure:"hostname"` |
| 30 |
Shield string `mapstructure:"shield"` |
| 31 |
UseSSL bool `mapstructure:"use_ssl"` |
| 32 |
SSLCheckCert bool `mapstructure:"ssl_check_cert"` |
| 33 |
SSLCACert string `mapstructure:"ssl_ca_cert"` |
| 34 |
SSLClientCert string `mapstructure:"ssl_client_cert"` |
| 35 |
SSLClientKey string `mapstructure:"ssl_client_key"` |
| 36 |
SSLHostname string `mapstructure:"ssl_hostname"` |
| 37 |
SSLCertHostname string `mapstructure:"ssl_cert_hostname"` |
| 38 |
SSLSNIHostname string `mapstructure:"ssl_sni_hostname"` |
| 39 |
MinTLSVersion string `mapstructure:"min_tls_version"` |
| 40 |
MaxTLSVersion string `mapstructure:"max_tls_version"` |
| 41 |
SSLCiphers []string `mapstructure:"ssl_ciphers"` |
| 42 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 43 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 44 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
type backendsByName []*Backend |
| 49 |
|
| 50 |
|
| 51 |
func (s backendsByName) Len() int { return len(s) } |
| 52 |
func (s backendsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 53 |
func (s backendsByName) Less(i, j int) bool { |
| 54 |
return s[i].Name < s[j].Name |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
type ListBackendsInput struct { |
| 59 |
|
| 60 |
Service string |
| 61 |
|
| 62 |
|
| 63 |
Version int |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (c *Client) ListBackends(i *ListBackendsInput) ([]*Backend, error) { |
| 68 |
if i.Service == "" { |
| 69 |
return nil, ErrMissingService |
| 70 |
} |
| 71 |
|
| 72 |
if i.Version == 0 { |
| 73 |
return nil, ErrMissingVersion |
| 74 |
} |
| 75 |
|
| 76 |
path := fmt.Sprintf("/service/%s/version/%d/backend", i.Service, i.Version) |
| 77 |
resp, err := c.Get(path, nil) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
|
| 82 |
var bs []*Backend |
| 83 |
if err := decodeJSON(&bs, resp.Body); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
sort.Stable(backendsByName(bs)) |
| 87 |
return bs, nil |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
type CreateBackendInput struct { |
| 92 |
|
| 93 |
|
| 94 |
Service string |
| 95 |
Version int |
| 96 |
|
| 97 |
Name string `form:"name,omitempty"` |
| 98 |
Comment string `form:"comment,omitempty"` |
| 99 |
Address string `form:"address,omitempty"` |
| 100 |
Port uint `form:"port,omitempty"` |
| 101 |
OverrideHost string `form:"override_host,omitempty"` |
| 102 |
ConnectTimeout uint `form:"connect_timeout,omitempty"` |
| 103 |
MaxConn uint `form:"max_conn,omitempty"` |
| 104 |
ErrorThreshold uint `form:"error_threshold,omitempty"` |
| 105 |
FirstByteTimeout uint `form:"first_byte_timeout,omitempty"` |
| 106 |
BetweenBytesTimeout uint `form:"between_bytes_timeout,omitempty"` |
| 107 |
AutoLoadbalance *Compatibool `form:"auto_loadbalance,omitempty"` |
| 108 |
Weight uint `form:"weight,omitempty"` |
| 109 |
RequestCondition string `form:"request_condition,omitempty"` |
| 110 |
HealthCheck string `form:"healthcheck,omitempty"` |
| 111 |
Shield string `form:"shield,omitempty"` |
| 112 |
UseSSL *Compatibool `form:"use_ssl,omitempty"` |
| 113 |
SSLCheckCert *Compatibool `form:"ssl_check_cert,omitempty"` |
| 114 |
SSLCACert string `form:"ssl_ca_cert,omitempty"` |
| 115 |
SSLClientCert string `form:"ssl_client_cert,omitempty"` |
| 116 |
SSLClientKey string `form:"ssl_client_key,omitempty"` |
| 117 |
SSLHostname string `form:"ssl_hostname,omitempty"` |
| 118 |
SSLCertHostname string `form:"ssl_cert_hostname,omitempty"` |
| 119 |
SSLSNIHostname string `form:"ssl_sni_hostname,omitempty"` |
| 120 |
MinTLSVersion string `form:"min_tls_version,omitempty"` |
| 121 |
MaxTLSVersion string `form:"max_tls_version,omitempty"` |
| 122 |
SSLCiphers []string `form:"ssl_ciphers,omitempty"` |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (c *Client) CreateBackend(i *CreateBackendInput) (*Backend, error) { |
| 127 |
if i.Service == "" { |
| 128 |
return nil, ErrMissingService |
| 129 |
} |
| 130 |
|
| 131 |
if i.Version == 0 { |
| 132 |
return nil, ErrMissingVersion |
| 133 |
} |
| 134 |
|
| 135 |
path := fmt.Sprintf("/service/%s/version/%d/backend", i.Service, i.Version) |
| 136 |
resp, err := c.PostForm(path, i, nil) |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
|
| 141 |
var b *Backend |
| 142 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
return b, nil |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
type GetBackendInput struct { |
| 150 |
|
| 151 |
|
| 152 |
Service string |
| 153 |
Version int |
| 154 |
|
| 155 |
|
| 156 |
Name string |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (c *Client) GetBackend(i *GetBackendInput) (*Backend, error) { |
| 161 |
if i.Service == "" { |
| 162 |
return nil, ErrMissingService |
| 163 |
} |
| 164 |
|
| 165 |
if i.Version == 0 { |
| 166 |
return nil, ErrMissingVersion |
| 167 |
} |
| 168 |
|
| 169 |
if i.Name == "" { |
| 170 |
return nil, ErrMissingName |
| 171 |
} |
| 172 |
|
| 173 |
path := fmt.Sprintf("/service/%s/version/%d/backend/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 174 |
resp, err := c.Get(path, nil) |
| 175 |
if err != nil { |
| 176 |
return nil, err |
| 177 |
} |
| 178 |
|
| 179 |
var b *Backend |
| 180 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
return b, nil |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
type UpdateBackendInput struct { |
| 188 |
|
| 189 |
|
| 190 |
Service string |
| 191 |
Version int |
| 192 |
|
| 193 |
|
| 194 |
Name string |
| 195 |
|
| 196 |
NewName string `form:"name,omitempty"` |
| 197 |
Comment string `form:"comment,omitempty"` |
| 198 |
Address string `form:"address,omitempty"` |
| 199 |
Port uint `form:"port,omitempty"` |
| 200 |
OverrideHost string `form:"override_host,omitempty"` |
| 201 |
ConnectTimeout uint `form:"connect_timeout,omitempty"` |
| 202 |
MaxConn uint `form:"max_conn,omitempty"` |
| 203 |
ErrorThreshold uint `form:"error_threshold,omitempty"` |
| 204 |
FirstByteTimeout uint `form:"first_byte_timeout,omitempty"` |
| 205 |
BetweenBytesTimeout uint `form:"between_bytes_timeout,omitempty"` |
| 206 |
AutoLoadbalance *Compatibool `form:"auto_loadbalance,omitempty"` |
| 207 |
Weight uint `form:"weight,omitempty"` |
| 208 |
RequestCondition string `form:"request_condition,omitempty"` |
| 209 |
HealthCheck string `form:"healthcheck,omitempty"` |
| 210 |
Shield string `form:"shield,omitempty"` |
| 211 |
UseSSL *Compatibool `form:"use_ssl,omitempty"` |
| 212 |
SSLCheckCert *Compatibool `form:"ssl_check_cert,omitempty"` |
| 213 |
SSLCACert string `form:"ssl_ca_cert,omitempty"` |
| 214 |
SSLClientCert string `form:"ssl_client_cert,omitempty"` |
| 215 |
SSLClientKey string `form:"ssl_client_key,omitempty"` |
| 216 |
SSLHostname string `form:"ssl_hostname,omitempty"` |
| 217 |
SSLCertHostname string `form:"ssl_cert_hostname,omitempty"` |
| 218 |
SSLSNIHostname string `form:"ssl_sni_hostname,omitempty"` |
| 219 |
MinTLSVersion string `form:"min_tls_version,omitempty"` |
| 220 |
MaxTLSVersion string `form:"max_tls_version,omitempty"` |
| 221 |
SSLCiphers []string `form:"ssl_ciphers,omitempty"` |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (c *Client) UpdateBackend(i *UpdateBackendInput) (*Backend, 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/backend/%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 *Backend |
| 245 |
if err := decodeJSON(&b, resp.Body); err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
return b, nil |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
type DeleteBackendInput struct { |
| 253 |
|
| 254 |
|
| 255 |
Service string |
| 256 |
Version int |
| 257 |
|
| 258 |
|
| 259 |
Name string |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
func (c *Client) DeleteBackend(i *DeleteBackendInput) 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/backend/%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 |
} |