Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update account roles #1405

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changelog/1405.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
account_role: autopaginate all available results instead of a static number
```

```release-note:breaking-change
account_role: `AccountRoles` has been renamed to `ListAccountRoles` to align with the updated method conventions
```

```release-note:breaking-change
account_role: `AccountRole` has been renamed to `GetAccountRole` to align with the updated method conventions
```
64 changes: 47 additions & 17 deletions account_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,65 @@ type AccountRoleDetailResponse struct {
Result AccountRole `json:"result"`
}

// AccountRoles returns all roles of an account.
type ListAccountRolesParams struct {
ResultInfo
}

// ListAccountRoles returns all roles of an account.
//
// API reference: https://api.cloudflare.com/#account-roles-list-roles
func (api *API) AccountRoles(ctx context.Context, accountID string) ([]AccountRole, error) {
uri := fmt.Sprintf("/accounts/%s/roles?per_page=50", accountID)
// API reference: https://developers.cloudflare.com/api/operations/account-roles-list-roles
func (api *API) ListAccountRoles(ctx context.Context, rc *ResourceContainer, params ListAccountRolesParams) ([]AccountRole, error) {
if rc.Identifier == "" {
return []AccountRole{}, ErrMissingAccountID
}
autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccountRole{}, err
if params.PerPage < 1 {
params.PerPage = 25
}

var accountRolesListResponse AccountRolesListResponse
err = json.Unmarshal(res, &accountRolesListResponse)
if err != nil {
return []AccountRole{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
if params.Page < 1 {
params.Page = 1
}
var roles []AccountRole
var r AccountRolesListResponse
for {
uri := buildURI(fmt.Sprintf("/accounts/%s/roles", rc.Identifier), params)

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

return accountRolesListResponse.Result, nil
err = json.Unmarshal(res, &r)
if err != nil {
return []AccountRole{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
roles = append(roles, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}

return roles, nil
}

// AccountRole returns the details of a single account role.
// GetAccountRole returns the details of a single account role.
//
// API reference: https://api.cloudflare.com/#account-roles-role-details
func (api *API) AccountRole(ctx context.Context, accountID string, roleID string) (AccountRole, error) {
uri := fmt.Sprintf("/accounts/%s/roles/%s", accountID, roleID)
// API reference: https://developers.cloudflare.com/api/operations/account-roles-role-details
func (api *API) GetAccountRole(ctx context.Context, rc *ResourceContainer, roleID string) (AccountRole, error) {
if rc.Identifier == "" {
return AccountRole{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/roles/%s", rc.Identifier, roleID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccountRole{}, err
return AccountRole{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

var accountRole AccountRoleDetailResponse
Expand Down
16 changes: 12 additions & 4 deletions account_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ func TestAccountRoles(t *testing.T) {
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/roles", handler)
mux.HandleFunc(fmt.Sprintf("/accounts/%s/roles", testAccountID), handler)
want := []AccountRole{expectedAccountRole}
_, err := client.ListAccountRoles(context.Background(), AccountIdentifier(""), ListAccountRolesParams{})
if assert.Error(t, err, "Expected error when no account ID is provided") {
assert.Equal(t, ErrMissingAccountID, err)
}

actual, err := client.AccountRoles(context.Background(), "01a7362d577a6c3019a474fd6f485823")
actual, err := client.ListAccountRoles(context.Background(), testAccountRC, ListAccountRolesParams{})

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
Expand Down Expand Up @@ -103,9 +107,13 @@ func TestAccountRole(t *testing.T) {
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/roles/3536bcfad5faccb999b47003c79917fb", handler)
mux.HandleFunc(fmt.Sprintf("/accounts/%s/roles/3536bcfad5faccb999b47003c79917fb", testAccountID), handler)
_, err := client.GetAccountRole(context.Background(), AccountIdentifier(""), "3536bcfad5faccb999b47003c79917fb")
if assert.Error(t, err, "Expected error when no account ID is provided") {
assert.Equal(t, ErrMissingAccountID, err)
}

actual, err := client.AccountRole(context.Background(), "01a7362d577a6c3019a474fd6f485823", "3536bcfad5faccb999b47003c79917fb")
actual, err := client.GetAccountRole(context.Background(), testAccountRC, "3536bcfad5faccb999b47003c79917fb")

if assert.NoError(t, err) {
assert.Equal(t, expectedAccountRole, actual)
Expand Down