Debian Codegraph @

fastly/splunk.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // Splunk represents a splunk response from the Fastly API.
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 // splunkByName is a sortable list of splunks.
28 type splunkByName []*Splunk
29
30 // Len, Swap, and Less implement the sortable interface.
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 // ListSplunksInput is used as input to the ListSplunks function.
38 type ListSplunksInput struct {
39 // Service is the ID of the service (required).
40 Service string
41
42 // Version is the specific configuration version (required).
43 Version int
44 }
45
46 // ListSplunks returns the list of splunks for the configuration version.
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 // CreateSplunkInput is used as input to the CreateSplunk function.
71 type CreateSplunkInput struct {
72 // Service is the ID of the service. Version is the specific configuration
73 // version. Both fields are required.
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 // CreateSplunk creates a new Fastly splunk.
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 // GetSplunkInput is used as input to the GetSplunk function.
110 type GetSplunkInput struct {
111 // Service is the ID of the service. Version is the specific configuration
112 // version. Both fields are required.
113 Service string
114 Version int
115
116 // Name is the name of the splunk to fetch.
117 Name string
118 }
119
120 // GetSplunk gets the splunk configuration with the given parameters.
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 // UpdateSplunkInput is used as input to the UpdateSplunk function.
148 type UpdateSplunkInput struct {
149 // Service is the ID of the service. Version is the specific configuration
150 // version. Both fields are required.
151 Service string
152 Version int
153
154 // Name is the name of the splunk to update.
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 // UpdateSplunk updates a specific splunk.
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 // DeleteSplunkInput is the input parameter to DeleteSplunk.
194 type DeleteSplunkInput struct {
195 // Service is the ID of the service. Version is the specific configuration
196 // version. Both fields are required.
197 Service string
198 Version int
199
200 // Name is the name of the splunk to delete (required).
201 Name string
202 }
203
204 // DeleteSplunk deletes the given splunk version.
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 }