Skip to content

Commit

Permalink
Implement ListOrganizations
Browse files Browse the repository at this point in the history
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
  • Loading branch information
gliptak authored and jamesog committed Jan 16, 2017
1 parent e3a3c00 commit 9d186a5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
31 changes: 28 additions & 3 deletions organizations.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package cloudflare

import (
"encoding/json"

"github.com/pkg/errors"
)

// Organization represents a multi-user organization.
type Organization struct {
ID string `json:"id,omitempty"`
Expand All @@ -9,9 +15,28 @@ type Organization struct {
Roles []string `json:"roles,omitempty"`
}

// OrganizationResponse represents the response from the Organization endpoint.
type OrganizationResponse struct {
// organizationResponse represents the response from the Organization endpoint.
type organizationResponse struct {
Response
Result []Organization `json:"result"`
ResultInfo ResultInfo `json:"result_info"`
ResultInfo `json:"result_info"`
}

// ListOrganizations lists organizations of the logged-in user.
// API reference:
// https://api.cloudflare.com/#user-s-organizations-list-organizations
// GET /user/organizations
func (api *API) ListOrganizations() ([]Organization, ResultInfo, error) {
var r organizationResponse
res, err := api.makeRequest("GET", "/user/organizations", nil)
if err != nil {
return []Organization{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
}

err = json.Unmarshal(res, &r)
if err != nil {
return []Organization{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
}

return r.Result, r.ResultInfo, nil
}
66 changes: 66 additions & 0 deletions organizations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cloudflare

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

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

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

mux.HandleFunc("/user/organizations", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", 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": "01a7362d577a6c3019a474fd6f485823",
"name": "Cloudflare, Inc.",
"status": "member",
"permissions": [
"#zones:read"
],
"roles": [
"All Privileges - Super Administrator"
]
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 2000
}
}`)
})

user, paginator, err := client.ListOrganizations()

want := []Organization{{
ID: "01a7362d577a6c3019a474fd6f485823",
Name: "Cloudflare, Inc.",
Status: "member",
Permissions: []string{"#zones:read"},
Roles: []string{"All Privileges - Super Administrator"},
}}

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

want_pagination := ResultInfo{
Page: 1,
PerPage: 20,
Count: 1,
Total: 2000,
}
assert.Equal(t, paginator, want_pagination)
}

0 comments on commit 9d186a5

Please sign in to comment.