-
Notifications
You must be signed in to change notification settings - Fork 13
/
cdn.go
228 lines (201 loc) · 6.04 KB
/
cdn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
const (
domainPath = "/domains"
usersPath = "/users"
clientsPath = "/clients"
)
type cdnService struct {
client *Client
}
var _ CDNService = (*cdnService)(nil)
type CDNService interface {
List(ctx context.Context, opts *ListOptions) (*DomainsResp, error)
Get(ctx context.Context, domainID string) (*Domain, error)
Create(ctx context.Context, cdrq *CreateDomainPayload) (*CreateDomainResponse, error)
Update(ctx context.Context, domainID string, udrq *UpdateDomainPayload) (*UpdateDomainResp, error)
Delete(ctx context.Context, domainID string) error
DeleteCache(ctx context.Context, domainID string, files *Files) error
}
// Origin represents a CDN origin information
type Origin struct {
Name string `json:"name"`
UpstreamHost string `json:"upstream_host"`
UpstreamAddrs string `json:"upstream_addrs"`
UpstreamProto string `json:"upstream_proto"`
}
// Domain represents CDN domain information
type Domain struct {
Domain string `json:"domain"`
Slug string `json:"slug"`
DomainCDN string `json:"domain_cdn"`
DomainID string `json:"domain_id"`
}
// DomainsResp represents a list of CDN domains and pagination information
type DomainsResp struct {
Domains []Domain `json:"results"`
Next string `json:"next"`
Prev string `json:"prev"`
Pages int `json:"pages"`
Total int `json:"total"`
}
// OriginAddr represents a CDN origin address information
type OriginAddr struct {
Type string `json:"type"`
Host string `json:"host"`
}
// ExtendedDomain represents a CDN domain information with additional information
type ExtendedDomain struct {
Domain
Last24hUsage int `json:"last_24h_usage"`
UpstreamHost string `json:"upstream_host"`
Slug string `json:"slug"`
SecretKey string `json:"secret_key"`
DomainCDN string `json:"domain_cdn"`
OriginAddrs []OriginAddr `json:"origin_addrs"`
HostID string `json:"host_id"`
}
// CreateDomainReq represents a request body for creating CDN domain
type CreateDomainPayload struct {
Domain string `json:"domain"`
Origin *Origin `json:"origin"`
}
// CreateDomainResp represents a response body when creating CDN domain
type CreateDomainResponse struct {
Message string `json:"message"`
Domain Domain `json:"domain"`
}
// UpdateDomainReq represents a request body for updating CDN domain
type UpdateDomainReq struct {
UpstreamAddrs string `json:"upstream_addrs"`
UpstreamProto string `json:"upstream_proto"`
PageSpeed int `json:"pagespeed"`
SecureLink int `json:"secure_link"`
}
type UpdateDomainPayload struct {
Origin *Origin `json:"origin"`
}
// UpdateDomainResp represents a response body when updating CDN domain
type UpdateDomainResp struct {
Message string `json:"message"`
Domain ExtendedDomain `json:"domain"`
}
// CheckConnResp represents a response body when checking CDN connection
type CheckConnResp struct {
Status string `json:"status"`
}
type Files struct {
Files []string `json:"files"`
}
func (c *cdnService) resourcePath() string {
return strings.Join([]string{clientsPath, domainPath}, "")
}
func (c *cdnService) itemPath(id string) string {
return strings.Join([]string{c.resourcePath(), id}, "/")
}
func (c *cdnService) List(ctx context.Context, opts *ListOptions) (*DomainsResp, error) {
u, _ := url.Parse(strings.Join([]string{usersPath, domainPath}, ""))
query := url.Values{}
if opts.Page != 0 {
query.Add("page", strconv.Itoa(opts.Page))
}
if opts.Page != 0 {
query.Add("limit", strconv.Itoa(opts.Limit))
}
u.RawQuery = query.Encode()
req, err := c.client.NewRequest(ctx, http.MethodGet, cdnName, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data *DomainsResp
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (c *cdnService) Get(ctx context.Context, domainID string) (*Domain, error) {
var data *CreateDomainResponse
req, err := c.client.NewRequest(ctx, http.MethodGet, cdnName, c.itemPath(domainID), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data.Domain, nil
}
func (c *cdnService) Create(ctx context.Context, cdrq *CreateDomainPayload) (*CreateDomainResponse, error) {
var data *CreateDomainResponse
req, err := c.client.NewRequest(ctx, http.MethodPost, cdnName, c.resourcePath(), &cdrq)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (c *cdnService) Update(ctx context.Context, domainID string, udrq *UpdateDomainPayload) (*UpdateDomainResp, error) {
req, err := c.client.NewRequest(ctx, http.MethodPut, cdnName, c.itemPath(domainID), udrq)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
var data *UpdateDomainResp
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (c *cdnService) Delete(ctx context.Context, domainID string) error {
req, err := c.client.NewRequest(ctx, http.MethodDelete, cdnName, c.itemPath(domainID), nil)
if err != nil {
return err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
func (c *cdnService) DeleteCache(ctx context.Context, domainID string, files *Files) error {
req, err := c.client.NewRequest(ctx, http.MethodDelete, cdnName, c.itemPath(domainID), files)
if err != nil {
return err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}