forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logpull.go
59 lines (52 loc) · 1.85 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
59
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"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 := fmt.Sprintf("/zones/%s/logs/control/retention/flag", zoneID)
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 := fmt.Sprintf("/zones/%s/logs/control/retention/flag", zoneID)
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
}