Skip to content

Commit

Permalink
add support for cache reserve
Browse files Browse the repository at this point in the history
  • Loading branch information
jfchevrette committed May 8, 2023
1 parent 9388783 commit d7fc5d4
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
79 changes: 79 additions & 0 deletions zone_cache_reserve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)

var validOnOffSettingValues = []string{"on", "off"}

// ZoneCacheReserve is the structure of the API object for the
// cache reserve setting.
type ZoneCacheReserve struct {
ID string `json:"id,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Value string `json:"value"`
}

// CacheReserveDetailsResponse is the API response for the
// cache reserve setting.
type CacheReserveDetailsResponse struct {
Result ZoneCacheReserve `json:"result"`
Response
}

type updateZoneCacheReserveRequest struct {
Value string `json:"value"`
}

type zoneCacheReserveSingleResponse struct {
Response
Result ZoneCacheReserve `json:"result"`
}

// ZoneCacheReserve returns information about the current cache reserve settings
//
// API reference: https://developers.cloudflare.com/api/operations/zone-cache-settings-get-cache-reserve-setting
func (api *API) ZoneCacheReserve(ctx context.Context, zoneID string) (ZoneCacheReserve, error) {
uri := fmt.Sprintf("/zones/%s/cache/cache_reserve", zoneID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZoneCacheReserve{}, err
}

var cacheReserveDetailsResponse CacheReserveDetailsResponse
err = json.Unmarshal(res, &cacheReserveDetailsResponse)
if err != nil {
return ZoneCacheReserve{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return cacheReserveDetailsResponse.Result, nil
}

// UpdateZoneCacheReserve updates the cache reserve setting for a zone
//
// API reference: https://developers.cloudflare.com/api/operations/zone-cache-settings-change-cache-reserve-setting
func (api *API) UpdateZoneCacheReserve(ctx context.Context, zoneID string, value string) (ZoneCacheReserve, error) {
if !contains(validOnOffSettingValues, value) {
return ZoneCacheReserve{}, fmt.Errorf("invalid setting value '%s'. must be 'on' or 'off'", value)
}

uri := fmt.Sprintf("/zones/%s/cache/cache_reserve", zoneID)

updateReq := &updateZoneCacheReserveRequest{Value: value}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, updateReq)
if err != nil {
return ZoneCacheReserve{}, err
}

response := &zoneCacheReserveSingleResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return ZoneCacheReserve{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return response.Result, nil
}
93 changes: 93 additions & 0 deletions zone_cache_reserve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

var cacheReserveTimestampString = "2019-02-20T22:37:07.107449Z"
var cacheReserveTimestamp, _ = time.Parse(time.RFC3339Nano, cacheReserveTimestampString)

func TestZoneCacheReserve(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "cache_reserve",
"value": "on",
"modified_on": "%s"
}
}
`, cacheReserveTimestampString)
}

mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/cache/cache_reserve", handler)
want := ZoneCacheReserve{
ID: "cache_reserve",
Value: "on",
ModifiedOn: cacheReserveTimestamp,
}

actual, err := client.ZoneCacheReserve(context.Background(), "01a7362d577a6c3019a474fd6f485823")

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestUpdateZoneCacheReserve(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "cache_reserve",
"value": "off",
"modified_on": "%s"
}
}
`, cacheReserveTimestampString)
}

mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/cache/cache_reserve", handler)
want := ZoneCacheReserve{
ID: "cache_reserve",
Value: "off",
ModifiedOn: cacheReserveTimestamp,
}

actual, err := client.UpdateZoneCacheReserve(context.Background(), "01a7362d577a6c3019a474fd6f485823", "off")

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestUpdateCacheReserveWithInvalidValue(t *testing.T) {
setup()
defer teardown()

_, err := client.UpdateZoneCacheReserve(context.Background(), "01a7362d577a6c3019a474fd6f485823", "invalid")

if assert.Error(t, err) {
assert.Equal(t, "invalid setting value 'invalid'. must be 'on' or 'off'", err.Error())
}
}

0 comments on commit d7fc5d4

Please sign in to comment.