-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathactive_property_hostname.go
279 lines (240 loc) · 10.4 KB
/
active_property_hostname.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v10/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// SortOrder represents SortOrder enum.
SortOrder string
// CertType represents CertType enum.
CertType string
// ListActivePropertyHostnamesRequest contains parameters required to list active property hostnames.
ListActivePropertyHostnamesRequest struct {
PropertyID string
Offset int
Limit int
Sort SortOrder
Hostname string
CnameTo string
Network ActivationNetwork
ContractID string
GroupID string
IncludeCertStatus bool
}
// GetActivePropertyHostnamesDiffRequest contains parameters required to list active property hostnames diff.
GetActivePropertyHostnamesDiffRequest struct {
PropertyID string
Offset int
Limit int
ContractID string
GroupID string
}
// ListActivePropertyHostnamesResponse contains information about each of the active property hostnames.
ListActivePropertyHostnamesResponse struct {
AccountID string `json:"accountId"`
AvailableSort []SortOrder `json:"availableSort"`
ContractID string `json:"contractId"`
CurrentSort SortOrder `json:"currentSort"`
DefaultSort SortOrder `json:"defaultSort"`
GroupID string `json:"groupId"`
PropertyID string `json:"propertyId"`
PropertyName string `json:"propertyName"`
Hostnames HostnamesResponseItems `json:"hostnames"`
}
// GetActivePropertyHostnamesDiffResponse contains information about each of the active property hostnames diff.
GetActivePropertyHostnamesDiffResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
PropertyID string `json:"propertyId"`
Hostnames HostnamesDiffResponseItems `json:"hostnames"`
}
// HostnamesResponseItems contains the response body for ListActivePropertyHostnamesResponse.
HostnamesResponseItems struct {
Items []HostnameItem `json:"items"`
CurrentItemCount int `json:"currentItemCount"`
NextLink *string `json:"nextLink"`
PreviousLink *string `json:"previousLink"`
TotalItems int `json:"totalItems"`
}
// HostnamesDiffResponseItems contains the response body for GetActivePropertyHostnamesDiffResponse.
HostnamesDiffResponseItems struct {
Items []HostnameDiffItem `json:"items"`
CurrentItemCount int `json:"currentItemCount"`
NextLink *string `json:"nextLink"`
PreviousLink *string `json:"previousLink"`
TotalItems int `json:"totalItems"`
}
// HostnameItem contains information about each of the HostnamesResponseItems.
HostnameItem struct {
CertStatus *CertStatusItem `json:"certStatus"`
CnameFrom string `json:"cnameFrom"`
CnameType HostnameCnameType `json:"cnameType"`
ProductionCertType CertType `json:"productionCertType"`
ProductionCnameTo string `json:"productionCnameTo"`
ProductionEdgeHostnameID string `json:"productionEdgeHostnameId"`
StagingCertType CertType `json:"stagingCertType"`
StagingCnameTo string `json:"StagingCnameTo"`
StagingEdgeHostnameID string `json:"stagingEdgeHostnameId"`
}
// HostnameDiffItem contains information about each of the HostnamesDiffResponseItems.
HostnameDiffItem struct {
CnameFrom string `json:"cnameFrom"`
ProductionCertProvisioningType CertType `json:"productionCertProvisioningType"`
ProductionCnameTo string `json:"productionCnameTo"`
ProductionCnameType HostnameCnameType `json:"productionCnameType"`
ProductionEdgeHostnameID string `json:"productionEdgeHostnameId"`
StagingCertProvisioningType CertType `json:"stagingCertProvisioningType"`
StagingCnameTo string `json:"stagingCnameTo"`
StagingCnameType HostnameCnameType `json:"stagingCnameType"`
StagingEdgeHostnameID string `json:"stagingEdgeHostnameId"`
}
)
const (
// SortAscending represents ascending sorting by hostname.
SortAscending SortOrder = "hostname:a"
// SortDescending represents descending sorting by hostname.
SortDescending SortOrder = "hostname:d"
// CertTypeCPSManaged indicates that the certificate is provisioned using the Certificate Provisioning System (CPS).
CertTypeCPSManaged CertType = "CPS_MANAGED"
// CertTypeDefault indicates that the certificate is a Default Domain Validation (DV) certificate.
CertTypeDefault CertType = "DEFAULT"
// maxHostnamesPerPage indicates the maximum possible value for 'limit' parameter for Get and List active property hostnames.
maxHostnamesPerPage int = 999
)
var (
// ErrListActivePropertyHostnames represents error when fetching active property hostnames fails.
ErrListActivePropertyHostnames = errors.New("fetching active property hostnames")
// ErrGetActivePropertyHostnamesDiff represents error when fetching active property hostnames diff fails.
ErrGetActivePropertyHostnamesDiff = errors.New("fetching active property hostnames diff")
)
// Validate validates SortOrder.
func (o SortOrder) Validate() validation.InRule {
return validation.In(SortAscending, SortDescending).
Error(fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'",
o, SortAscending, SortDescending))
}
// Validate validates CertType.
func (t CertType) Validate() validation.InRule {
return validation.In(CertTypeCPSManaged, CertTypeDefault).
Error(fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'",
t, CertTypeCPSManaged, CertTypeDefault))
}
// Validate validates ListActivePropertyHostnamesRequest.
func (r ListActivePropertyHostnamesRequest) Validate() error {
return validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"Network": validation.Validate(r.Network, r.Network.Validate()),
"Sort": validation.Validate(r.Sort, r.Sort.Validate()),
"Offset": validation.Validate(r.Offset, validation.Min(0)),
"Limit": validation.Validate(r.Limit, validation.Min(1), validation.Max(maxHostnamesPerPage)),
}.Filter()
}
// Validate validates GetActivePropertyHostnamesDiffRequest.
func (r GetActivePropertyHostnamesDiffRequest) Validate() error {
return validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"Offset": validation.Validate(r.Offset, validation.Min(0)),
"Limit": validation.Validate(r.Limit, validation.Min(1), validation.Max(maxHostnamesPerPage)),
}.Filter()
}
func (p *papi) ListActivePropertyHostnames(ctx context.Context, params ListActivePropertyHostnamesRequest) (*ListActivePropertyHostnamesResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListActivePropertyHostnames")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListActivePropertyHostnames, ErrStructValidation, err)
}
baseURL := fmt.Sprintf("/papi/v1/properties/%s/hostnames", params.PropertyID)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("%w: failed to parse base URL: %s", ErrListActivePropertyHostnames, err)
}
query := parsedURL.Query()
if params.ContractID != "" {
query.Set("contractId", params.ContractID)
}
if params.GroupID != "" {
query.Set("groupId", params.GroupID)
}
if params.Sort != "" {
query.Set("sort", string(params.Sort))
}
if params.Hostname != "" {
query.Set("hostname", params.Hostname)
}
if params.CnameTo != "" {
query.Set("cnameTo", params.CnameTo)
}
if params.Network != "" {
query.Set("network", string(params.Network))
}
if params.IncludeCertStatus {
query.Set("includeCertStatus", fmt.Sprintf("%t", params.IncludeCertStatus))
}
if params.Limit != 0 {
query.Set("limit", fmt.Sprintf("%d", params.Limit))
}
if params.Offset != 0 {
query.Set("offset", fmt.Sprintf("%d", params.Offset))
}
parsedURL.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, parsedURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListActivePropertyHostnames, err)
}
var hostnames ListActivePropertyHostnamesResponse
resp, err := p.Exec(req, &hostnames)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListActivePropertyHostnames, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListActivePropertyHostnames, p.Error(resp))
}
return &hostnames, nil
}
func (p *papi) GetActivePropertyHostnamesDiff(ctx context.Context, params GetActivePropertyHostnamesDiffRequest) (*GetActivePropertyHostnamesDiffResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetActivePropertyHostnamesDiff")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetActivePropertyHostnamesDiff, ErrStructValidation, err)
}
baseURL := fmt.Sprintf("/papi/v1/properties/%s/hostnames/diff", params.PropertyID)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("%w: failed to parse base URL: %s", ErrGetActivePropertyHostnamesDiff, err)
}
query := parsedURL.Query()
if params.ContractID != "" {
query.Set("contractId", params.ContractID)
}
if params.GroupID != "" {
query.Set("groupId", params.GroupID)
}
if params.Limit != 0 {
query.Set("limit", fmt.Sprintf("%d", params.Limit))
}
if params.Offset != 0 {
query.Set("offset", fmt.Sprintf("%d", params.Offset))
}
parsedURL.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, parsedURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetActivePropertyHostnamesDiff, err)
}
var hostnamesDiff GetActivePropertyHostnamesDiffResponse
resp, err := p.Exec(req, &hostnamesDiff)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetActivePropertyHostnamesDiff, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetActivePropertyHostnamesDiff, p.Error(resp))
}
return &hostnamesDiff, nil
}