-
Notifications
You must be signed in to change notification settings - Fork 70
/
propertyhostname.go
197 lines (167 loc) · 6.99 KB
/
propertyhostname.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// GetPropertyVersionHostnamesRequest contains parameters required to list property version hostnames
GetPropertyVersionHostnamesRequest struct {
PropertyID string
PropertyVersion int
ContractID string
GroupID string
ValidateHostnames bool
IncludeCertStatus bool
}
// GetPropertyVersionHostnamesResponse contains all property version hostnames associated to the given parameters
GetPropertyVersionHostnamesResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
PropertyID string `json:"propertyId"`
PropertyVersion int `json:"propertyVersion"`
Etag string `json:"etag"`
Hostnames HostnameResponseItems `json:"hostnames"`
}
// HostnameResponseItems contains the response body for GetPropertyVersionHostnamesResponse
HostnameResponseItems struct {
Items []Hostname `json:"items"`
}
// Hostname contains information about each of the HostnameResponseItems
Hostname struct {
CnameType HostnameCnameType `json:"cnameType"`
EdgeHostnameID string `json:"edgeHostnameId,omitempty"`
CnameFrom string `json:"cnameFrom"`
CnameTo string `json:"cnameTo,omitempty"`
CertProvisioningType string `json:"certProvisioningType"`
CertStatus CertStatusItem `json:"certStatus,omitempty"`
}
// CertStatusItem contains information about certificate status for specific Hostname
CertStatusItem struct {
ValidationCname ValidationCname `json:"validationCname,omitempty"`
Staging []StatusItem `json:"staging,omitempty"`
Production []StatusItem `json:"production,omitempty"`
}
// ValidationCname is the CNAME record used to validate the certificate’s domain
ValidationCname struct {
Hostname string `json:"hostname,omitempty"`
Target string `json:"target,omitempty"`
}
// StatusItem determines whether a hostname is capable of serving secure content over the staging or production network.
StatusItem struct {
Status string `json:"status,omitempty"`
}
// UpdatePropertyVersionHostnamesRequest contains parameters required to update the set of hostname entries for a property version
UpdatePropertyVersionHostnamesRequest struct {
PropertyID string
PropertyVersion int
ContractID string
GroupID string
ValidateHostnames bool
IncludeCertStatus bool
Hostnames []Hostname
}
// UpdatePropertyVersionHostnamesResponse contains information about each of the HostnameRequestItems
UpdatePropertyVersionHostnamesResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
PropertyID string `json:"propertyId"`
PropertyVersion int `json:"propertyVersion"`
Etag string `json:"etag"`
Hostnames HostnameResponseItems `json:"hostnames"`
}
// HostnameCnameType represents HostnameCnameType enum
HostnameCnameType string
)
const (
// HostnameCnameTypeEdgeHostname const
HostnameCnameTypeEdgeHostname HostnameCnameType = "EDGE_HOSTNAME"
)
// Validate validates GetPropertyVersionHostnamesRequest
func (ph GetPropertyVersionHostnamesRequest) Validate() error {
return validation.Errors{
"PropertyID": validation.Validate(ph.PropertyID, validation.Required),
"PropertyVersion": validation.Validate(ph.PropertyVersion, validation.Required),
}.Filter()
}
// Validate validates UpdatePropertyVersionHostnamesRequest
func (ch UpdatePropertyVersionHostnamesRequest) Validate() error {
return validation.Errors{
"PropertyID": validation.Validate(ch.PropertyID, validation.Required),
"PropertyVersion": validation.Validate(ch.PropertyVersion, validation.Required),
}.Filter()
}
var (
// ErrGetPropertyVersionHostnames represents error when fetching hostnames fails
ErrGetPropertyVersionHostnames = errors.New("fetching hostnames")
// ErrUpdatePropertyVersionHostnames represents error when updating hostnames fails
ErrUpdatePropertyVersionHostnames = errors.New("updating hostnames")
)
func (p *papi) GetPropertyVersionHostnames(ctx context.Context, params GetPropertyVersionHostnamesRequest) (*GetPropertyVersionHostnamesResponse, error) {
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetPropertyVersionHostnames, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("GetPropertyVersionHostnames")
getURL := fmt.Sprintf(
"/papi/v1/properties/%s/versions/%d/hostnames?contractId=%s&groupId=%s&validateHostnames=%t&includeCertStatus=%t",
params.PropertyID,
params.PropertyVersion,
params.ContractID,
params.GroupID,
params.ValidateHostnames,
params.IncludeCertStatus)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPropertyVersionHostnames, err)
}
var hostnames GetPropertyVersionHostnamesResponse
resp, err := p.Exec(req, &hostnames)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetPropertyVersionHostnames, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetPropertyVersionHostnames, p.Error(resp))
}
return &hostnames, nil
}
func (p *papi) UpdatePropertyVersionHostnames(ctx context.Context, params UpdatePropertyVersionHostnamesRequest) (*UpdatePropertyVersionHostnamesResponse, error) {
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrUpdatePropertyVersionHostnames, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("UpdatePropertyVersionHostnames")
putURL := fmt.Sprintf(
"/papi/v1/properties/%s/versions/%v/hostnames?contractId=%s&groupId=%s&validateHostnames=%t&includeCertStatus=%t",
params.PropertyID,
params.PropertyVersion,
params.ContractID,
params.GroupID,
params.ValidateHostnames,
params.IncludeCertStatus,
)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdatePropertyVersionHostnames, err)
}
var hostnames UpdatePropertyVersionHostnamesResponse
newHostnames := params.Hostnames
if newHostnames == nil {
newHostnames = []Hostname{}
}
resp, err := p.Exec(req, &hostnames, newHostnames)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdatePropertyVersionHostnames, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdatePropertyVersionHostnames, p.Error(resp))
}
return &hostnames, nil
}