Skip to content

Commit e91b3c2

Browse files
authored
logpull: Add initial support for retention flag (#448)
Updates the library to add support for logpull retention flag. API documentation: https://developers.cloudflare.com/logs/logpull-api/enabling-log-retention/ Paves the way for support in the Terraform provider at terraform-providers/terraform-provider-cloudflare#665
1 parent 4dc225a commit e91b3c2

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

logpull.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cloudflare
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/pkg/errors"
7+
)
8+
9+
// LogpullRentionConfiguration describes a the structure of a Logpull Rention
10+
// payload.
11+
type LogpullRentionConfiguration struct {
12+
Flag bool `json:"flag"`
13+
}
14+
15+
// LogpullRentionConfigurationResponse is the API response, containing the
16+
// Logpull rentention result.
17+
type LogpullRentionConfigurationResponse struct {
18+
Response
19+
Result LogpullRentionConfiguration `json:"result"`
20+
}
21+
22+
// GetLogpullRentionFlag gets the current setting flag.
23+
//
24+
// API reference: https://developers.cloudflare.com/logs/logpull-api/enabling-log-retention/
25+
func (api *API) GetLogpullRentionFlag(zoneID string) (*LogpullRentionConfiguration, error) {
26+
uri := "/zones/" + zoneID + "/logs/control/retention/flag"
27+
res, err := api.makeRequest("GET", uri, nil)
28+
if err != nil {
29+
return &LogpullRentionConfiguration{}, errors.Wrap(err, errMakeRequestError)
30+
}
31+
var r LogpullRentionConfigurationResponse
32+
err = json.Unmarshal(res, &r)
33+
if err != nil {
34+
return nil, errors.Wrap(err, errUnmarshalError)
35+
}
36+
return &r.Result, nil
37+
}
38+
39+
// SetLogpullRentionFlag updates the retention flag to the defined boolean.
40+
//
41+
// API reference: https://developers.cloudflare.com/logs/logpull-api/enabling-log-retention/
42+
func (api *API) SetLogpullRentionFlag(zoneID string, enabled bool) (*LogpullRentionConfiguration, error) {
43+
uri := "/zones/" + zoneID + "/logs/control/retention/flag"
44+
flagPayload := LogpullRentionConfiguration{Flag: enabled}
45+
46+
res, err := api.makeRequest("POST", uri, flagPayload)
47+
if err != nil {
48+
return &LogpullRentionConfiguration{}, errors.Wrap(err, errMakeRequestError)
49+
}
50+
var r LogpullRentionConfigurationResponse
51+
err = json.Unmarshal(res, &r)
52+
if err != nil {
53+
return &LogpullRentionConfiguration{}, errors.Wrap(err, errMakeRequestError)
54+
}
55+
return &r.Result, nil
56+
}

logpull_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cloudflare
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestGetLogpullRentionFlag(t *testing.T) {
12+
setup()
13+
defer teardown()
14+
15+
handler := func(w http.ResponseWriter, r *http.Request) {
16+
assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method)
17+
w.Header().Set("content-type", "application/json")
18+
fmt.Fprintf(w, `{
19+
"errors": [],
20+
"messages": [],
21+
"result": {
22+
"flag": true
23+
},
24+
"success": true
25+
}`)
26+
}
27+
28+
mux.HandleFunc("/zones/d56084adb405e0b7e32c52321bf07be6/logs/control/retention/flag", handler)
29+
want := &LogpullRentionConfiguration{Flag: true}
30+
31+
actual, err := client.GetLogpullRentionFlag("d56084adb405e0b7e32c52321bf07be6")
32+
if assert.NoError(t, err) {
33+
assert.Equal(t, want, actual)
34+
}
35+
}
36+
37+
func TestSetLogpullRentionFlag(t *testing.T) {
38+
setup()
39+
defer teardown()
40+
41+
handler := func(w http.ResponseWriter, r *http.Request) {
42+
assert.Equal(t, r.Method, "POST", "Expected method 'POST', got %s", r.Method)
43+
w.Header().Set("content-type", "application/json")
44+
fmt.Fprintf(w, `{
45+
"errors": [],
46+
"messages": [],
47+
"result": {
48+
"flag": false
49+
},
50+
"success": true
51+
}`)
52+
}
53+
54+
mux.HandleFunc("/zones/d56084adb405e0b7e32c52321bf07be6/logs/control/retention/flag", handler)
55+
want := &LogpullRentionConfiguration{Flag: false}
56+
57+
actual, err := client.SetLogpullRentionFlag("d56084adb405e0b7e32c52321bf07be6", false)
58+
if assert.NoError(t, err) {
59+
assert.Equal(t, want, actual)
60+
}
61+
}

0 commit comments

Comments
 (0)