-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathedgehostname.go
355 lines (304 loc) · 12.6 KB
/
edgehostname.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package hapi
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// DeleteEdgeHostnameRequest is used to delete edge hostname
DeleteEdgeHostnameRequest struct {
DNSZone string
RecordName string
StatusUpdateEmail []string
Comments string
}
// DeleteEdgeHostnameResponse is a response from deleting edge hostname
DeleteEdgeHostnameResponse struct {
Action string `json:"action"`
ChangeID int `json:"changeId"`
Comments string `json:"comments"`
Status string `json:"status"`
StatusMessage string `json:"statusMessage"`
StatusUpdateDate string `json:"statusUpdateDate"`
StatusUpdateEmail string `json:"statusUpdateEmail"`
SubmitDate string `json:"submitDate"`
Submitter string `json:"submitter"`
SubmitterEmail string `json:"submitterEmail"`
EdgeHostnames []EdgeHostname `json:"edgeHostnames"`
}
// UpdateEdgeHostnameRequest is a request used to update edge hostname
UpdateEdgeHostnameRequest struct {
DNSZone string
RecordName string
StatusUpdateEmail []string
Comments string
Body []UpdateEdgeHostnameRequestBody
}
// UpdateEdgeHostnameRequestBody is a request's body used to update edge hostname
UpdateEdgeHostnameRequestBody struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
// UpdateEdgeHostnameResponse is a response from deleting edge hostname
UpdateEdgeHostnameResponse struct {
Action string `json:"action,omitempty"`
ChangeID int `json:"changeId,omitempty"`
Comments string `json:"comments,omitempty"`
Status string `json:"status,omitempty"`
StatusMessage string `json:"statusMessage,omitempty"`
StatusUpdateDate string `json:"statusUpdateDate,omitempty"`
StatusUpdateEmail string `json:"statusUpdateEmail,omitempty"`
SubmitDate string `json:"submitDate,omitempty"`
Submitter string `json:"submitter,omitempty"`
SubmitterEmail string `json:"submitterEmail,omitempty"`
EdgeHostnames []EdgeHostname `json:"edgeHostnames,omitempty"`
}
// EdgeHostname represents edge hostname part of DeleteEdgeHostnameResponse and UpdateEdgeHostnameResponse
EdgeHostname struct {
EdgeHostnameID int `json:"edgeHostnameId,omitempty"`
RecordName string `json:"recordName"`
DNSZone string `json:"dnsZone"`
SecurityType string `json:"securityType"`
UseDefaultTTL bool `json:"useDefaultTtl"`
UseDefaultMap bool `json:"useDefaultMap"`
TTL int `json:"ttl"`
Map string `json:"map,omitempty"`
SlotNumber int `json:"slotNumber,omitempty"`
IPVersionBehavior string `json:"ipVersionBehavior,omitempty"`
Comments string `json:"comments,omitempty"`
ChinaCDN ChinaCDN `json:"chinaCdn,omitempty"`
CustomTarget string `json:"customTarget,omitempty"`
IsEdgeIPBindingEnabled bool `json:"isEdgeIPBindingEnabled,omitempty"`
MapAlias string `json:"mapAlias,omitempty"`
ProductId string `json:"productId,omitempty"`
SerialNumber int `json:"serialNumber,omitempty"`
UseCases []UseCase `json:"useCases,omitempty"`
}
// ChinaCDN represents China CDN settings of EdgeHostname
ChinaCDN struct {
IsChinaCDN bool `json:"isChinaCdn,omitempty"`
CustomChinaCDNMap string `json:"customChinaCdnMap,omitempty"`
}
// UseCase represents useCase attribute in EdgeHostname
UseCase struct {
Type string `json:"type,omitempty"`
Option string `json:"option"`
UseCase string `json:"useCase"`
}
// GetEdgeHostnameResponse represents edge hostname
GetEdgeHostnameResponse struct {
EdgeHostnameID int `json:"edgeHostnameId"`
RecordName string `json:"recordName"`
DNSZone string `json:"dnsZone"`
SecurityType string `json:"securityType"`
UseDefaultTTL bool `json:"useDefaultTtl"`
UseDefaultMap bool `json:"useDefaultMap"`
IPVersionBehavior string `json:"ipVersionBehavior"`
ProductID string `json:"productId"`
TTL int `json:"ttl"`
Map string `json:"map,omitempty"`
SlotNumber int `json:"slotNumber,omitempty"`
Comments string `json:"comments"`
SerialNumber int `json:"serialNumber,omitempty"`
CustomTarget string `json:"customTarget,omitempty"`
ChinaCdn ChinaCDN `json:"chinaCdn,omitempty"`
IsEdgeIPBindingEnabled bool `json:"isEdgeIPBindingEnabled,omitempty"`
MapAlias string `json:"mapAlias"`
UseCases []UseCase `json:"useCases"`
}
// GetCertificateRequest is used to get certificate associated with edge hostname
GetCertificateRequest struct {
DNSZone string
RecordName string
}
// GetCertificateResponse represents edge hostname certificate
GetCertificateResponse struct {
AvailableDomains []string `json:"availableDomains"`
CertificateID string `json:"certificateId"`
CertificateType string `json:"certificateType"`
CommonName string `json:"commonName"`
ExpirationDate time.Time `json:"expirationDate"`
SerialNumber string `json:"serialNumber"`
SlotNumber int `json:"slotNumber"`
Status string `json:"status"`
ValidationType string `json:"validationType"`
}
)
// Validate validates DeleteEdgeHostnameRequest
func (r DeleteEdgeHostnameRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"DNSZone": validation.Validate(r.DNSZone, validation.Required),
"RecordName": validation.Validate(r.RecordName, validation.Required),
})
}
// Validate validates DeleteEdgeHostnameRequest
func (r UpdateEdgeHostnameRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"DNSZone": validation.Validate(r.DNSZone, validation.Required),
"RecordName": validation.Validate(r.RecordName, validation.Required),
"Body": validation.Validate(r.Body),
})
}
// Validate validates UpdateEdgeHostnameRequestBody
func (b UpdateEdgeHostnameRequestBody) Validate() error {
return validation.Errors{
"Path": validation.Validate(b.Path, validation.Required, validation.In("/ttl", "/ipVersionBehavior").Error(fmt.Sprintf("value '%s' is invalid. Must be one of: '/ttl' or '/ipVersionBehavior'", b.Path))),
"Op": validation.Validate(b.Op, validation.Required, validation.In("replace").Error(fmt.Sprintf("value '%s' is invalid. Must use 'replace'", b.Op))),
"Value": validation.Validate(b.Value, validation.Required),
}.Filter()
}
// Validate validates GetCertificateRequest
func (r GetCertificateRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"DNSZone": validation.Validate(r.DNSZone, validation.Required),
"RecordName": validation.Validate(r.RecordName, validation.Required),
})
}
var (
// ErrDeleteEdgeHostname represents error when deleting edge hostname fails
ErrDeleteEdgeHostname = errors.New("delete edge hostname")
// ErrGetEdgeHostname represents error when getting edge hostname fails
ErrGetEdgeHostname = errors.New("get edge hostname")
// ErrUpdateEdgeHostname represents error when updating edge hostname fails
ErrUpdateEdgeHostname = errors.New("update edge hostname")
// ErrGetCertificate represents error when getting edge hostname certificate fails
ErrGetCertificate = errors.New("get edge hostname certificate")
// ErrNotFound represents error when getting edge hostname fails
ErrNotFound = errors.New("not found")
)
func (h *hapi) DeleteEdgeHostname(ctx context.Context, params DeleteEdgeHostnameRequest) (*DeleteEdgeHostnameResponse, error) {
logger := h.Log(ctx)
logger.Debug("DeleteEdgeHostname")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrDeleteEdgeHostname, ErrStructValidation, err)
}
uri := fmt.Sprintf(
"/hapi/v1/dns-zones/%s/edge-hostnames/%s",
params.DNSZone,
params.RecordName,
)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrDeleteEdgeHostname, err)
}
q := req.URL.Query()
if len(params.StatusUpdateEmail) > 0 {
emails := strings.Join(params.StatusUpdateEmail, ",")
q.Add("statusUpdateEmail", emails)
}
if params.Comments != "" {
q.Add("comments", params.Comments)
}
req.URL.RawQuery = q.Encode()
var rval DeleteEdgeHostnameResponse
resp, err := h.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrDeleteEdgeHostname, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("%s: %w", ErrDeleteEdgeHostname, h.Error(resp))
}
return &rval, nil
}
func (h *hapi) GetEdgeHostname(ctx context.Context, edgeHostnameID int) (*GetEdgeHostnameResponse, error) {
logger := h.Log(ctx)
logger.Debug("GetEdgeHostname")
uri := fmt.Sprintf("/hapi/v1/edge-hostnames/%d", edgeHostnameID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeHostname, err)
}
var rval GetEdgeHostnameResponse
resp, err := h.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeHostname, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetEdgeHostname, h.Error(resp))
}
return &rval, nil
}
func (h *hapi) UpdateEdgeHostname(ctx context.Context, request UpdateEdgeHostnameRequest) (*UpdateEdgeHostnameResponse, error) {
logger := h.Log(ctx)
logger.Debug("UpdateEdgeHostname")
if err := request.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s: %s", ErrUpdateEdgeHostname, ErrStructValidation, err)
}
uri := fmt.Sprintf("/hapi/v1/dns-zones/%s/edge-hostnames/%s", request.DNSZone, request.RecordName)
body, err := buildBody(request.Body)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request body", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, uri, body)
if err != nil {
return nil, err
}
q := req.URL.Query()
if len(request.StatusUpdateEmail) > 0 {
emails := strings.Join(request.StatusUpdateEmail, ",")
q.Add("statusUpdateEmail", emails)
}
if request.Comments != "" {
q.Add("comments", request.Comments)
}
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/json-patch+json")
var result UpdateEdgeHostnameResponse
resp, err := h.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateEdgeHostname, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("%w: %s", ErrUpdateEdgeHostname, h.Error(resp))
}
return &result, nil
}
func (h *hapi) GetCertificate(ctx context.Context, params GetCertificateRequest) (*GetCertificateResponse, error) {
logger := h.Log(ctx)
logger.Debug("GetCertificate")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetCertificate, ErrStructValidation, err)
}
uri := fmt.Sprintf(
"/hapi/v1/dns-zones/%s/edge-hostnames/%s/certificate",
params.DNSZone,
params.RecordName,
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetCertificate, err)
}
var result GetCertificateResponse
resp, err := h.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetCertificate, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%s: %s: %w", ErrGetCertificate, ErrNotFound, h.Error(resp))
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetCertificate, h.Error(resp))
}
return &result, nil
}
func buildBody(body []UpdateEdgeHostnameRequestBody) (io.Reader, error) {
reqBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(reqBody), nil
}