Skip to content

Commit 04a8dba

Browse files
authored
Merge pull request #175 from sunny-b/master
Add support for spaces cdn
2 parents 498a141 + 96f3a26 commit 04a8dba

File tree

5 files changed

+493
-1
lines changed

5 files changed

+493
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## [v1.4.0] - 2018-08-22
4+
5+
- #170 Add support for Spaces CDN - @sunny-b
6+
37
## [v1.3.0] - 2018-05-24
48

59
- #170 Add support for volume formatting - @adamwg

cdn.go

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package godo
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
)
9+
10+
const cdnBasePath = "v2/cdn/endpoints"
11+
12+
// CDNService is an interface for managing Spaces CDN with the DigitalOcean API.
13+
type CDNService interface {
14+
List(context.Context, *ListOptions) ([]CDN, *Response, error)
15+
Get(context.Context, string) (*CDN, *Response, error)
16+
Create(context.Context, *CDNCreateRequest) (*CDN, *Response, error)
17+
UpdateTTL(context.Context, string, *CDNUpdateRequest) (*CDN, *Response, error)
18+
FlushCache(context.Context, string, *CDNFlushCacheRequest) (*Response, error)
19+
Delete(context.Context, string) (*Response, error)
20+
}
21+
22+
// CDNServiceOp handles communication with the CDN related methods of the
23+
// DigitalOcean API.
24+
type CDNServiceOp struct {
25+
client *Client
26+
}
27+
28+
var _ CDNService = &CDNServiceOp{}
29+
30+
// CDN represents a DigitalOcean CDN
31+
type CDN struct {
32+
ID string `json:"id"`
33+
Origin string `json:"origin"`
34+
Endpoint string `json:"endpoint"`
35+
CreatedAt time.Time `json:"created_at"`
36+
TTL uint32 `json:"ttl"`
37+
}
38+
39+
// CDNRoot represents a response from the DigitalOcean API
40+
type cdnRoot struct {
41+
Endpoint *CDN `json:"endpoint"`
42+
}
43+
44+
type cdnsRoot struct {
45+
Endpoints []CDN `json:"endpoints"`
46+
Links *Links `json:"links"`
47+
}
48+
49+
// CDNCreateRequest represents a request to create a CDN.
50+
type CDNCreateRequest struct {
51+
Origin string `json:"origin"`
52+
TTL uint32 `json:"ttl"`
53+
}
54+
55+
// CDNUpdateRequest represents a request to update the ttl of a CDN.
56+
type CDNUpdateRequest struct {
57+
TTL uint32 `json:"ttl"`
58+
}
59+
60+
// CDNFlushCacheRequest represents a request to flush cache of a CDN.
61+
type CDNFlushCacheRequest struct {
62+
Files []string `json:"files"`
63+
}
64+
65+
// List all CDN endpoints
66+
func (c CDNServiceOp) List(ctx context.Context, opt *ListOptions) ([]CDN, *Response, error) {
67+
path, err := addOptions(cdnBasePath, opt)
68+
if err != nil {
69+
return nil, nil, err
70+
}
71+
72+
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
73+
if err != nil {
74+
return nil, nil, err
75+
}
76+
77+
root := new(cdnsRoot)
78+
resp, err := c.client.Do(ctx, req, root)
79+
if err != nil {
80+
return nil, resp, err
81+
}
82+
if l := root.Links; l != nil {
83+
resp.Links = l
84+
}
85+
86+
return root.Endpoints, resp, err
87+
}
88+
89+
// Get individual CDN. It requires a non-empty cdn id.
90+
func (c CDNServiceOp) Get(ctx context.Context, id string) (*CDN, *Response, error) {
91+
if len(id) == 0 {
92+
return nil, nil, NewArgError("id", "cannot be an empty string")
93+
}
94+
95+
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
96+
97+
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
98+
if err != nil {
99+
return nil, nil, err
100+
}
101+
102+
root := new(cdnRoot)
103+
resp, err := c.client.Do(ctx, req, root)
104+
if err != nil {
105+
return nil, resp, err
106+
}
107+
108+
return root.Endpoint, resp, err
109+
}
110+
111+
// Create a new CDN
112+
func (c CDNServiceOp) Create(ctx context.Context, createRequest *CDNCreateRequest) (*CDN, *Response, error) {
113+
if createRequest == nil {
114+
return nil, nil, NewArgError("createRequest", "cannot be nil")
115+
}
116+
117+
req, err := c.client.NewRequest(ctx, http.MethodPost, cdnBasePath, createRequest)
118+
if err != nil {
119+
return nil, nil, err
120+
}
121+
122+
root := new(cdnRoot)
123+
resp, err := c.client.Do(ctx, req, root)
124+
if err != nil {
125+
return nil, resp, err
126+
}
127+
128+
return root.Endpoint, resp, err
129+
}
130+
131+
// UpdateTTL updates the ttl of individual CDN
132+
func (c CDNServiceOp) UpdateTTL(ctx context.Context, id string, updateRequest *CDNUpdateRequest) (*CDN, *Response, error) {
133+
if updateRequest == nil {
134+
return nil, nil, NewArgError("updateRequest", "cannot be nil")
135+
}
136+
137+
if len(id) == 0 {
138+
return nil, nil, NewArgError("id", "cannot be an empty string")
139+
}
140+
141+
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
142+
143+
req, err := c.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
144+
if err != nil {
145+
return nil, nil, err
146+
}
147+
148+
root := new(cdnRoot)
149+
resp, err := c.client.Do(ctx, req, root)
150+
if err != nil {
151+
return nil, resp, err
152+
}
153+
154+
return root.Endpoint, resp, err
155+
}
156+
157+
// FlushCache flushes the cache of an individual CDN. Requires a non-empty slice of file paths and/or wildcards
158+
func (c CDNServiceOp) FlushCache(ctx context.Context, id string, flushCacheRequest *CDNFlushCacheRequest) (*Response, error) {
159+
if flushCacheRequest == nil {
160+
return nil, NewArgError("flushCacheRequest", "cannot be nil")
161+
}
162+
163+
if len(id) == 0 {
164+
return nil, NewArgError("id", "cannot be an empty string")
165+
}
166+
167+
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
168+
169+
req, err := c.client.NewRequest(ctx, http.MethodDelete, path, flushCacheRequest)
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
resp, err := c.client.Do(ctx, req, nil)
175+
176+
return resp, err
177+
}
178+
179+
// Delete an individual CDN
180+
func (c CDNServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
181+
if len(id) == 0 {
182+
return nil, NewArgError("id", "cannot be an empty string")
183+
}
184+
185+
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
186+
187+
req, err := c.client.NewRequest(ctx, http.MethodDelete, path, nil)
188+
if err != nil {
189+
return nil, err
190+
}
191+
192+
resp, err := c.client.Do(ctx, req, nil)
193+
194+
return resp, err
195+
}

0 commit comments

Comments
 (0)