forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logpush.go
275 lines (249 loc) · 8.92 KB
/
logpush.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
// LogpushJob describes a Logpush job.
type LogpushJob struct {
ID int `json:"id,omitempty"`
Dataset string `json:"dataset"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
LogpullOptions string `json:"logpull_options"`
DestinationConf string `json:"destination_conf"`
OwnershipChallenge string `json:"ownership_challenge,omitempty"`
LastComplete *time.Time `json:"last_complete,omitempty"`
LastError *time.Time `json:"last_error,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}
// LogpushJobsResponse is the API response, containing an array of Logpush Jobs.
type LogpushJobsResponse struct {
Response
Result []LogpushJob `json:"result"`
}
// LogpushJobDetailsResponse is the API response, containing a single Logpush Job.
type LogpushJobDetailsResponse struct {
Response
Result LogpushJob `json:"result"`
}
// LogpushFieldsResponse is the API response for a datasets fields
type LogpushFieldsResponse struct {
Response
Result LogpushFields `json:"result"`
}
// LogpushFields is a map of available Logpush field names & descriptions
type LogpushFields map[string]string
// LogpushGetOwnershipChallenge describes a ownership validation.
type LogpushGetOwnershipChallenge struct {
Filename string `json:"filename"`
Valid bool `json:"valid"`
Message string `json:"message"`
}
// LogpushGetOwnershipChallengeResponse is the API response, containing a ownership challenge.
type LogpushGetOwnershipChallengeResponse struct {
Response
Result LogpushGetOwnershipChallenge `json:"result"`
}
// LogpushGetOwnershipChallengeRequest is the API request for get ownership challenge.
type LogpushGetOwnershipChallengeRequest struct {
DestinationConf string `json:"destination_conf"`
}
// LogpushOwnershipChallengeValidationResponse is the API response,
// containing a ownership challenge validation result.
type LogpushOwnershipChallengeValidationResponse struct {
Response
Result struct {
Valid bool `json:"valid"`
}
}
// LogpushValidateOwnershipChallengeRequest is the API request for validate ownership challenge.
type LogpushValidateOwnershipChallengeRequest struct {
DestinationConf string `json:"destination_conf"`
OwnershipChallenge string `json:"ownership_challenge"`
}
// LogpushDestinationExistsResponse is the API response,
// containing a destination exists check result.
type LogpushDestinationExistsResponse struct {
Response
Result struct {
Exists bool `json:"exists"`
}
}
// LogpushDestinationExistsRequest is the API request for check destination exists.
type LogpushDestinationExistsRequest struct {
DestinationConf string `json:"destination_conf"`
}
// CreateLogpushJob creates a new LogpushJob for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-create-logpush-job
func (api *API) CreateLogpushJob(ctx context.Context, zoneID string, job LogpushJob) (*LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, job)
if err != nil {
return nil, err
}
var r LogpushJobDetailsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return nil, errors.Wrap(err, errUnmarshalError)
}
return &r.Result, nil
}
// LogpushJobs returns all Logpush Jobs for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) LogpushJobs(ctx context.Context, zoneID string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []LogpushJob{}, err
}
var r LogpushJobsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []LogpushJob{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// LogpushJobsForDataset returns all Logpush Jobs for a dataset in a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs-for-a-dataset
func (api *API) LogpushJobsForDataset(ctx context.Context, zoneID, dataset string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/datasets/%s/jobs", zoneID, dataset)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []LogpushJob{}, err
}
var r LogpushJobsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []LogpushJob{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// LogpushFields returns fields for a given dataset.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) LogpushFields(ctx context.Context, zoneID, dataset string) (LogpushFields, error) {
uri := fmt.Sprintf("/zones/%s/logpush/datasets/%s/fields", zoneID, dataset)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return LogpushFields{}, err
}
var r LogpushFieldsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return LogpushFields{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// LogpushJob fetches detail about one Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-logpush-job-details
func (api *API) LogpushJob(ctx context.Context, zoneID string, jobID int) (LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return LogpushJob{}, err
}
var r LogpushJobDetailsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return LogpushJob{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// UpdateLogpushJob lets you update a Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-update-logpush-job
func (api *API) UpdateLogpushJob(ctx context.Context, zoneID string, jobID int, job LogpushJob) error {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, job)
if err != nil {
return err
}
var r LogpushJobDetailsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// DeleteLogpushJob deletes a Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-delete-logpush-job
func (api *API) DeleteLogpushJob(ctx context.Context, zoneID string, jobID int) error {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
var r LogpushJobDetailsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// GetLogpushOwnershipChallenge returns ownership challenge.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-get-ownership-challenge
func (api *API) GetLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf string) (*LogpushGetOwnershipChallenge, error) {
uri := fmt.Sprintf("/zones/%s/logpush/ownership", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushGetOwnershipChallengeRequest{
DestinationConf: destinationConf,
})
if err != nil {
return nil, err
}
var r LogpushGetOwnershipChallengeResponse
err = json.Unmarshal(res, &r)
if err != nil {
return nil, errors.Wrap(err, errUnmarshalError)
}
if !r.Result.Valid {
return nil, errors.New(r.Result.Message)
}
return &r.Result, nil
}
// ValidateLogpushOwnershipChallenge returns ownership challenge validation result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-validate-ownership-challenge
func (api *API) ValidateLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf, ownershipChallenge string) (bool, error) {
uri := fmt.Sprintf("/zones/%s/logpush/ownership/validate", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushValidateOwnershipChallengeRequest{
DestinationConf: destinationConf,
OwnershipChallenge: ownershipChallenge,
})
if err != nil {
return false, err
}
var r LogpushGetOwnershipChallengeResponse
err = json.Unmarshal(res, &r)
if err != nil {
return false, errors.Wrap(err, errUnmarshalError)
}
return r.Result.Valid, nil
}
// CheckLogpushDestinationExists returns destination exists check result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-check-destination-exists
func (api *API) CheckLogpushDestinationExists(ctx context.Context, zoneID, destinationConf string) (bool, error) {
uri := fmt.Sprintf("/zones/%s/logpush/validate/destination/exists", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushDestinationExistsRequest{
DestinationConf: destinationConf,
})
if err != nil {
return false, err
}
var r LogpushDestinationExistsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return false, errors.Wrap(err, errUnmarshalError)
}
return r.Result.Exists, nil
}