Debian Codegraph @

fastly/logentries.go

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