Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adc590d

Browse files
committed
chore: implement fetch all organizations endpoint
1 parent 6f20a64 commit adc590d

File tree

7 files changed

+159
-8
lines changed

7 files changed

+159
-8
lines changed

coderd/apidoc/docs.go

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ func New(options *Options) *API {
865865
apiKeyMiddleware,
866866
)
867867
r.Post("/", api.postOrganizations)
868+
r.Get("/", api.getOrganizations)
868869
r.Route("/{organization}", func(r chi.Router) {
869870
r.Use(
870871
httpmw.ExtractOrganizationParam(options.Database),

coderd/organizations.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,40 @@ import (
1111

1212
"github.com/coder/coder/v2/coderd/audit"
1313
"github.com/coder/coder/v2/coderd/database"
14+
"github.com/coder/coder/v2/coderd/database/db2sdk"
1415
"github.com/coder/coder/v2/coderd/database/dbtime"
1516
"github.com/coder/coder/v2/coderd/httpapi"
1617
"github.com/coder/coder/v2/coderd/httpmw"
1718
"github.com/coder/coder/v2/codersdk"
1819
)
1920

2021
// @Summary Get organization by ID
21-
// @ID get-organization-by-id
22+
// @ID get-organizations
23+
// @Security CoderSessionToken
24+
// @Produce json
25+
// @Tags Organizations
26+
// @Success 200 {object} []codersdk.Organization
27+
// @Router /organizations [get]
28+
func (api *API) getOrganizations(rw http.ResponseWriter, r *http.Request) {
29+
ctx := r.Context()
30+
organizations, err := api.Database.GetOrganizations(ctx)
31+
if httpapi.Is404Error(err) {
32+
httpapi.ResourceNotFound(rw)
33+
return
34+
}
35+
if err != nil {
36+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
37+
Message: "Internal error fetching organizations.",
38+
Detail: err.Error(),
39+
})
40+
return
41+
}
42+
43+
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(organizations, convertOrganization))
44+
}
45+
46+
// @Summary Get organizations
47+
// @ID get-organizations
2248
// @Security CoderSessionToken
2349
// @Produce json
2450
// @Tags Organizations

coderd/organizations_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ func TestMultiOrgFetch(t *testing.T) {
2727
require.NoError(t, err)
2828
}
2929

30-
orgs, err := client.OrganizationsByUser(ctx, codersdk.Me)
30+
myOrgs, err := client.OrganizationsByUser(ctx, codersdk.Me)
31+
require.NoError(t, err)
32+
require.NotNil(t, myOrgs)
33+
require.Len(t, myOrgs, len(makeOrgs)+1)
34+
35+
orgs, err := client.Organizations(ctx)
3136
require.NoError(t, err)
3237
require.NotNil(t, orgs)
33-
require.Len(t, orgs, len(makeOrgs)+1)
38+
require.ElementsMatch(t, myOrgs, orgs)
3439
}
3540

3641
func TestOrganizationsByUser(t *testing.T) {

codersdk/organizations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ func (c *Client) OrganizationByName(ctx context.Context, name string) (Organizat
215215
return organization, json.NewDecoder(res.Body).Decode(&organization)
216216
}
217217

218+
func (c *Client) Organizations(ctx context.Context) ([]Organization, error) {
219+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/organizations", nil)
220+
if err != nil {
221+
return []Organization{}, xerrors.Errorf("execute request: %w", err)
222+
}
223+
defer res.Body.Close()
224+
225+
if res.StatusCode != http.StatusOK {
226+
return []Organization{}, ReadBodyAsError(res)
227+
}
228+
229+
var organizations []Organization
230+
return organizations, json.NewDecoder(res.Body).Decode(&organizations)
231+
}
232+
218233
func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization, error) {
219234
// OrganizationByName uses the exact same endpoint. It accepts a name or uuid.
220235
// We just provide this function for type safety.

docs/api/organizations.md

Lines changed: 57 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)