cdn.go raw
1 package govultr
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 )
8
9 const cdnPath string = "/v2/cdns"
10
11 var (
12 cdnPullPath string = fmt.Sprintf("%s/pull-zones", cdnPath)
13 cdnPushPath string = fmt.Sprintf("%s/push-zones", cdnPath)
14 )
15
16 // CDNService is the interface to interact with the CDN endpoints on the Vultr API
17 // Link : https://www.vultr.com/api/#tag/CDNs
18 type CDNService interface {
19 ListPullZones(ctx context.Context) ([]CDNZone, *Meta, *http.Response, error)
20 GetPullZone(ctx context.Context, zoneID string) (*CDNZone, *http.Response, error)
21 CreatePullZone(ctx context.Context, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error)
22 UpdatePullZone(ctx context.Context, zoneID string, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error)
23 DeletePullZone(ctx context.Context, zoneID string) error
24 PurgePullZone(ctx context.Context, zoneID string) error
25
26 ListPushZones(ctx context.Context) ([]CDNZone, *Meta, *http.Response, error)
27 GetPushZone(ctx context.Context, zoneID string) (*CDNZone, *http.Response, error)
28 CreatePushZone(ctx context.Context, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error)
29 UpdatePushZone(ctx context.Context, zoneID string, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error)
30 DeletePushZone(ctx context.Context, zoneID string) error
31
32 ListPushZoneFiles(ctx context.Context, zoneID string) (*CDNZoneFileData, *http.Response, error)
33 GetPushZoneFile(ctx context.Context, zoneID, fileName string) (*CDNZoneFile, *http.Response, error)
34 DeletePushZoneFile(ctx context.Context, zoneID, fileName string) error
35 CreatePushZoneFileEndpoint(ctx context.Context, zoneID string, endpointReq *CDNZoneEndpointReq) (*CDNZoneEndpoint, *http.Response, error) //nolint:lll
36 }
37
38 // CDNZone represents the CDN push/pull zone data
39 type CDNZone struct {
40 ID string `json:"id"`
41 DateCreated string `json:"date_created"`
42 Status string `json:"status"`
43 Label string `json:"label"`
44 OriginScheme string `json:"origin_scheme"`
45 OriginDomain string `json:"origin_domain"`
46 VanityDomain string `json:"vanity_domain"`
47 SSLCert string `json:"ssl_cert"`
48 SSLCertKey string `json:"ssl_cert_key"`
49 CDNURL string `json:"cdn_url"`
50 CacheSize int `json:"cache_size"`
51 Requests int `json:"requests"`
52 BytesIn int `json:"in_bytes"`
53 BytesOut int `json:"out_bytes"`
54 PacketsPerSec int `json:"packets_per_sec"`
55 DatePurged string `json:"last_purge"`
56 CORS bool `json:"cors"`
57 GZIP bool `json:"gzip"`
58 BlockAI bool `json:"block_ai"`
59 BlockBadBots bool `json:"block_bad_bots"`
60 Regions []string `json:"regions"`
61 }
62
63 // CDNZoneReq is the data used to create a push/pull zone
64 type CDNZoneReq struct {
65 Label string `json:"label"`
66 OriginScheme string `json:"origin_scheme,omitempty"`
67 OriginDomain string `json:"origin_domain,omitempty"`
68 VanityDomain string `json:"vanity_domain,omitempty"`
69 SSLCert string `json:"ssl_cert,omitempty"`
70 SSLCertKey string `json:"ssl_cert_key,omitempty"`
71 CORS *bool `json:"cors,omitempty"`
72 GZIP *bool `json:"gzip,omitempty"`
73 BlockAI *bool `json:"block_ai,omitempty"`
74 BlockBadBots *bool `json:"block_bad_bots,omitempty"`
75 Regions []string `json:"regions,omitempty"`
76 }
77
78 // CDNZoneFileData represents the push zone files and metadata
79 type CDNZoneFileData struct {
80 Files []CDNZoneFile `json:"files"`
81 Count int `json:"count"`
82 Size int `json:"total_size"`
83 }
84
85 // CDNZoneFile is the data for a push zone file
86 type CDNZoneFile struct {
87 Name string `json:"name"`
88 Size int `json:"size"`
89 DateModified string `json:"last_modified"`
90 }
91
92 // CDNZoneEndpointReq is the data used to create a push zone upload endpoint
93 type CDNZoneEndpointReq struct {
94 Name string `json:"name"`
95 Size int `json:"size"`
96 }
97
98 // CDNZoneEndpoint is the data for a push zone file endpoint
99 type CDNZoneEndpoint struct {
100 URL string `json:"url"`
101 Inputs CDNZoneEndpointInputs `json:"inputs"`
102 }
103
104 // CDNZoneEndpointInputs is the data for push zone file endpoint inputs
105 type CDNZoneEndpointInputs struct {
106 ACL string `json:"acl"`
107 Key string `json:"key"`
108 Policy string `json:"policy"`
109 Credential string `json:"x-amz-credential"`
110 Algorithm string `json:"x-amz-algorithm"`
111 Signature string `json:"x-amz-signature"`
112 }
113
114 type cdnPullZonesBase struct {
115 PullZones []CDNZone `json:"pull_zones"`
116 Meta *Meta `json:"meta"`
117 }
118
119 type cdnPullZoneBase struct {
120 PullZone CDNZone `json:"pull_zone"`
121 }
122
123 type cdnPushZonesBase struct {
124 PushZones []CDNZone `json:"push_zones"`
125 Meta *Meta `json:"meta"`
126 }
127
128 type cdnPushZoneBase struct {
129 PushZone CDNZone `json:"push_zone"`
130 }
131
132 type cdnPushZoneEndpointBase struct {
133 Endpoint CDNZoneEndpoint `json:"upload_endpoint"`
134 }
135
136 // CDNServiceHandler handles interaction with the CDN methods for the Vultr API
137 type CDNServiceHandler struct {
138 client *Client
139 }
140
141 // ListPullZones will retrieve a CDN pull zone
142 func (c *CDNServiceHandler) ListPullZones(ctx context.Context) ([]CDNZone, *Meta, *http.Response, error) {
143 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPullPath, nil)
144 if err != nil {
145 return nil, nil, nil, err
146 }
147
148 zones := new(cdnPullZonesBase)
149 resp, err := c.client.DoWithContext(ctx, req, &zones)
150 if err != nil {
151 return nil, nil, resp, err
152 }
153
154 return zones.PullZones, zones.Meta, resp, nil
155 }
156
157 // GetPullZone will retrieve data for a CDN pull zone
158 func (c *CDNServiceHandler) GetPullZone(ctx context.Context, zoneID string) (*CDNZone, *http.Response, error) {
159 cdnPullGetPath := fmt.Sprintf("%s/%s", cdnPullPath, zoneID)
160 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPullGetPath, nil)
161 if err != nil {
162 return nil, nil, err
163 }
164
165 zone := new(cdnPullZoneBase)
166 resp, err := c.client.DoWithContext(ctx, req, &zone)
167 if err != nil {
168 return nil, resp, err
169 }
170
171 return &zone.PullZone, resp, nil
172 }
173
174 // CreatePullZone will create a new CDN pull zone
175 func (c *CDNServiceHandler) CreatePullZone(ctx context.Context, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error) {
176 req, err := c.client.NewRequest(ctx, http.MethodPost, cdnPullPath, zoneReq)
177 if err != nil {
178 return nil, nil, err
179 }
180
181 zone := new(cdnPullZoneBase)
182 resp, err := c.client.DoWithContext(ctx, req, &zone)
183 if err != nil {
184 return nil, resp, err
185 }
186
187 return &zone.PullZone, resp, nil
188 }
189
190 // UpdatePullZone will update an existing CDN pull zone
191 func (c *CDNServiceHandler) UpdatePullZone(ctx context.Context, zoneID string, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error) { //nolint:dupl,lll
192 cndPullUpdatePath := fmt.Sprintf("%s/%s", cdnPullPath, zoneID)
193 req, err := c.client.NewRequest(ctx, http.MethodPut, cndPullUpdatePath, zoneReq)
194 if err != nil {
195 return nil, nil, err
196 }
197
198 zone := new(cdnPullZoneBase)
199 resp, err := c.client.DoWithContext(ctx, req, &zone)
200 if err != nil {
201 return nil, resp, err
202 }
203
204 return &zone.PullZone, resp, nil
205 }
206
207 // DeletePullZone will delete a CDN pull zone
208 func (c *CDNServiceHandler) DeletePullZone(ctx context.Context, zoneID string) error {
209 cdnPullDeletePath := fmt.Sprintf("%s/%s", cdnPullPath, zoneID)
210 req, err := c.client.NewRequest(ctx, http.MethodDelete, cdnPullDeletePath, nil)
211 if err != nil {
212 return err
213 }
214
215 _, err = c.client.DoWithContext(ctx, req, nil)
216 if err != nil {
217 return err
218 }
219
220 return nil
221 }
222
223 // PurgePullZone will clear the cache on a CDN pull zone
224 func (c *CDNServiceHandler) PurgePullZone(ctx context.Context, zoneID string) error {
225 cdnPullPurgePath := fmt.Sprintf("%s/%s", cdnPullPath, zoneID)
226 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPullPurgePath, nil)
227 if err != nil {
228 return err
229 }
230
231 _, err = c.client.DoWithContext(ctx, req, nil)
232 if err != nil {
233 return err
234 }
235
236 return nil
237 }
238
239 // ListPushZones will retrieve a CDN push zone
240 func (c *CDNServiceHandler) ListPushZones(ctx context.Context) ([]CDNZone, *Meta, *http.Response, error) {
241 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPushPath, nil)
242 if err != nil {
243 return nil, nil, nil, err
244 }
245
246 zones := new(cdnPushZonesBase)
247 resp, err := c.client.DoWithContext(ctx, req, &zones)
248 if err != nil {
249 return nil, nil, resp, err
250 }
251
252 return zones.PushZones, zones.Meta, resp, nil
253 }
254
255 // GetPushZone will retrieve data for a CDN push zone
256 func (c *CDNServiceHandler) GetPushZone(ctx context.Context, zoneID string) (*CDNZone, *http.Response, error) {
257 cdnPushGetPath := fmt.Sprintf("%s/%s", cdnPushPath, zoneID)
258 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPushGetPath, nil)
259 if err != nil {
260 return nil, nil, err
261 }
262
263 zone := new(cdnPushZoneBase)
264 resp, err := c.client.DoWithContext(ctx, req, &zone)
265 if err != nil {
266 return nil, resp, err
267 }
268
269 return &zone.PushZone, resp, nil
270 }
271
272 // CreatePushZone will create a new CDN push zone
273 func (c *CDNServiceHandler) CreatePushZone(ctx context.Context, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error) {
274 req, err := c.client.NewRequest(ctx, http.MethodPost, cdnPushPath, zoneReq)
275 if err != nil {
276 return nil, nil, err
277 }
278
279 zone := new(cdnPushZoneBase)
280 resp, err := c.client.DoWithContext(ctx, req, &zone)
281 if err != nil {
282 return nil, resp, err
283 }
284
285 return &zone.PushZone, resp, nil
286 }
287
288 // UpdatePushZone will update an existing CDN push zone
289 func (c *CDNServiceHandler) UpdatePushZone(ctx context.Context, zoneID string, zoneReq *CDNZoneReq) (*CDNZone, *http.Response, error) { //nolint:dupl,lll
290 cndPushUpdatePath := fmt.Sprintf("%s/%s", cdnPushPath, zoneID)
291 req, err := c.client.NewRequest(ctx, http.MethodPut, cndPushUpdatePath, zoneReq)
292 if err != nil {
293 return nil, nil, err
294 }
295
296 zone := new(cdnPushZoneBase)
297 resp, err := c.client.DoWithContext(ctx, req, &zone)
298 if err != nil {
299 return nil, resp, err
300 }
301
302 return &zone.PushZone, resp, nil
303 }
304
305 // DeletePushZone will delete a CDN push zone
306 func (c *CDNServiceHandler) DeletePushZone(ctx context.Context, zoneID string) error {
307 cdnPushDeletePath := fmt.Sprintf("%s/%s", cdnPushPath, zoneID)
308 req, err := c.client.NewRequest(ctx, http.MethodDelete, cdnPushDeletePath, nil)
309 if err != nil {
310 return err
311 }
312
313 _, err = c.client.DoWithContext(ctx, req, nil)
314 if err != nil {
315 return err
316 }
317
318 return nil
319 }
320
321 // CreatePushZoneFileEndpoint will create a new CDN push zone file upload
322 // endpoint
323 func (c *CDNServiceHandler) CreatePushZoneFileEndpoint(ctx context.Context, zoneID string, zoneEndpointReq *CDNZoneEndpointReq) (*CDNZoneEndpoint, *http.Response, error) { //nolint:lll,dupl
324 cdnPushEnpointPath := fmt.Sprintf("%s/%s/files", cdnPushPath, zoneID)
325 req, err := c.client.NewRequest(ctx, http.MethodPost, cdnPushEnpointPath, zoneEndpointReq)
326 if err != nil {
327 return nil, nil, err
328 }
329
330 endpoint := new(cdnPushZoneEndpointBase)
331 resp, err := c.client.DoWithContext(ctx, req, &endpoint)
332 if err != nil {
333 return nil, resp, err
334 }
335
336 return &endpoint.Endpoint, resp, nil
337 }
338
339 // ListPushZoneFiles will retrieve all CDN push zone file data that have been
340 // uploaded
341 func (c *CDNServiceHandler) ListPushZoneFiles(ctx context.Context, zoneID string) (*CDNZoneFileData, *http.Response, error) {
342 cdnPushFilesPath := fmt.Sprintf("%s/%s/files", cdnPushPath, zoneID)
343 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnPushFilesPath, nil)
344 if err != nil {
345 return nil, nil, err
346 }
347
348 fd := new(CDNZoneFileData)
349 resp, err := c.client.DoWithContext(ctx, req, &fd)
350 if err != nil {
351 return nil, resp, err
352 }
353
354 return fd, resp, nil
355 }
356
357 // GetPushZoneFile will retrieve data on a file in a CDN push zone
358 func (c *CDNServiceHandler) GetPushZoneFile(ctx context.Context, zoneID, fileName string) (*CDNZoneFile, *http.Response, error) {
359 cdnFileGetPath := fmt.Sprintf("%s/%s/files/%s", cdnPushPath, zoneID, fileName)
360 req, err := c.client.NewRequest(ctx, http.MethodGet, cdnFileGetPath, nil)
361 if err != nil {
362 return nil, nil, err
363 }
364
365 file := new(CDNZoneFile)
366 resp, err := c.client.DoWithContext(ctx, req, &file)
367 if err != nil {
368 return nil, resp, err
369 }
370
371 return file, resp, nil
372 }
373
374 // DeletePushZoneFile delete a file in a CDN push zone
375 func (c *CDNServiceHandler) DeletePushZoneFile(ctx context.Context, zoneID, fileName string) error {
376 cdnFileDelPath := fmt.Sprintf("%s/%s/files/%s", cdnPushPath, zoneID, fileName)
377 if _, err := c.client.NewRequest(ctx, http.MethodDelete, cdnFileDelPath, nil); err != nil {
378 return err
379 }
380
381 return nil
382 }
383