-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdeployments.go
179 lines (145 loc) · 6.18 KB
/
deployments.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
package cps
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ListDeploymentsRequest contains parameters for ListDeployments
ListDeploymentsRequest struct {
EnrollmentID int
}
// GetDeploymentRequest contains parameters for GetProductionDeployment and GetStagingDeployment
GetDeploymentRequest struct {
EnrollmentID int
}
// ListDeploymentsResponse contains response for ListDeployments
ListDeploymentsResponse struct {
Production *Deployment `json:"production"`
Staging *Deployment `json:"staging"`
}
// GetProductionDeploymentResponse contains response for GetProductionDeployment
GetProductionDeploymentResponse Deployment
// GetStagingDeploymentResponse contains response for GetStagingDeployment
GetStagingDeploymentResponse Deployment
// Deployment represents details of production or staging deployment
Deployment struct {
OCSPStapled *bool `json:"ocspStapled"`
OCSPURIs []string `json:"ocspUris"`
NetworkConfiguration DeploymentNetworkConfiguration `json:"networkConfiguration"`
PrimaryCertificate DeploymentCertificate `json:"primaryCertificate"`
MultiStackedCertificates []DeploymentCertificate `json:"multiStackedCertificates"`
}
// DeploymentCertificate represents certificate in context of deployment operation
DeploymentCertificate struct {
Certificate string `json:"certificate"`
Expiry string `json:"expiry"`
KeyAlgorithm string `json:"keyAlgorithm"`
SignatureAlgorithm string `json:"signatureAlgorithm"`
TrustChain string `json:"trustChain"`
}
// DeploymentNetworkConfiguration represents network configuration in context of deployment operation
DeploymentNetworkConfiguration struct {
Geography string `json:"geography"`
MustHaveCiphers string `json:"mustHaveCiphers"`
OCSPStapling string `json:"ocspStapling"`
PreferredCiphers string `json:"preferredCiphers"`
QUICEnabled bool `json:"quicEnabled"`
SecureNetwork string `json:"secureNetwork"`
SNIOnly bool `json:"sniOnly"`
DisallowedTLSVersions []string `json:"disallowedTlsVersions"`
DNSNames []string `json:"dnsNames"`
}
)
// Validate validates ListDeploymentsRequest
func (c ListDeploymentsRequest) Validate() error {
return validation.Errors{
"EnrollmentID": validation.Validate(c.EnrollmentID, validation.Required),
}.Filter()
}
// Validate validates GetDeploymentsRequest
func (c GetDeploymentRequest) Validate() error {
return validation.Errors{
"EnrollmentID": validation.Validate(c.EnrollmentID, validation.Required),
}.Filter()
}
var (
// ErrListDeployments is returned when ListDeployments fails
ErrListDeployments = errors.New("list deployments")
// ErrGetProductionDeployment is returned when GetProductionDeployment fails
ErrGetProductionDeployment = errors.New("get production deployment")
// ErrGetStagingDeployment is returned when GetStagingDeployment fails
ErrGetStagingDeployment = errors.New("get staging deployment")
)
func (c *cps) ListDeployments(ctx context.Context, params ListDeploymentsRequest) (*ListDeploymentsResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListDeployments")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListDeployments, ErrStructValidation, err)
}
uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments", params.EnrollmentID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListDeployments, err)
}
req.Header.Set("Accept", "application/vnd.akamai.cps.deployments.v8+json")
var result ListDeploymentsResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListDeployments, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListDeployments, c.Error(resp))
}
return &result, nil
}
func (c *cps) GetProductionDeployment(ctx context.Context, params GetDeploymentRequest) (*GetProductionDeploymentResponse, error) {
logger := c.Log(ctx)
logger.Debug("GetProductionDeployment")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetProductionDeployment, ErrStructValidation, err)
}
uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments/production", params.EnrollmentID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetProductionDeployment, err)
}
req.Header.Set("Accept", "application/vnd.akamai.cps.deployment.v8+json")
var result GetProductionDeploymentResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetProductionDeployment, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetProductionDeployment, c.Error(resp))
}
return &result, nil
}
func (c *cps) GetStagingDeployment(ctx context.Context, params GetDeploymentRequest) (*GetStagingDeploymentResponse, error) {
logger := c.Log(ctx)
logger.Debug("GetStagingDeployment")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetStagingDeployment, ErrStructValidation, err)
}
uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments/staging", params.EnrollmentID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetStagingDeployment, err)
}
req.Header.Set("Accept", "application/vnd.akamai.cps.deployment.v8+json")
var result GetStagingDeploymentResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetStagingDeployment, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetStagingDeployment, c.Error(resp))
}
return &result, nil
}