Debian Codegraph @

fastly/vcl_snippets.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 const (
11 // SnippetTypeInit sets the type to init
12 SnippetTypeInit SnippetType = "init"
13
14 // SnippetTypeRecv sets the type to recv
15 SnippetTypeRecv SnippetType = "recv"
16
17 // SnippetTypeHit sets the type to hit
18 SnippetTypeHit SnippetType = "hit"
19
20 // SnippetTypeMiss sets the type to miss
21 SnippetTypeMiss SnippetType = "miss"
22
23 // SnippetTypePass sets the type to pass
24 SnippetTypePass SnippetType = "pass"
25
26 // SnippetTypeFetch sets the type to fetch
27 SnippetTypeFetch SnippetType = "fetch"
28
29 // SnippetTypeError sets the type to error
30 SnippetTypeError SnippetType = "error"
31
32 // SnippetTypeDeliver sets the type to deliver
33 SnippetTypeDeliver SnippetType = "deliver"
34
35 // SnippetTypeLog sets the type to log
36 SnippetTypeLog SnippetType = "log"
37
38 // SnippetTypeNone sets the type to none
39 SnippetTypeNone SnippetType = "none"
40 )
41
42 // SnippetType is the type of VCL Snippet
43 type SnippetType string
44
45 // Snippet is the Fastly Snippet object
46 type Snippet struct {
47 ServiceID string `mapstructure:"service_id"`
48 Version int `mapstructure:"version"`
49
50 Name string `mapstructure:"name"`
51 ID string `mapstructure:"id"`
52 Priority int `mapstructure:"priority"`
53 Dynamic int `mapstructure:"dynamic"`
54 Content string `mapstructure:"content"`
55 Type SnippetType `mapstructure:"type"`
56 CreatedAt *time.Time `mapstructure:"created_at"`
57 UpdatedAt *time.Time `mapstructure:"updated_at"`
58 DeletedAt *time.Time `mapstructure:"deleted_at"`
59 }
60
61 // CreateSnippetInput is the input for CreateSnippet
62 type CreateSnippetInput struct {
63 // Service is the ID of the Service to add the snippet to.
64 Service string
65
66 // Version is the editable version of the service
67 Version int
68
69 // Name is the name for the snippet.
70 Name string `form:"name"`
71
72 // Priority determines the ordering for multiple snippets. Lower numbers execute first.
73 Priority int `form:"priority"`
74
75 // Dynamic sets the snippet version to regular (0) or dynamic (1).
76 Dynamic int `form:"dynamic"`
77
78 // Content is the VCL code that specifies exactly what the snippet does.
79 Content string `form:"content"`
80
81 // Type is the location in generated VCL where the snippet should be placed.
82 Type SnippetType `form:"type"`
83 }
84
85 // CreateSnippet creates a new snippet or dynamic snippet on a unlocked version
86 func (c *Client) CreateSnippet(i *CreateSnippetInput) (*Snippet, error) {
87 if i.Service == "" {
88 return nil, ErrMissingService
89 }
90
91 if i.Version == 0 {
92 return nil, ErrMissingVersion
93 }
94
95 if i.Name == "" {
96 return nil, ErrMissingName
97 }
98
99 if i.Dynamic == 0 && i.Content == "" {
100 return nil, ErrMissingContent
101 }
102
103 path := fmt.Sprintf("/service/%s/version/%d/snippet", i.Service, i.Version)
104 resp, err := c.PostForm(path, i, nil)
105 if err != nil {
106 return nil, err
107 }
108
109 var snippet *Snippet
110 if err := decodeJSON(&snippet, resp.Body); err != nil {
111 return nil, err
112 }
113 return snippet, err
114 }
115
116 // UpdateSnippetInput is the input for UpdateSnippet
117 type UpdateSnippetInput struct {
118 // Service is the ID of the Service to add the snippet to.
119 Service string
120
121 // Version is the editable version of the service
122 Version int
123
124 Name string
125
126 // Name is the name for the snippet.
127 NewName string `form:"name"`
128
129 // Priority determines the ordering for multiple snippets. Lower numbers execute first.
130 Priority int `form:"priority"`
131
132 // Dynamic sets the snippet version to regular (0) or dynamic (1).
133 Dynamic int `form:"dynamic"`
134
135 // Content is the VCL code that specifies exactly what the snippet does.
136 Content string `form:"content"`
137
138 // Type is the location in generated VCL where the snippet should be placed.
139 Type SnippetType `form:"type"`
140 }
141
142 // UpdateSnippet updates a snippet on a unlocked version
143 func (c *Client) UpdateSnippet(i *UpdateSnippetInput) (*Snippet, error) {
144 if i.Service == "" {
145 return nil, ErrMissingService
146 }
147
148 if i.Version == 0 {
149 return nil, ErrMissingVersion
150 }
151
152 if i.Name == "" {
153 return nil, ErrMissingName
154 }
155
156 if i.Dynamic == 0 && i.Content == "" {
157 return nil, ErrMissingContent
158 }
159
160 path := fmt.Sprintf("/service/%s/version/%d/snippet/%s", i.Service, i.Version, url.PathEscape(i.Name))
161 resp, err := c.PutForm(path, i, nil)
162 if err != nil {
163 return nil, err
164 }
165
166 var snippet *Snippet
167 if err := decodeJSON(&snippet, resp.Body); err != nil {
168 return nil, err
169 }
170 return snippet, err
171 }
172
173 // DynamicSnippet is the object returned when updating or retrieving a Dynamic Snippet
174 type DynamicSnippet struct {
175 ServiceID string `mapstructure:"service_id"`
176 ID string `mapstructure:"snippet_id"`
177
178 Content string `mapstructure:"content"`
179 CreatedAt *time.Time `mapstructure:"created_at"`
180 UpdatedAt *time.Time `mapstructure:"updated_at"`
181 }
182
183 // UpdateDynamicSnippetInput is the input for UpdateDynamicSnippet
184 type UpdateDynamicSnippetInput struct {
185 // Service is the ID of the Service to add the snippet to.
186 Service string
187
188 // ID is the ID of the Snippet to modify
189 ID string
190
191 // Content is the VCL code that specifies exactly what the snippet does.
192 Content string `form:"content"`
193 }
194
195 // UpdateDynamicSnippet replaces the content of a Dynamic Snippet
196 func (c *Client) UpdateDynamicSnippet(i *UpdateDynamicSnippetInput) (*DynamicSnippet, error) {
197 if i.Service == "" {
198 return nil, ErrMissingService
199 }
200
201 if i.ID == "" {
202 return nil, ErrMissingID
203 }
204
205 path := fmt.Sprintf("/service/%s/snippet/%s", i.Service, i.ID)
206 resp, err := c.PutForm(path, i, nil)
207 if err != nil {
208 return nil, err
209 }
210
211 var updateSnippet *DynamicSnippet
212 if err := decodeJSON(&updateSnippet, resp.Body); err != nil {
213 return nil, err
214 }
215 return updateSnippet, err
216 }
217
218 type DeleteSnippetInput struct {
219 // Service is the ID of the Service to add the snippet to.
220 Service string
221
222 // Name is the Name of the Snippet to Delete
223 Name string
224
225 // Version is the editable version of the service
226 Version int
227 }
228
229 func (c *Client) DeleteSnippet(i *DeleteSnippetInput) error {
230 if i.Service == "" {
231 return ErrMissingService
232 }
233
234 if i.Version == 0 {
235 return ErrMissingVersion
236 }
237
238 if i.Name == "" {
239 return ErrMissingName
240 }
241
242 path := fmt.Sprintf("/service/%s/version/%d/snippet/%s", i.Service, i.Version, url.PathEscape(i.Name))
243 resp, err := c.Delete(path, nil)
244 if err != nil {
245 return err
246 }
247
248 var r *statusResp
249 if err := decodeJSON(&r, resp.Body); err != nil {
250 return err
251 }
252 if !r.Ok() {
253 return fmt.Errorf("Not Ok")
254 }
255 return nil
256 }
257
258 // ListSnippetsInput is used as input to the ListSnippets function.
259 type ListSnippetsInput struct {
260 // Service is the ID of the service (required).
261 Service string
262
263 // Version is the specific configuration version (required).
264 Version int
265 }
266
267 // snippetsByName is a sortable list of Snippets.
268 type snippetsByName []*Snippet
269
270 // Len, Swap, and Less implement the sortable interface.
271 func (s snippetsByName) Len() int { return len(s) }
272 func (s snippetsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
273 func (s snippetsByName) Less(i, j int) bool {
274 return s[i].Name < s[j].Name
275 }
276
277 // ListSnippets returns the list of Snippets for the configuration version. Content is not displayed for Dynmanic Snippets due to them being
278 // versionless, use the GetDynamicSnippet function to show current content.
279 func (c *Client) ListSnippets(i *ListSnippetsInput) ([]*Snippet, error) {
280 if i.Service == "" {
281 return nil, ErrMissingService
282 }
283
284 if i.Version == 0 {
285 return nil, ErrMissingVersion
286 }
287
288 path := fmt.Sprintf("/service/%s/version/%d/snippet", i.Service, i.Version)
289 resp, err := c.Get(path, nil)
290 if err != nil {
291 return nil, err
292 }
293
294 var snippets []*Snippet
295 if err := decodeJSON(&snippets, resp.Body); err != nil {
296 return nil, err
297 }
298 sort.Stable(snippetsByName(snippets))
299 return snippets, nil
300 }
301
302 // GetSnippetInput is used as input to the GetSnippet function.
303 type GetSnippetInput struct {
304 // Service is the ID of the service. Version is the specific configuration
305 // version. Both fields are required.
306 Service string
307 Version int
308
309 // Name is the name of the Snippet to fetch.
310 Name string
311 }
312
313 // GetSnippet gets the Snippet configuration with the given parameters. Dynamic Snippets will not show content due to them
314 // being versionless, use GetDynamicSnippet to see content.
315 func (c *Client) GetSnippet(i *GetSnippetInput) (*Snippet, error) {
316 if i.Service == "" {
317 return nil, ErrMissingService
318 }
319
320 if i.Version == 0 {
321 return nil, ErrMissingVersion
322 }
323
324 if i.Name == "" {
325 return nil, ErrMissingName
326 }
327
328 path := fmt.Sprintf("/service/%s/version/%d/snippet/%s", i.Service, i.Version, url.PathEscape(i.Name))
329 resp, err := c.Get(path, nil)
330 if err != nil {
331 return nil, err
332 }
333
334 var snippet *Snippet
335 if err := decodeJSON(&snippet, resp.Body); err != nil {
336 return nil, err
337 }
338 return snippet, nil
339 }
340
341 // GetDynamicSnippetInput is used as input to the GetDynamicSnippet function.
342 type GetDynamicSnippetInput struct {
343 // Service is the ID of the service.
344 Service string
345
346 // ID is the ID of the Snippet to fetch.
347 ID string
348 }
349
350 // GetDynamicSnippet gets the Snippet configuration with the given parameters. This will show the current content
351 // associated with a Dynamic Snippet.
352 func (c *Client) GetDynamicSnippet(i *GetDynamicSnippetInput) (*DynamicSnippet, error) {
353 if i.Service == "" {
354 return nil, ErrMissingService
355 }
356
357 if i.ID == "" {
358 return nil, ErrMissingID
359 }
360
361 path := fmt.Sprintf("/service/%s/snippet/%s", i.Service, i.ID)
362 resp, err := c.Get(path, nil)
363 if err != nil {
364 return nil, err
365 }
366
367 var snippet *DynamicSnippet
368 if err := decodeJSON(&snippet, resp.Body); err != nil {
369 return nil, err
370 }
371 return snippet, nil
372 }