Skip to content

Commit

Permalink
add ListDeviceSettingsPolicies
Browse files Browse the repository at this point in the history
  • Loading branch information
broswen committed Nov 1, 2023
1 parent 5a403c8 commit 60bff7f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
device_settings_policy: Add support for listing device settings policies
```
58 changes: 53 additions & 5 deletions devices_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type DeviceClientCertificatesZone struct {
type ServiceMode string

const (
oneDotOne ServiceMode = "1dot1"
warp ServiceMode = "warp"
proxy ServiceMode = "proxy"
postureOnly ServiceMode = "posture_only"
warpTunnelOnly ServiceMode = "warp_tunnel_only"
oneDotOne ServiceMode = "1dot1"
warp ServiceMode = "warp"
proxy ServiceMode = "proxy"
postureOnly ServiceMode = "posture_only"
warpTunnelOnly ServiceMode = "warp_tunnel_only"
listDeviceSettingsPoliciesDefaultPageSize = 20
)

type ServiceModeV2 struct {
Expand Down Expand Up @@ -85,6 +86,12 @@ type DeviceSettingsPolicyRequest struct {
Description *string `json:"description,omitempty"`
}

type ListDeviceSettingsPoliciesResponse struct {
Response
ResultInfo ResultInfo `json:"result_info"`
Result []DeviceSettingsPolicy `json:"result"`
}

// UpdateDeviceClientCertificates controls the zero trust zone used to provision client certificates.
//
// API reference: https://api.cloudflare.com/#device-client-certificates
Expand Down Expand Up @@ -236,3 +243,44 @@ func (api *API) GetDeviceSettingsPolicy(ctx context.Context, accountID, policyID

return result, err
}

type ListDeviceSettingsPoliciesParams struct {
ResultInfo
}

// ListDeviceSettingsPolicies returns all device settings policies for an account
//
// API reference: https://api.cloudflare.com/#devices-list-device-settings-policies
func (api *API) ListDeviceSettingsPolicies(ctx context.Context, accountID string, params ListDeviceSettingsPoliciesParams) ([]DeviceSettingsPolicy, *ResultInfo, error) {

autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}

if params.PerPage < 1 {
params.PerPage = listDeviceSettingsPoliciesDefaultPageSize
}

var policies []DeviceSettingsPolicy
var lastResultInfo ResultInfo
for {
uri := buildURI(fmt.Sprintf("/%s/%s/devices/policies", AccountRouteRoot, accountID), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
var r ListDeviceSettingsPoliciesResponse
err = json.Unmarshal(res, &r)
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
policies = append(policies, r.Result...)
lastResultInfo = r.ResultInfo
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
return policies, &lastResultInfo, nil
}
44 changes: 44 additions & 0 deletions devices_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,47 @@ func TestGetDeviceSettings(t *testing.T) {
assert.Equal(t, want, actual)
}
}

func TestListDeviceSettingsPolicies(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": null,
"messages": null,
"result": [%s],
"result_info": {
"count": 1,
"page": 1,
"per_page": 20,
"total_count": 1
}
}`, nonDefaultDeviceSettingsPolicyJson)
}

want := []DeviceSettingsPolicy{nonDefaultDeviceSettingsPolicy}

mux.HandleFunc("/accounts/"+testAccountID+"/devices/policies", handler)

actual, resultInfo, err := client.ListDeviceSettingsPolicies(context.Background(), testAccountID, ListDeviceSettingsPoliciesParams{
ResultInfo: ResultInfo{
Page: 1,
PerPage: 20,
},
})

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
assert.Equal(t, &ResultInfo{
Count: 1,
Page: 1,
PerPage: 20,
Total: 1,
}, resultInfo)
assert.Len(t, actual, 1)
}
}

0 comments on commit 60bff7f

Please sign in to comment.