-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathedgehostname.go
289 lines (252 loc) · 10.3 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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"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 (
// GetEdgeHostnamesRequest contains query params used for listing edge hostnames
GetEdgeHostnamesRequest struct {
ContractID string
GroupID string
Options []string
}
// GetEdgeHostnameRequest contains path and query params used to fetch specific edge hostname
GetEdgeHostnameRequest struct {
EdgeHostnameID string
ContractID string
GroupID string
Options []string
}
// GetEdgeHostnamesResponse contains data received by calling GetEdgeHostnames or GetEdgeHostname
GetEdgeHostnamesResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
EdgeHostnames EdgeHostnameItems `json:"edgeHostnames"`
EdgeHostname EdgeHostnameGetItem
}
// EdgeHostnameItems contains a list of EdgeHostnames
EdgeHostnameItems struct {
Items []EdgeHostnameGetItem `json:"items"`
}
// EdgeHostnameGetItem contains GET details for edge hostname
EdgeHostnameGetItem struct {
ID string `json:"edgeHostnameId"`
Domain string `json:"edgeHostnameDomain"`
ProductID string `json:"productId"`
DomainPrefix string `json:"domainPrefix"`
DomainSuffix string `json:"domainSuffix"`
Status string `json:"status,omitempty"`
Secure bool `json:"secure"`
IPVersionBehavior string `json:"ipVersionBehavior"`
UseCases []UseCase `json:"useCases,omitempty"`
}
// UseCase contains UseCase data
UseCase struct {
Option string `json:"option"`
Type string `json:"type"`
UseCase string `json:"useCase"`
}
// CreateEdgeHostnameRequest contains query params and body required for creation of new edge hostname
CreateEdgeHostnameRequest struct {
ContractID string
GroupID string
Options []string
EdgeHostname EdgeHostnameCreate
}
// EdgeHostnameCreate contains body of edge hostname POST request
EdgeHostnameCreate struct {
ProductID string `json:"productId"`
DomainPrefix string `json:"domainPrefix"`
DomainSuffix string `json:"domainSuffix"`
Secure bool `json:"secure,omitempty"`
SecureNetwork string `json:"secureNetwork,omitempty"`
SlotNumber int `json:"slotNumber,omitempty"`
IPVersionBehavior string `json:"ipVersionBehavior"`
CertEnrollmentID int `json:"certEnrollmentId,omitempty"`
UseCases []UseCase `json:"useCases,omitempty"`
}
// CreateEdgeHostnameResponse contains a link returned after creating new edge hostname and DI of this hostname
CreateEdgeHostnameResponse struct {
EdgeHostnameLink string `json:"edgeHostnameLink"`
EdgeHostnameID string `json:"-"`
}
)
const (
// EHSecureNetworkStandardTLS constant
EHSecureNetworkStandardTLS = "STANDARD_TLS"
// EHSecureNetworkSharedCert constant
EHSecureNetworkSharedCert = "SHARED_CERT"
// EHSecureNetworkEnhancedTLS constant
EHSecureNetworkEnhancedTLS = "ENHANCED_TLS"
// EHIPVersionV4 constant
EHIPVersionV4 = "IPV4"
// EHIPVersionV6Performance constant
EHIPVersionV6Performance = "IPV6_PERFORMANCE"
// EHIPVersionV6Compliance constant
EHIPVersionV6Compliance = "IPV6_COMPLIANCE"
// UseCaseGlobal constant
UseCaseGlobal = "GLOBAL"
)
// Validate validates CreateEdgeHostnameRequest
func (eh CreateEdgeHostnameRequest) Validate() error {
errs := validation.Errors{
"ContractID": validation.Validate(eh.ContractID, validation.Required),
"GroupID": validation.Validate(eh.GroupID, validation.Required),
"EdgeHostname": validation.Validate(eh.EdgeHostname),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates EdgeHostnameCreate
func (eh EdgeHostnameCreate) Validate() error {
return validation.Errors{
"DomainPrefix": validation.Validate(eh.DomainPrefix, validation.Required),
"DomainSuffix": validation.Validate(eh.DomainSuffix, validation.Required,
validation.When(eh.SecureNetwork == EHSecureNetworkStandardTLS, validation.In("edgesuite.net")),
validation.When(eh.SecureNetwork == EHSecureNetworkSharedCert, validation.In("akamaized.net")),
validation.When(eh.SecureNetwork == EHSecureNetworkEnhancedTLS, validation.In("edgekey.net")),
),
"ProductID": validation.Validate(eh.ProductID, validation.Required),
"CertEnrollmentID": validation.Validate(eh.CertEnrollmentID, validation.Required.When(eh.SecureNetwork == EHSecureNetworkEnhancedTLS)),
"IPVersionBehavior": validation.Validate(eh.IPVersionBehavior, validation.Required, validation.In(EHIPVersionV4, EHIPVersionV6Performance, EHIPVersionV6Compliance)),
"SecureNetwork": validation.Validate(eh.SecureNetwork, validation.In(EHSecureNetworkStandardTLS, EHSecureNetworkSharedCert, EHSecureNetworkEnhancedTLS)),
"UseCases": validation.Validate(eh.UseCases),
}.Filter()
}
// Validate validates UseCase
func (uc UseCase) Validate() error {
return validation.Errors{
"Option": validation.Validate(uc.Option, validation.Required),
"Type": validation.Validate(uc.Type, validation.Required, validation.In(UseCaseGlobal)),
"UseCase": validation.Validate(uc.UseCase, validation.Required),
}.Filter()
}
// Validate validates GetEdgeHostnamesRequest
func (eh GetEdgeHostnamesRequest) Validate() error {
return validation.Errors{
"ContractID": validation.Validate(eh.ContractID, validation.Required),
"GroupID": validation.Validate(eh.GroupID, validation.Required),
}.Filter()
}
// Validate validates GetEdgeHostnameRequest
func (eh GetEdgeHostnameRequest) Validate() error {
return validation.Errors{
"EdgeHostnameID": validation.Validate(eh.EdgeHostnameID, validation.Required),
"ContractID": validation.Validate(eh.ContractID, validation.Required),
"GroupID": validation.Validate(eh.GroupID, validation.Required),
}.Filter()
}
var (
// ErrGetEdgeHostnames represents error when fetching edge hostnames fails
ErrGetEdgeHostnames = errors.New("fetching edge hostnames")
// ErrGetEdgeHostname represents error when fetching edge hostname fails
ErrGetEdgeHostname = errors.New("fetching edge hostname")
// ErrCreateEdgeHostname represents error when creating edge hostname fails
ErrCreateEdgeHostname = errors.New("creating edge hostname")
)
// GetEdgeHostnames id used to list edge hostnames for provided group and contract IDs
func (p *papi) GetEdgeHostnames(ctx context.Context, params GetEdgeHostnamesRequest) (*GetEdgeHostnamesResponse, error) {
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetEdgeHostnames, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("GetEdgeHostnames")
getURL := fmt.Sprintf(
"/papi/v1/edgehostnames?contractId=%s&groupId=%s",
params.ContractID,
params.GroupID,
)
if len(params.Options) > 0 {
getURL = fmt.Sprintf("%s&options=%s", getURL, strings.Join(params.Options, ","))
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeHostnames, err)
}
var edgeHostnames GetEdgeHostnamesResponse
resp, err := p.Exec(req, &edgeHostnames)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeHostnames, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetEdgeHostnames, p.Error(resp))
}
return &edgeHostnames, nil
}
// GetEdgeHostname id used to fetch edge hostname with given ID for provided group and contract IDs
func (p *papi) GetEdgeHostname(ctx context.Context, params GetEdgeHostnameRequest) (*GetEdgeHostnamesResponse, error) {
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetEdgeHostname, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("GetEdgeHostname")
getURL := fmt.Sprintf(
"/papi/v1/edgehostnames/%s?contractId=%s&groupId=%s",
params.EdgeHostnameID,
params.ContractID,
params.GroupID,
)
if len(params.Options) > 0 {
getURL = fmt.Sprintf("%s&options=%s", getURL, strings.Join(params.Options, ","))
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeHostname, err)
}
var edgeHostname GetEdgeHostnamesResponse
resp, err := p.Exec(req, &edgeHostname)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeHostname, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
if len(edgeHostname.EdgeHostnames.Items) == 0 {
return nil, fmt.Errorf("%s: %w: EdgeHostnameID: %s", ErrGetEdgeHostname, ErrNotFound, params.EdgeHostnameID)
}
edgeHostname.EdgeHostname = edgeHostname.EdgeHostnames.Items[0]
return &edgeHostname, nil
}
// CreateEdgeHostname id used to create new edge hostname for provided group and contract IDs
func (p *papi) CreateEdgeHostname(ctx context.Context, r CreateEdgeHostnameRequest) (*CreateEdgeHostnameResponse, error) {
if err := r.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateEdgeHostname, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("CreateEdgeHostname")
createURL := fmt.Sprintf(
"/papi/v1/edgehostnames?contractId=%s&groupId=%s",
r.ContractID,
r.GroupID,
)
if len(r.Options) > 0 {
createURL = fmt.Sprintf("%s&options=%s", createURL, strings.Join(r.Options, ","))
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, createURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateEdgeHostname, err)
}
var createResponse CreateEdgeHostnameResponse
resp, err := p.Exec(req, &createResponse, r.EdgeHostname)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateEdgeHostname, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateEdgeHostname, p.Error(resp))
}
id, err := ResponseLinkParse(createResponse.EdgeHostnameLink)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateEdgeHostname, ErrInvalidResponseLink, err)
}
createResponse.EdgeHostnameID = id
return &createResponse, nil
}