Debian Codegraph @

fastly/papertrail.go

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