| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"sort" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Splunk struct { |
| 12 |
ServiceID string `mapstructure:"service_id"` |
| 13 |
Version int `mapstructure:"version"` |
| 14 |
|
| 15 |
Name string `mapstructure:"name"` |
| 16 |
URL string `mapstructure:"url"` |
| 17 |
Format string `mapstructure:"format"` |
| 18 |
FormatVersion uint `mapstructure:"format_version"` |
| 19 |
ResponseCondition string `mapstructure:"response_condition"` |
| 20 |
Placement string `mapstructure:"placement"` |
| 21 |
Token string `mapstructure:"token"` |
| 22 |
CreatedAt *time.Time `mapstructure:"created_at"` |
| 23 |
UpdatedAt *time.Time `mapstructure:"updated_at"` |
| 24 |
DeletedAt *time.Time `mapstructure:"deleted_at"` |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
type splunkByName []*Splunk |
| 29 |
|
| 30 |
|
| 31 |
func (s splunkByName) Len() int { return len(s) } |
| 32 |
func (s splunkByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 33 |
func (s splunkByName) Less(i, j int) bool { |
| 34 |
return s[i].Name < s[j].Name |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
type ListSplunksInput struct { |
| 39 |
|
| 40 |
Service string |
| 41 |
|
| 42 |
|
| 43 |
Version int |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (c *Client) ListSplunks(i *ListSplunksInput) ([]*Splunk, error) { |
| 48 |
if i.Service == "" { |
| 49 |
return nil, ErrMissingService |
| 50 |
} |
| 51 |
|
| 52 |
if i.Version == 0 { |
| 53 |
return nil, ErrMissingVersion |
| 54 |
} |
| 55 |
|
| 56 |
path := fmt.Sprintf("/service/%s/version/%d/logging/splunk", i.Service, i.Version) |
| 57 |
resp, err := c.Get(path, nil) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
|
| 62 |
var ss []*Splunk |
| 63 |
if err := decodeJSON(&ss, resp.Body); err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
sort.Stable(splunkByName(ss)) |
| 67 |
return ss, nil |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
type CreateSplunkInput struct { |
| 72 |
|
| 73 |
|
| 74 |
Service string |
| 75 |
Version int |
| 76 |
|
| 77 |
Name string `form:"name,omitempty"` |
| 78 |
URL string `form:"url,omitempty"` |
| 79 |
Format string `form:"format,omitempty"` |
| 80 |
FormatVersion uint `form:"format_version,omitempty"` |
| 81 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 82 |
Placement string `form:"placement,omitempty"` |
| 83 |
Token string `form:"token,omitempty"` |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (c *Client) CreateSplunk(i *CreateSplunkInput) (*Splunk, error) { |
| 88 |
if i.Service == "" { |
| 89 |
return nil, ErrMissingService |
| 90 |
} |
| 91 |
|
| 92 |
if i.Version == 0 { |
| 93 |
return nil, ErrMissingVersion |
| 94 |
} |
| 95 |
|
| 96 |
path := fmt.Sprintf("/service/%s/version/%d/logging/splunk", i.Service, i.Version) |
| 97 |
resp, err := c.PostForm(path, i, nil) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
|
| 102 |
var s *Splunk |
| 103 |
if err := decodeJSON(&s, resp.Body); err != nil { |
| 104 |
return nil, err |
| 105 |
} |
| 106 |
return s, nil |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
type GetSplunkInput struct { |
| 111 |
|
| 112 |
|
| 113 |
Service string |
| 114 |
Version int |
| 115 |
|
| 116 |
|
| 117 |
Name string |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (c *Client) GetSplunk(i *GetSplunkInput) (*Splunk, error) { |
| 122 |
if i.Service == "" { |
| 123 |
return nil, ErrMissingService |
| 124 |
} |
| 125 |
|
| 126 |
if i.Version == 0 { |
| 127 |
return nil, ErrMissingVersion |
| 128 |
} |
| 129 |
|
| 130 |
if i.Name == "" { |
| 131 |
return nil, ErrMissingName |
| 132 |
} |
| 133 |
|
| 134 |
path := fmt.Sprintf("/service/%s/version/%d/logging/splunk/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 135 |
resp, err := c.Get(path, nil) |
| 136 |
if err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
|
| 140 |
var s *Splunk |
| 141 |
if err := decodeJSON(&s, resp.Body); err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
return s, nil |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
type UpdateSplunkInput struct { |
| 149 |
|
| 150 |
|
| 151 |
Service string |
| 152 |
Version int |
| 153 |
|
| 154 |
|
| 155 |
Name string |
| 156 |
|
| 157 |
NewName string `form:"name,omitempty"` |
| 158 |
URL string `form:"url,omitempty"` |
| 159 |
Format string `form:"format,omitempty"` |
| 160 |
FormatVersion uint `form:"format_version,omitempty"` |
| 161 |
ResponseCondition string `form:"response_condition,omitempty"` |
| 162 |
Placement string `form:"placement,omitempty"` |
| 163 |
Token string `form:"token,omitempty"` |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
func (c *Client) UpdateSplunk(i *UpdateSplunkInput) (*Splunk, 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/logging/splunk/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 181 |
resp, err := c.PutForm(path, i, nil) |
| 182 |
if err != nil { |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
|
| 186 |
var s *Splunk |
| 187 |
if err := decodeJSON(&s, resp.Body); err != nil { |
| 188 |
return nil, err |
| 189 |
} |
| 190 |
return s, nil |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
type DeleteSplunkInput struct { |
| 195 |
|
| 196 |
|
| 197 |
Service string |
| 198 |
Version int |
| 199 |
|
| 200 |
|
| 201 |
Name string |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (c *Client) DeleteSplunk(i *DeleteSplunkInput) error { |
| 206 |
if i.Service == "" { |
| 207 |
return ErrMissingService |
| 208 |
} |
| 209 |
|
| 210 |
if i.Version == 0 { |
| 211 |
return ErrMissingVersion |
| 212 |
} |
| 213 |
|
| 214 |
if i.Name == "" { |
| 215 |
return ErrMissingName |
| 216 |
} |
| 217 |
|
| 218 |
path := fmt.Sprintf("/service/%s/version/%d/logging/splunk/%s", i.Service, i.Version, url.PathEscape(i.Name)) |
| 219 |
resp, err := c.Delete(path, nil) |
| 220 |
if err != nil { |
| 221 |
return err |
| 222 |
} |
| 223 |
|
| 224 |
var r *statusResp |
| 225 |
if err := decodeJSON(&r, resp.Body); err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
if !r.Ok() { |
| 229 |
return fmt.Errorf("Not Ok") |
| 230 |
} |
| 231 |
return nil |
| 232 |
} |