account_members.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "github.com/pkg/errors"
  8. )
  9. // AccountMember is the definition of a member of an account.
  10. type AccountMember struct {
  11. ID string `json:"id"`
  12. Code string `json:"code"`
  13. User AccountMemberUserDetails `json:"user"`
  14. Status string `json:"status"`
  15. Roles []AccountRole `json:"roles"`
  16. }
  17. // AccountMemberUserDetails outlines all the personal information about
  18. // a member.
  19. type AccountMemberUserDetails struct {
  20. ID string `json:"id"`
  21. FirstName string `json:"first_name"`
  22. LastName string `json:"last_name"`
  23. Email string `json:"email"`
  24. TwoFactorAuthenticationEnabled bool
  25. }
  26. // AccountMembersListResponse represents the response from the list
  27. // account members endpoint.
  28. type AccountMembersListResponse struct {
  29. Result []AccountMember `json:"result"`
  30. Response
  31. ResultInfo `json:"result_info"`
  32. }
  33. // AccountMemberDetailResponse is the API response, containing a single
  34. // account member.
  35. type AccountMemberDetailResponse struct {
  36. Success bool `json:"success"`
  37. Errors []string `json:"errors"`
  38. Messages []string `json:"messages"`
  39. Result AccountMember `json:"result"`
  40. }
  41. // AccountMemberInvitation represents the invitation for a new member to
  42. // the account.
  43. type AccountMemberInvitation struct {
  44. Email string `json:"email"`
  45. Roles []string `json:"roles"`
  46. }
  47. // AccountMembers returns all members of an account.
  48. //
  49. // API reference: https://api.cloudflare.com/#accounts-list-accounts
  50. func (api *API) AccountMembers(accountID string, pageOpts PaginationOptions) ([]AccountMember, ResultInfo, error) {
  51. if accountID == "" {
  52. return []AccountMember{}, ResultInfo{}, errors.New(errMissingAccountID)
  53. }
  54. v := url.Values{}
  55. if pageOpts.PerPage > 0 {
  56. v.Set("per_page", strconv.Itoa(pageOpts.PerPage))
  57. }
  58. if pageOpts.Page > 0 {
  59. v.Set("page", strconv.Itoa(pageOpts.Page))
  60. }
  61. uri := "/accounts/" + accountID + "/members"
  62. if len(v) > 0 {
  63. uri = uri + "?" + v.Encode()
  64. }
  65. res, err := api.makeRequest("GET", uri, nil)
  66. if err != nil {
  67. return []AccountMember{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
  68. }
  69. var accountMemberListresponse AccountMembersListResponse
  70. err = json.Unmarshal(res, &accountMemberListresponse)
  71. if err != nil {
  72. return []AccountMember{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
  73. }
  74. return accountMemberListresponse.Result, accountMemberListresponse.ResultInfo, nil
  75. }
  76. // CreateAccountMember invites a new member to join an account.
  77. //
  78. // API reference: https://api.cloudflare.com/#account-members-add-member
  79. func (api *API) CreateAccountMember(accountID string, emailAddress string, roles []string) (AccountMember, error) {
  80. if accountID == "" {
  81. return AccountMember{}, errors.New(errMissingAccountID)
  82. }
  83. uri := "/accounts/" + accountID + "/members"
  84. var newMember = AccountMemberInvitation{
  85. Email: emailAddress,
  86. Roles: roles,
  87. }
  88. res, err := api.makeRequest("POST", uri, newMember)
  89. if err != nil {
  90. return AccountMember{}, errors.Wrap(err, errMakeRequestError)
  91. }
  92. var accountMemberListResponse AccountMemberDetailResponse
  93. err = json.Unmarshal(res, &accountMemberListResponse)
  94. if err != nil {
  95. return AccountMember{}, errors.Wrap(err, errUnmarshalError)
  96. }
  97. return accountMemberListResponse.Result, nil
  98. }
  99. // DeleteAccountMember removes a member from an account.
  100. //
  101. // API reference: https://api.cloudflare.com/#account-members-remove-member
  102. func (api *API) DeleteAccountMember(accountID string, userID string) error {
  103. if accountID == "" {
  104. return errors.New(errMissingAccountID)
  105. }
  106. uri := fmt.Sprintf("/accounts/%s/members/%s", accountID, userID)
  107. _, err := api.makeRequest("DELETE", uri, nil)
  108. if err != nil {
  109. return errors.Wrap(err, errMakeRequestError)
  110. }
  111. return nil
  112. }
  113. // UpdateAccountMember modifies an existing account member.
  114. //
  115. // API reference: https://api.cloudflare.com/#account-members-update-member
  116. func (api *API) UpdateAccountMember(accountID string, userID string, member AccountMember) (AccountMember, error) {
  117. if accountID == "" {
  118. return AccountMember{}, errors.New(errMissingAccountID)
  119. }
  120. uri := fmt.Sprintf("/accounts/%s/members/%s", accountID, userID)
  121. res, err := api.makeRequest("PUT", uri, member)
  122. if err != nil {
  123. return AccountMember{}, errors.Wrap(err, errMakeRequestError)
  124. }
  125. var accountMemberListResponse AccountMemberDetailResponse
  126. err = json.Unmarshal(res, &accountMemberListResponse)
  127. if err != nil {
  128. return AccountMember{}, errors.Wrap(err, errUnmarshalError)
  129. }
  130. return accountMemberListResponse.Result, nil
  131. }
  132. // AccountMember returns details of a single account member.
  133. //
  134. // API reference: https://api.cloudflare.com/#account-members-member-details
  135. func (api *API) AccountMember(accountID string, memberID string) (AccountMember, error) {
  136. if accountID == "" {
  137. return AccountMember{}, errors.New(errMissingAccountID)
  138. }
  139. uri := fmt.Sprintf(
  140. "/accounts/%s/members/%s",
  141. accountID,
  142. memberID,
  143. )
  144. res, err := api.makeRequest("GET", uri, nil)
  145. if err != nil {
  146. return AccountMember{}, errors.Wrap(err, errMakeRequestError)
  147. }
  148. var accountMemberResponse AccountMemberDetailResponse
  149. err = json.Unmarshal(res, &accountMemberResponse)
  150. if err != nil {
  151. return AccountMember{}, errors.Wrap(err, errUnmarshalError)
  152. }
  153. return accountMemberResponse.Result, nil
  154. }