| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
type S3Redundancy string |
| 11 |
|
| 12 |
const ( |
| 13 |
S3RedundancyStandard S3Redundancy = "standard" |
| 14 |
S3RedundancyReduced S3Redundancy = "reduced_redundancy" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type S3 struct { |
| 19 |
ServiceID string `mapstructure:"service_id"` |
| 20 |
Version int `mapstructure:"version"` |
| 21 |
|
| 22 |
Name string `mapstructure:"name"` |
| 23 |
BucketName string `mapstructure:"bucket_name"` |
| 24 |
Domain string `mapstructure:"domain"` |
| 25 |
AccessKey string `mapstructure:"access_key"` |
| 26 |
SecretKey string `mapstructure:"secret_key"` |
| 27 |
Path string `mapstructure:"path"` |
| 28 |
Period uint `mapstructure:"period"` |
| 29 |
GzipLevel uint `mapstructure:"gzip_level"` |
| 30 |
Format string `mapstructure:"format"` |
| 31 |
FormatVersion uint `mapstructure:"format_version"` |
| 32 |
ResponseCondition string `mapstructure:"response_condition"` |
| 33 |
MessageType string `mapstructure:"message_type"` |
| 34 |
TimestampFormat string `mapstructure:"timestamp_format"` |
| 35 |
Placement string `mapstructure:"placement"` |
| 36 |
Redundancy S3Redundancy `mapstructure:"redundancy"` |
| 37 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 38 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 39 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
type s3sByName []*S3 |
| 44 |
|
| 45 |
|
| 46 |
func (s s3sByName) Len() int { return len(s) } |
| 47 |
func (s s3sByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 48 |
func (s s3sByName) Less(i, j int) bool { |
| 49 |
return s[i].Name < s[j].Name |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
type ListS3sInput struct { |
| 54 |
|
| 55 |
Service string |
| 56 |
|
| 57 |
|
| 58 |
Version int |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
func (c *Client) ListS3s(i *ListS3sInput) ([]*S3, error) { |
| 63 |
if i.Service == "" { |
| 64 |
return nil, ErrMissingService |
| 65 |
} |
| 66 |
|
| 67 |
if i.Version == 0 { |
| 68 |
return nil, ErrMissingVersion |
| 69 |
} |
| 70 |
|
| 71 |
path := fmt.Sprintf("/service/%s/version/%d/logging/s3", i.Service, i.Version) |
| 72 |
resp, err := c.Get(path, nil) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
|
| 77 |
var s3s []*S3 |
| 78 |
if err := decodeJSON(&s3s, resp.Body); err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
sort.Stable(s3sByName(s3s)) |
| 82 |
return s3s, nil |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
type CreateS3Input struct { |
| 87 |
|
| 88 |
|
| 89 |
Service string |
| 90 |
Version int |
| 91 |
|
| 92 |
Name string `form:"name,omitempty"` |
| 93 |
BucketName string `form:"bucket_name,omitempty"` |
| 94 |
Domain string `form:"domain,omitempty"` |
| 95 |
AccessKey string `form:"access_key,omitempty"` |
| 96 |
SecretKey string `form:"secret_key,omitempty"` |
| 97 |
Path string `form:"path,omitempty"` |
| 98 |
Period uint `form:"period,omitempty"` |
| 99 |
GzipLevel uint `form:"gzip_level,omitempty"` |
| 100 |
Format string `form:"format,omitempty"` |
| 101 |
MessageType string `form:"message_type,omitempty"` |
| 102 |
FormatVersion uint `form:"format_version,omitempty"` |
| 103 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 104 |
TimestampFormat string `form:"timestamp_format,omitempty"` |
| 105 |
Redundancy S3Redundancy `form:"redundancy,omitempty"` |
| 106 |
Placement string `form:"placement,omitempty"` |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (c *Client) CreateS3(i *CreateS3Input) (*S3, error) { |
| 111 |
if i.Service == "" { |
| 112 |
return nil, ErrMissingService |
| 113 |
} |
| 114 |
|
| 115 |
if i.Version == 0 { |
| 116 |
return nil, ErrMissingVersion |
| 117 |
} |
| 118 |
|
| 119 |
path := fmt.Sprintf("/service/%s/version/%d/logging/s3", i.Service, i.Version) |
| 120 |
resp, err := c.PostForm(path, i, nil) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
|
| 125 |
var s3 *S3 |
| 126 |
if err := decodeJSON(&s3, resp.Body); err != nil { |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
return s3, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
type GetS3Input struct { |
| 134 |
|
| 135 |
|
| 136 |
Service string |
| 137 |
Version int |
| 138 |
|
| 139 |
|
| 140 |
Name string |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (c *Client) GetS3(i *GetS3Input) (*S3, error) { |
| 145 |
if i.Service == "" { |
| 146 |
return nil, ErrMissingService |
| 147 |
} |
| 148 |
|
| 149 |
if i.Version == 0 { |
| 150 |
return nil, ErrMissingVersion |
| 151 |
} |
| 152 |
|
| 153 |
if i.Name == "" { |
| 154 |
return nil, ErrMissingName |
| 155 |
} |
| 156 |
|
| 157 |
path := fmt.Sprintf("/service/%s/version/%d/logging/s3/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 158 |
resp, err := c.Get(path, nil) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
|
| 163 |
var s3 *S3 |
| 164 |
if err := decodeJSON(&s3, resp.Body); err != nil { |
| 165 |
return nil, err |
| 166 |
} |
| 167 |
return s3, nil |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
type UpdateS3Input struct { |
| 172 |
|
| 173 |
|
| 174 |
Service string |
| 175 |
Version int |
| 176 |
|
| 177 |
|
| 178 |
Name string |
| 179 |
|
| 180 |
NewName string `form:"name,omitempty"` |
| 181 |
BucketName string `form:"bucket_name,omitempty"` |
| 182 |
Domain string `form:"domain,omitempty"` |
| 183 |
AccessKey string `form:"access_key,omitempty"` |
| 184 |
SecretKey string `form:"secret_key,omitempty"` |
| 185 |
Path string `form:"path,omitempty"` |
| 186 |
Period uint `form:"period,omitempty"` |
| 187 |
GzipLevel uint `form:"gzip_level,omitempty"` |
| 188 |
Format string `form:"format,omitempty"` |
| 189 |
FormatVersion uint `form:"format_version,omitempty"` |
| 190 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 191 |
MessageType string `form:"message_type,omitempty"` |
| 192 |
TimestampFormat string `form:"timestamp_format,omitempty"` |
| 193 |
Redundancy S3Redundancy `form:"redundancy,omitempty"` |
| 194 |
Placement string `form:"placement,omitempty"` |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
func (c *Client) UpdateS3(i *UpdateS3Input) (*S3, error) { |
| 199 |
if i.Service == "" { |
| 200 |
return nil, ErrMissingService |
| 201 |
} |
| 202 |
|
| 203 |
if i.Version == 0 { |
| 204 |
return nil, ErrMissingVersion |
| 205 |
} |
| 206 |
|
| 207 |
if i.Name == "" { |
| 208 |
return nil, ErrMissingName |
| 209 |
} |
| 210 |
|
| 211 |
path := fmt.Sprintf("/service/%s/version/%d/logging/s3/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 212 |
resp, err := c.PutForm(path, i, nil) |
| 213 |
if err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
|
| 217 |
var s3 *S3 |
| 218 |
if err := decodeJSON(&s3, resp.Body); err != nil { |
| 219 |
return nil, err |
| 220 |
} |
| 221 |
return s3, nil |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
type DeleteS3Input struct { |
| 226 |
|
| 227 |
|
| 228 |
Service string |
| 229 |
Version int |
| 230 |
|
| 231 |
|
| 232 |
Name string |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
func (c *Client) DeleteS3(i *DeleteS3Input) error { |
| 237 |
if i.Service == "" { |
| 238 |
return ErrMissingService |
| 239 |
} |
| 240 |
|
| 241 |
if i.Version == 0 { |
| 242 |
return ErrMissingVersion |
| 243 |
} |
| 244 |
|
| 245 |
if i.Name == "" { |
| 246 |
return ErrMissingName |
| 247 |
} |
| 248 |
|
| 249 |
path := fmt.Sprintf("/service/%s/version/%d/logging/s3/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 250 |
resp, err := c.Delete(path, nil) |
| 251 |
if err != nil { |
| 252 |
return err |
| 253 |
} |
| 254 |
|
| 255 |
var r *statusResp |
| 256 |
if err := decodeJSON(&r, resp.Body); err != nil { |
| 257 |
return err |
| 258 |
} |
| 259 |
if !r.Ok() { |
| 260 |
return fmt.Errorf("Not Ok") |
| 261 |
} |
| 262 |
return nil |
| 263 |
} |