Skip to content

Commit a1acc10

Browse files
authored
Merge pull request #81 from planetscale/iheanyi/add-organization-regions
Add ListRegions to OrganizationsService.
2 parents 900e4f3 + 447be65 commit a1acc10

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Diff for: planetscale/organizations.go

+25
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ type GetOrganizationRequest struct {
2222
type OrganizationsService interface {
2323
Get(context.Context, *GetOrganizationRequest) (*Organization, error)
2424
List(context.Context) ([]*Organization, error)
25+
ListRegions(context.Context, *ListOrganizationRegionsRequest) ([]*Region, error)
26+
}
27+
28+
// ListRegionsRequest encapsulates the request for getting a list of regions for
29+
// an organization.
30+
type ListOrganizationRegionsRequest struct {
31+
Organization string
2532
}
2633

2734
// Organization represents a PlanetScale organization.
@@ -76,3 +83,21 @@ func (o *organizationsService) List(ctx context.Context) ([]*Organization, error
7683

7784
return orgResponse.Organizations, nil
7885
}
86+
87+
type listRegionsResponse struct {
88+
Regions []*Region `json:"data"`
89+
}
90+
91+
func (o *organizationsService) ListRegions(ctx context.Context, listReq *ListOrganizationRegionsRequest) ([]*Region, error) {
92+
req, err := o.client.newRequest(http.MethodGet, fmt.Sprintf("%s/%s/regions", organizationsAPIPath, listReq.Organization), nil)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
listResponse := &listRegionsResponse{}
98+
if err := o.client.do(ctx, req, &listResponse); err != nil {
99+
return nil, err
100+
}
101+
102+
return listResponse.Regions, nil
103+
}

Diff for: planetscale/organizations_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,45 @@ func TestOrganizations_Get(t *testing.T) {
8585

8686
c.Assert(org, qt.DeepEquals, want)
8787
}
88+
89+
func TestOrganizations_ListRegions(t *testing.T) {
90+
c := qt.New(t)
91+
92+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
93+
w.WriteHeader(200)
94+
out := `{
95+
"data": [
96+
{
97+
"id": "my-cool-org",
98+
"type": "Region",
99+
"slug": "us-east",
100+
"display_name": "US East",
101+
"enabled": true
102+
}
103+
]
104+
}`
105+
106+
_, err := w.Write([]byte(out))
107+
c.Assert(err, qt.IsNil)
108+
}))
109+
110+
client, err := NewClient(WithBaseURL(ts.URL))
111+
c.Assert(err, qt.IsNil)
112+
113+
ctx := context.Background()
114+
115+
orgs, err := client.Organizations.ListRegions(ctx, &ListOrganizationRegionsRequest{
116+
Organization: "my-cool-org",
117+
})
118+
119+
c.Assert(err, qt.IsNil)
120+
want := []*Region{
121+
{
122+
Name: "US East",
123+
Slug: "us-east",
124+
Enabled: true,
125+
},
126+
}
127+
128+
c.Assert(orgs, qt.DeepEquals, want)
129+
}

0 commit comments

Comments
 (0)