account_roles.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. // AccountRole defines the roles that a member can have attached.
  8. type AccountRole struct {
  9. ID string `json:"id"`
  10. Name string `json:"name"`
  11. Description string `json:"description"`
  12. Permissions map[string]AccountRolePermission `json:"permissions"`
  13. }
  14. // AccountRolePermission is the shared structure for all permissions
  15. // that can be assigned to a member.
  16. type AccountRolePermission struct {
  17. Read bool `json:"read"`
  18. Edit bool `json:"edit"`
  19. }
  20. // AccountRolesListResponse represents the list response from the
  21. // account roles.
  22. type AccountRolesListResponse struct {
  23. Result []AccountRole `json:"result"`
  24. Response
  25. ResultInfo `json:"result_info"`
  26. }
  27. // AccountRoleDetailResponse is the API response, containing a single
  28. // account role.
  29. type AccountRoleDetailResponse struct {
  30. Success bool `json:"success"`
  31. Errors []string `json:"errors"`
  32. Messages []string `json:"messages"`
  33. Result AccountRole `json:"result"`
  34. }
  35. // AccountRoles returns all roles of an account.
  36. //
  37. // API reference: https://api.cloudflare.com/#account-roles-list-roles
  38. func (api *API) AccountRoles(accountID string) ([]AccountRole, error) {
  39. uri := "/accounts/" + accountID + "/roles"
  40. res, err := api.makeRequest("GET", uri, nil)
  41. if err != nil {
  42. return []AccountRole{}, errors.Wrap(err, errMakeRequestError)
  43. }
  44. var accountRolesListResponse AccountRolesListResponse
  45. err = json.Unmarshal(res, &accountRolesListResponse)
  46. if err != nil {
  47. return []AccountRole{}, errors.Wrap(err, errUnmarshalError)
  48. }
  49. return accountRolesListResponse.Result, nil
  50. }
  51. // AccountRole returns the details of a single account role.
  52. //
  53. // API reference: https://api.cloudflare.com/#account-roles-role-details
  54. func (api *API) AccountRole(accountID string, roleID string) (AccountRole, error) {
  55. uri := fmt.Sprintf("/accounts/%s/roles/%s", accountID, roleID)
  56. res, err := api.makeRequest("GET", uri, nil)
  57. if err != nil {
  58. return AccountRole{}, errors.Wrap(err, errMakeRequestError)
  59. }
  60. var accountRole AccountRoleDetailResponse
  61. err = json.Unmarshal(res, &accountRole)
  62. if err != nil {
  63. return AccountRole{}, errors.Wrap(err, errUnmarshalError)
  64. }
  65. return accountRole.Result, nil
  66. }