Debian Codegraph @

fastly/vcl.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // VCL represents a response about VCL from the Fastly API.
11 type VCL struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Main bool `mapstructure:"main"`
17 Content string `mapstructure:"content"`
18 CreatedAt *time.Time `mapstructure:"created_at"`
19 UpdatedAt *time.Time `mapstructure:"updated_at"`
20 DeletedAt *time.Time `mapstructure:"deleted_at"`
21 }
22
23 // vclsByName is a sortable list of VCLs.
24 type vclsByName []*VCL
25
26 // Len, Swap, and Less implement the sortable interface.
27 func (s vclsByName) Len() int { return len(s) }
28 func (s vclsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
29 func (s vclsByName) Less(i, j int) bool {
30 return s[i].Name < s[j].Name
31 }
32
33 // ListVCLsInput is used as input to the ListVCLs function.
34 type ListVCLsInput struct {
35 // Service is the ID of the service (required).
36 Service string
37
38 // Version is the specific configuration version (required).
39 Version int
40 }
41
42 // ListVCLs returns the list of VCLs for the configuration version.
43 func (c *Client) ListVCLs(i *ListVCLsInput) ([]*VCL, error) {
44 if i.Service == "" {
45 return nil, ErrMissingService
46 }
47
48 if i.Version == 0 {
49 return nil, ErrMissingVersion
50 }
51
52 path := fmt.Sprintf("/service/%s/version/%d/vcl", i.Service, i.Version)
53 resp, err := c.Get(path, nil)
54 if err != nil {
55 return nil, err
56 }
57
58 var vcls []*VCL
59 if err := decodeJSON(&vcls, resp.Body); err != nil {
60 return nil, err
61 }
62 sort.Stable(vclsByName(vcls))
63 return vcls, nil
64 }
65
66 // GetVCLInput is used as input to the GetVCL function.
67 type GetVCLInput struct {
68 // Service is the ID of the service. Version is the specific configuration
69 // version. Both fields are required.
70 Service string
71 Version int
72
73 // Name is the name of the VCL to fetch.
74 Name string
75 }
76
77 // GetVCL gets the VCL configuration with the given parameters.
78 func (c *Client) GetVCL(i *GetVCLInput) (*VCL, error) {
79 if i.Service == "" {
80 return nil, ErrMissingService
81 }
82
83 if i.Version == 0 {
84 return nil, ErrMissingVersion
85 }
86
87 if i.Name == "" {
88 return nil, ErrMissingName
89 }
90
91 path := fmt.Sprintf("/service/%s/version/%d/vcl/%s", i.Service, i.Version, url.PathEscape(i.Name))
92 resp, err := c.Get(path, nil)
93 if err != nil {
94 return nil, err
95 }
96
97 var vcl *VCL
98 if err := decodeJSON(&vcl, resp.Body); err != nil {
99 return nil, err
100 }
101 return vcl, nil
102 }
103
104 // GetGeneratedVCLInput is used as input to the GetGeneratedVCL function.
105 type GetGeneratedVCLInput struct {
106 // Service is the ID of the service. Version is the specific configuration
107 // version. Both fields are required.
108 Service string
109 Version int
110 }
111
112 // GetGeneratedVCL gets the VCL configuration with the given parameters.
113 func (c *Client) GetGeneratedVCL(i *GetGeneratedVCLInput) (*VCL, error) {
114 if i.Service == "" {
115 return nil, ErrMissingService
116 }
117
118 if i.Version == 0 {
119 return nil, ErrMissingVersion
120 }
121
122 path := fmt.Sprintf("/service/%s/version/%d/generated_vcl", i.Service, i.Version)
123 resp, err := c.Get(path, nil)
124 if err != nil {
125 return nil, err
126 }
127
128 var vcl *VCL
129 if err := decodeJSON(&vcl, resp.Body); err != nil {
130 return nil, err
131 }
132 return vcl, nil
133 }
134
135 // CreateVCLInput is used as input to the CreateVCL function.
136 type CreateVCLInput struct {
137 // Service is the ID of the service. Version is the specific configuration
138 // version. Both fields are required.
139 Service string
140 Version int
141
142 Name string `form:"name,omitempty"`
143 Content string `form:"content,omitempty"`
144 Main bool `form:"main,omitempty"`
145 }
146
147 // CreateVCL creates a new Fastly VCL.
148 func (c *Client) CreateVCL(i *CreateVCLInput) (*VCL, error) {
149 if i.Service == "" {
150 return nil, ErrMissingService
151 }
152
153 if i.Version == 0 {
154 return nil, ErrMissingVersion
155 }
156
157 path := fmt.Sprintf("/service/%s/version/%d/vcl", i.Service, i.Version)
158 resp, err := c.PostForm(path, i, nil)
159 if err != nil {
160 return nil, err
161 }
162
163 var vcl *VCL
164 if err := decodeJSON(&vcl, resp.Body); err != nil {
165 return nil, err
166 }
167 return vcl, nil
168 }
169
170 // UpdateVCLInput is used as input to the UpdateVCL function.
171 type UpdateVCLInput struct {
172 // Service is the ID of the service. Version is the specific configuration
173 // version. Both fields are required.
174 Service string
175 Version int
176
177 // Name is the name of the VCL to update (required).
178 Name string
179
180 NewName string `form:"name,omitempty"`
181 Content string `form:"content,omitempty"`
182 }
183
184 // UpdateVCL creates a new Fastly VCL.
185 func (c *Client) UpdateVCL(i *UpdateVCLInput) (*VCL, error) {
186 if i.Service == "" {
187 return nil, ErrMissingService
188 }
189
190 if i.Version == 0 {
191 return nil, ErrMissingVersion
192 }
193
194 if i.Name == "" {
195 return nil, ErrMissingName
196 }
197
198 path := fmt.Sprintf("/service/%s/version/%d/vcl/%s", i.Service, i.Version, url.PathEscape(i.Name))
199 resp, err := c.PutForm(path, i, nil)
200 if err != nil {
201 return nil, err
202 }
203
204 var vcl *VCL
205 if err := decodeJSON(&vcl, resp.Body); err != nil {
206 return nil, err
207 }
208 return vcl, nil
209 }
210
211 // ActivateVCLInput is used as input to the ActivateVCL function.
212 type ActivateVCLInput struct {
213 // Service is the ID of the service. Version is the specific configuration
214 // version. Both fields are required.
215 Service string
216 Version int
217
218 // Name is the name of the VCL to mark as main (required).
219 Name string
220 }
221
222 // ActivateVCL creates a new Fastly VCL.
223 func (c *Client) ActivateVCL(i *ActivateVCLInput) (*VCL, error) {
224 if i.Service == "" {
225 return nil, ErrMissingService
226 }
227
228 if i.Version == 0 {
229 return nil, ErrMissingVersion
230 }
231
232 if i.Name == "" {
233 return nil, ErrMissingName
234 }
235
236 path := fmt.Sprintf("/service/%s/version/%d/vcl/%s/main", i.Service, i.Version, url.PathEscape(i.Name))
237 resp, err := c.Put(path, nil)
238 if err != nil {
239 return nil, err
240 }
241
242 var vcl *VCL
243 if err := decodeJSON(&vcl, resp.Body); err != nil {
244 return nil, err
245 }
246 return vcl, nil
247 }
248
249 // DeleteVCLInput is the input parameter to DeleteVCL.
250 type DeleteVCLInput struct {
251 // Service is the ID of the service. Version is the specific configuration
252 // version. Both fields are required.
253 Service string
254 Version int
255
256 // Name is the name of the VCL to delete (required).
257 Name string
258 }
259
260 // DeleteVCL deletes the given VCL version.
261 func (c *Client) DeleteVCL(i *DeleteVCLInput) error {
262 if i.Service == "" {
263 return ErrMissingService
264 }
265
266 if i.Version == 0 {
267 return ErrMissingVersion
268 }
269
270 if i.Name == "" {
271 return ErrMissingName
272 }
273
274 path := fmt.Sprintf("/service/%s/version/%d/vcl/%s", i.Service, i.Version, url.PathEscape(i.Name))
275 resp, err := c.Delete(path, nil)
276 if err != nil {
277 return err
278 }
279
280 var r *statusResp
281 if err := decodeJSON(&r, resp.Body); err != nil {
282 return err
283 }
284 if !r.Ok() {
285 return fmt.Errorf("Not Ok")
286 }
287 return nil
288 }