/
pages_deployment.go
326 lines (271 loc) · 9.73 KB
/
pages_deployment.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
package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
// SizeOptions can be passed to a list request to configure size and cursor location.
// These values will be defaulted if omitted.
//
// This should be swapped to ResultInfoCursors once the types are corrected.
type SizeOptions struct {
Size int `json:"size,omitempty" url:"size,omitempty"`
Before *int `json:"before,omitempty" url:"before,omitempty"`
After *int `json:"after,omitempty" url:"after,omitempty"`
}
// PagesDeploymentStageLogs represents the logs for a Pages deployment stage.
type PagesDeploymentStageLogs struct {
Name string `json:"name"`
StartedOn *time.Time `json:"started_on"`
EndedOn *time.Time `json:"ended_on"`
Status string `json:"status"`
Start int `json:"start"`
End int `json:"end"`
Total int `json:"total"`
Data []PagesDeploymentStageLogEntry `json:"data"`
}
// PagesDeploymentStageLogEntry represents a single log entry in a Pages deployment stage.
type PagesDeploymentStageLogEntry struct {
ID int `json:"id"`
Timestamp *time.Time `json:"timestamp"`
Message string `json:"message"`
}
// PagesDeploymentLogs represents the logs for a Pages deployment.
type PagesDeploymentLogs struct {
Total int `json:"total"`
IncludesContainerLogs bool `json:"includes_container_logs"`
Data []PagesDeploymentLogEntry `json:"data"`
}
// PagesDeploymentLogEntry represents a single log entry in a Pages deployment.
type PagesDeploymentLogEntry struct {
Timestamp *time.Time `json:"ts"`
Line string `json:"line"`
}
type pagesDeploymentListResponse struct {
Response
Result []PagesProjectDeployment `json:"result"`
ResultInfo `json:"result_info"`
}
type pagesDeploymentResponse struct {
Response
Result PagesProjectDeployment `json:"result"`
}
type pagesDeploymentStageLogsResponse struct {
Response
Result PagesDeploymentStageLogs `json:"result"`
ResultInfo `json:"result_info"`
}
type pagesDeploymentLogsResponse struct {
Response
Result PagesDeploymentLogs `json:"result"`
ResultInfo `json:"result_info"`
}
type ListPagesDeploymentsParams struct {
ProjectName string
PaginationOptions
}
type GetPagesDeploymentInfoParams struct {
ProjectName string
DeploymentID string
}
type GetPagesDeploymentStageLogsParams struct {
ProjectName string
DeploymentID string
StageName string
SizeOptions
}
type GetPagesDeploymentLogsParams struct {
ProjectName string
DeploymentID string
SizeOptions
}
type DeletePagesDeploymentParams struct {
ProjectName string
DeploymentID string
}
type CreatePagesDeploymentParams struct {
ProjectName string
}
type RetryPagesDeploymentParams struct {
ProjectName string
DeploymentID string
}
type RollbackPagesDeploymentParams struct {
ProjectName string
DeploymentID string
}
var (
ErrMissingProjectName = errors.New("required missing project name")
ErrMissingDeploymentID = errors.New("required missing deployment ID")
ErrMissingStageName = errors.New("required missing stage name")
)
// ListPagesDeployments returns all deployments for a Pages project.
//
// API reference: https://api.cloudflare.com/#pages-deployment-get-deployments
func (api *API) ListPagesDeployments(ctx context.Context, rc *ResourceContainer, params ListPagesDeploymentsParams) ([]PagesProjectDeployment, ResultInfo, error) {
if rc.Identifier == "" {
return []PagesProjectDeployment{}, ResultInfo{}, ErrMissingAccountID
}
if params.ProjectName == "" {
return []PagesProjectDeployment{}, ResultInfo{}, ErrMissingProjectName
}
uri := buildURI(fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments", rc.Identifier, params.ProjectName), params.PaginationOptions)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []PagesProjectDeployment{}, ResultInfo{}, err
}
var r pagesDeploymentListResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []PagesProjectDeployment{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, r.ResultInfo, nil
}
// GetPagesDeploymentInfo returns a deployment for a Pages project.
//
// API reference: https://api.cloudflare.com/#pages-deployment-get-deployment-info
func (api *API) GetPagesDeploymentInfo(ctx context.Context, rc *ResourceContainer, projectName, deploymentID string) (PagesProjectDeployment, error) {
if rc.Identifier == "" {
return PagesProjectDeployment{}, ErrMissingAccountID
}
if projectName == "" {
return PagesProjectDeployment{}, ErrMissingProjectName
}
if deploymentID == "" {
return PagesProjectDeployment{}, ErrMissingDeploymentID
}
uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments/%s", rc.Identifier, projectName, deploymentID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return PagesProjectDeployment{}, err
}
var r pagesDeploymentResponse
err = json.Unmarshal(res, &r)
if err != nil {
return PagesProjectDeployment{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// GetPagesDeploymentLogs returns the logs for a Pages deployment.
//
// API reference: https://api.cloudflare.com/#pages-deployment-get-deployment-logs
func (api *API) GetPagesDeploymentLogs(ctx context.Context, rc *ResourceContainer, params GetPagesDeploymentLogsParams) (PagesDeploymentLogs, error) {
if rc.Identifier == "" {
return PagesDeploymentLogs{}, ErrMissingAccountID
}
if params.ProjectName == "" {
return PagesDeploymentLogs{}, ErrMissingProjectName
}
if params.DeploymentID == "" {
return PagesDeploymentLogs{}, ErrMissingDeploymentID
}
uri := buildURI(
fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments/%s/history/logs", rc.Identifier, params.ProjectName, params.DeploymentID),
params.SizeOptions,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return PagesDeploymentLogs{}, err
}
var r pagesDeploymentLogsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return PagesDeploymentLogs{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DeletePagesDeployment deletes a Pages deployment.
//
// API reference: https://api.cloudflare.com/#pages-deployment-delete-deployment
func (api *API) DeletePagesDeployment(ctx context.Context, rc *ResourceContainer, projectName, deploymentID string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}
if projectName == "" {
return ErrMissingProjectName
}
if deploymentID == "" {
return ErrMissingDeploymentID
}
uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments/%s", rc.Identifier, projectName, deploymentID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
// CreatePagesDeployment creates a Pages production deployment.
//
// API reference: https://api.cloudflare.com/#pages-deployment-create-deployment
func (api *API) CreatePagesDeployment(ctx context.Context, rc *ResourceContainer, params CreatePagesDeploymentParams) (PagesProjectDeployment, error) {
if rc.Identifier == "" {
return PagesProjectDeployment{}, ErrMissingAccountID
}
if params.ProjectName == "" {
return PagesProjectDeployment{}, ErrMissingProjectName
}
uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments", rc.Identifier, params.ProjectName)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return PagesProjectDeployment{}, err
}
var r pagesDeploymentResponse
err = json.Unmarshal(res, &r)
if err != nil {
return PagesProjectDeployment{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// RetryPagesDeployment retries a specific Pages deployment.
//
// API reference: https://api.cloudflare.com/#pages-deployment-retry-deployment
func (api *API) RetryPagesDeployment(ctx context.Context, rc *ResourceContainer, projectName, deploymentID string) (PagesProjectDeployment, error) {
if rc.Identifier == "" {
return PagesProjectDeployment{}, ErrMissingAccountID
}
if projectName == "" {
return PagesProjectDeployment{}, ErrMissingProjectName
}
if deploymentID == "" {
return PagesProjectDeployment{}, ErrMissingDeploymentID
}
uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments/%s/retry", rc.Identifier, projectName, deploymentID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return PagesProjectDeployment{}, err
}
var r pagesDeploymentResponse
err = json.Unmarshal(res, &r)
if err != nil {
return PagesProjectDeployment{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// RollbackPagesDeployment rollbacks the Pages production deployment to a previous production deployment.
//
// API reference: https://api.cloudflare.com/#pages-deployment-rollback-deployment
func (api *API) RollbackPagesDeployment(ctx context.Context, rc *ResourceContainer, projectName, deploymentID string) (PagesProjectDeployment, error) {
if rc.Identifier == "" {
return PagesProjectDeployment{}, ErrMissingAccountID
}
if projectName == "" {
return PagesProjectDeployment{}, ErrMissingProjectName
}
if deploymentID == "" {
return PagesProjectDeployment{}, ErrMissingDeploymentID
}
uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/deployments/%s/rollback", rc.Identifier, projectName, deploymentID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return PagesProjectDeployment{}, err
}
var r pagesDeploymentResponse
err = json.Unmarshal(res, &r)
if err != nil {
return PagesProjectDeployment{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}