Debian Codegraph @

fastly/syslog.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // Syslog represents a syslog response from the Fastly API.
11 type Syslog struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Address string `mapstructure:"address"`
17 Hostname string `mapstructure:"hostname"`
18 Port uint `mapstructure:"port"`
19 UseTLS bool `mapstructure:"use_tls"`
20 IPV4 string `mapstructure:"ipv4"`
21 TLSCACert string `mapstructure:"tls_ca_cert"`
22 TLSHostname string `mapstructure:"tls_hostname"`
23 Token string `mapstructure:"token"`
24 Format string `mapstructure:"format"`
25 FormatVersion uint `mapstructure:"format_version"`
26 MessageType string `mapstructure:"message_type"`
27 ResponseCondition string `mapstructure:"response_condition"`
28 Placement string `mapstructure:"placement"`
29 CreatedAt *time.Time `mapstructure:"created_at"`
30 UpdatedAt *time.Time `mapstructure:"updated_at"`
31 DeletedAt *time.Time `mapstructure:"deleted_at"`
32 }
33
34 // syslogsByName is a sortable list of syslogs.
35 type syslogsByName []*Syslog
36
37 // Len, Swap, and Less implement the sortable interface.
38 func (s syslogsByName) Len() int { return len(s) }
39 func (s syslogsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
40 func (s syslogsByName) Less(i, j int) bool {
41 return s[i].Name < s[j].Name
42 }
43
44 // ListSyslogsInput is used as input to the ListSyslogs function.
45 type ListSyslogsInput struct {
46 // Service is the ID of the service (required).
47 Service string
48
49 // Version is the specific configuration version (required).
50 Version int
51 }
52
53 // ListSyslogs returns the list of syslogs for the configuration version.
54 func (c *Client) ListSyslogs(i *ListSyslogsInput) ([]*Syslog, error) {
55 if i.Service == "" {
56 return nil, ErrMissingService
57 }
58
59 if i.Version == 0 {
60 return nil, ErrMissingVersion
61 }
62
63 path := fmt.Sprintf("/service/%s/version/%d/logging/syslog", i.Service, i.Version)
64 resp, err := c.Get(path, nil)
65 if err != nil {
66 return nil, err
67 }
68
69 var ss []*Syslog
70 if err := decodeJSON(&ss, resp.Body); err != nil {
71 return nil, err
72 }
73 sort.Stable(syslogsByName(ss))
74 return ss, nil
75 }
76
77 // CreateSyslogInput is used as input to the CreateSyslog function.
78 type CreateSyslogInput struct {
79 // Service is the ID of the service. Version is the specific configuration
80 // version. Both fields are required.
81 Service string
82 Version int
83
84 Name string `form:"name,omitempty"`
85 Address string `form:"address,omitempty"`
86 Hostname string `form:"hostname,omitempty"`
87 Port uint `form:"port,omitempty"`
88 UseTLS *Compatibool `form:"use_tls,omitempty"`
89 IPV4 string `form:"ipv4,omitempty"`
90 TLSCACert string `form:"tls_ca_cert,omitempty"`
91 TLSHostname string `form:"tls_hostname,omitempty"`
92 Token string `form:"token,omitempty"`
93 Format string `form:"format,omitempty"`
94 FormatVersion uint `form:"format_version,omitempty"`
95 MessageType string `form:"message_type,omitempty"`
96 ResponseCondition string `form:"response_condition,omitempty"`
97 Placement string `form:"placement,omitempty"`
98 }
99
100 // CreateSyslog creates a new Fastly syslog.
101 func (c *Client) CreateSyslog(i *CreateSyslogInput) (*Syslog, error) {
102 if i.Service == "" {
103 return nil, ErrMissingService
104 }
105
106 if i.Version == 0 {
107 return nil, ErrMissingVersion
108 }
109
110 path := fmt.Sprintf("/service/%s/version/%d/logging/syslog", i.Service, i.Version)
111 resp, err := c.PostForm(path, i, nil)
112 if err != nil {
113 return nil, err
114 }
115
116 var s *Syslog
117 if err := decodeJSON(&s, resp.Body); err != nil {
118 return nil, err
119 }
120 return s, nil
121 }
122
123 // GetSyslogInput is used as input to the GetSyslog function.
124 type GetSyslogInput struct {
125 // Service is the ID of the service. Version is the specific configuration
126 // version. Both fields are required.
127 Service string
128 Version int
129
130 // Name is the name of the syslog to fetch.
131 Name string
132 }
133
134 // GetSyslog gets the syslog configuration with the given parameters.
135 func (c *Client) GetSyslog(i *GetSyslogInput) (*Syslog, error) {
136 if i.Service == "" {
137 return nil, ErrMissingService
138 }
139
140 if i.Version == 0 {
141 return nil, ErrMissingVersion
142 }
143
144 if i.Name == "" {
145 return nil, ErrMissingName
146 }
147
148 path := fmt.Sprintf("/service/%s/version/%d/logging/syslog/%s", i.Service, i.Version, url.PathEscape(i.Name))
149 resp, err := c.Get(path, nil)
150 if err != nil {
151 return nil, err
152 }
153
154 var s *Syslog
155 if err := decodeJSON(&s, resp.Body); err != nil {
156 return nil, err
157 }
158 return s, nil
159 }
160
161 // UpdateSyslogInput is used as input to the UpdateSyslog function.
162 type UpdateSyslogInput struct {
163 // Service is the ID of the service. Version is the specific configuration
164 // version. Both fields are required.
165 Service string
166 Version int
167
168 // Name is the name of the syslog to update.
169 Name string
170
171 NewName string `form:"name,omitempty"`
172 Address string `form:"address,omitempty"`
173 Hostname string `form:"hostname,omitempty"`
174 Port uint `form:"port,omitempty"`
175 UseTLS *Compatibool `form:"use_tls,omitempty"`
176 IPV4 string `form:"ipv4,omitempty"`
177 TLSCACert string `form:"tls_ca_cert,omitempty"`
178 TLSHostname string `form:"tls_hostname,omitempty"`
179 Token string `form:"token,omitempty"`
180 Format string `form:"format,omitempty"`
181 FormatVersion uint `form:"format_version,omitempty"`
182 MessageType string `form:"message_type,omitempty"`
183 ResponseCondition string `form:"response_condition,omitempty"`
184 Placement string `form:"placement,omitempty"`
185 }
186
187 // UpdateSyslog updates a specific syslog.
188 func (c *Client) UpdateSyslog(i *UpdateSyslogInput) (*Syslog, error) {
189 if i.Service == "" {
190 return nil, ErrMissingService
191 }
192
193 if i.Version == 0 {
194 return nil, ErrMissingVersion
195 }
196
197 if i.Name == "" {
198 return nil, ErrMissingName
199 }
200
201 path := fmt.Sprintf("/service/%s/version/%d/logging/syslog/%s", i.Service, i.Version, url.PathEscape(i.Name))
202 resp, err := c.PutForm(path, i, nil)
203 if err != nil {
204 return nil, err
205 }
206
207 var s *Syslog
208 if err := decodeJSON(&s, resp.Body); err != nil {
209 return nil, err
210 }
211 return s, nil
212 }
213
214 // DeleteSyslogInput is the input parameter to DeleteSyslog.
215 type DeleteSyslogInput struct {
216 // Service is the ID of the service. Version is the specific configuration
217 // version. Both fields are required.
218 Service string
219 Version int
220
221 // Name is the name of the syslog to delete (required).
222 Name string
223 }
224
225 // DeleteSyslog deletes the given syslog version.
226 func (c *Client) DeleteSyslog(i *DeleteSyslogInput) error {
227 if i.Service == "" {
228 return ErrMissingService
229 }
230
231 if i.Version == 0 {
232 return ErrMissingVersion
233 }
234
235 if i.Name == "" {
236 return ErrMissingName
237 }
238
239 path := fmt.Sprintf("/service/%s/version/%d/logging/syslog/%s", i.Service, i.Version, url.PathEscape(i.Name))
240 resp, err := c.Delete(path, nil)
241 if err != nil {
242 return err
243 }
244
245 var r *statusResp
246 if err := decodeJSON(&r, resp.Body); err != nil {
247 return err
248 }
249 if !r.Ok() {
250 return fmt.Errorf("Not Ok")
251 }
252 return nil
253 }