Skip to content

Commit 9d186a5

Browse files
gliptakjamesog
authored andcommitted
Implement ListOrganizations
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
1 parent e3a3c00 commit 9d186a5

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

organizations.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package cloudflare
22

3+
import (
4+
"encoding/json"
5+
6+
"github.com/pkg/errors"
7+
)
8+
39
// Organization represents a multi-user organization.
410
type Organization struct {
511
ID string `json:"id,omitempty"`
@@ -9,9 +15,28 @@ type Organization struct {
915
Roles []string `json:"roles,omitempty"`
1016
}
1117

12-
// OrganizationResponse represents the response from the Organization endpoint.
13-
type OrganizationResponse struct {
18+
// organizationResponse represents the response from the Organization endpoint.
19+
type organizationResponse struct {
1420
Response
1521
Result []Organization `json:"result"`
16-
ResultInfo ResultInfo `json:"result_info"`
22+
ResultInfo `json:"result_info"`
23+
}
24+
25+
// ListOrganizations lists organizations of the logged-in user.
26+
// API reference:
27+
// https://api.cloudflare.com/#user-s-organizations-list-organizations
28+
// GET /user/organizations
29+
func (api *API) ListOrganizations() ([]Organization, ResultInfo, error) {
30+
var r organizationResponse
31+
res, err := api.makeRequest("GET", "/user/organizations", nil)
32+
if err != nil {
33+
return []Organization{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
34+
}
35+
36+
err = json.Unmarshal(res, &r)
37+
if err != nil {
38+
return []Organization{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
39+
}
40+
41+
return r.Result, r.ResultInfo, nil
1742
}

organizations_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cloudflare
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestOrganizations_ListOrganizations(t *testing.T) {
12+
setup()
13+
defer teardown()
14+
15+
mux.HandleFunc("/user/organizations", func(w http.ResponseWriter, r *http.Request) {
16+
assert.Equal(t, "GET", r.Method, "Expected method 'GET', got %s", r.Method)
17+
18+
w.Header().Set("content-type", "application/json")
19+
fmt.Fprintf(w, `{
20+
"success": true,
21+
"errors": [],
22+
"messages": [],
23+
"result": [
24+
{
25+
"id": "01a7362d577a6c3019a474fd6f485823",
26+
"name": "Cloudflare, Inc.",
27+
"status": "member",
28+
"permissions": [
29+
"#zones:read"
30+
],
31+
"roles": [
32+
"All Privileges - Super Administrator"
33+
]
34+
}
35+
],
36+
"result_info": {
37+
"page": 1,
38+
"per_page": 20,
39+
"count": 1,
40+
"total_count": 2000
41+
}
42+
}`)
43+
})
44+
45+
user, paginator, err := client.ListOrganizations()
46+
47+
want := []Organization{{
48+
ID: "01a7362d577a6c3019a474fd6f485823",
49+
Name: "Cloudflare, Inc.",
50+
Status: "member",
51+
Permissions: []string{"#zones:read"},
52+
Roles: []string{"All Privileges - Super Administrator"},
53+
}}
54+
55+
if assert.NoError(t, err) {
56+
assert.Equal(t, user, want)
57+
}
58+
59+
want_pagination := ResultInfo{
60+
Page: 1,
61+
PerPage: 20,
62+
Count: 1,
63+
Total: 2000,
64+
}
65+
assert.Equal(t, paginator, want_pagination)
66+
}

0 commit comments

Comments
 (0)