forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logpull.go
58 lines (51 loc) · 1.82 KB
/
logpull.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
package cloudflare
import (
"context"
"encoding/json"
"net/http"
"github.com/pkg/errors"
)
// LogpullRetentionConfiguration describes a the structure of a Logpull Retention
// payload.
type LogpullRetentionConfiguration struct {
Flag bool `json:"flag"`
}
// LogpullRetentionConfigurationResponse is the API response, containing the
// Logpull retention result.
type LogpullRetentionConfigurationResponse struct {
Response
Result LogpullRetentionConfiguration `json:"result"`
}
// GetLogpullRetentionFlag gets the current setting flag.
//
// API reference: https://developers.cloudflare.com/logs/logpull-api/enabling-log-retention/
func (api *API) GetLogpullRetentionFlag(ctx context.Context, zoneID string) (*LogpullRetentionConfiguration, error) {
uri := "/zones/" + zoneID + "/logs/control/retention/flag"
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return &LogpullRetentionConfiguration{}, err
}
var r LogpullRetentionConfigurationResponse
err = json.Unmarshal(res, &r)
if err != nil {
return nil, errors.Wrap(err, errUnmarshalError)
}
return &r.Result, nil
}
// SetLogpullRetentionFlag updates the retention flag to the defined boolean.
//
// API reference: https://developers.cloudflare.com/logs/logpull-api/enabling-log-retention/
func (api *API) SetLogpullRetentionFlag(ctx context.Context, zoneID string, enabled bool) (*LogpullRetentionConfiguration, error) {
uri := "/zones/" + zoneID + "/logs/control/retention/flag"
flagPayload := LogpullRetentionConfiguration{Flag: enabled}
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, flagPayload)
if err != nil {
return &LogpullRetentionConfiguration{}, err
}
var r LogpullRetentionConfigurationResponse
err = json.Unmarshal(res, &r)
if err != nil {
return &LogpullRetentionConfiguration{}, err
}
return &r.Result, nil
}