mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
chore: implement fetch all organizations endpoint (#13941)
* chore: implement fetch all organizations endpoint * update ui to use list all orgs
This commit is contained in:
26
coderd/apidoc/docs.go
generated
26
coderd/apidoc/docs.go
generated
@ -2037,6 +2037,32 @@ const docTemplate = `{
|
||||
}
|
||||
},
|
||||
"/organizations": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"CoderSessionToken": []
|
||||
}
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Organizations"
|
||||
],
|
||||
"summary": "Get organizations",
|
||||
"operationId": "get-organizations",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/codersdk.Organization"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
|
22
coderd/apidoc/swagger.json
generated
22
coderd/apidoc/swagger.json
generated
@ -1779,6 +1779,28 @@
|
||||
}
|
||||
},
|
||||
"/organizations": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"CoderSessionToken": []
|
||||
}
|
||||
],
|
||||
"produces": ["application/json"],
|
||||
"tags": ["Organizations"],
|
||||
"summary": "Get organizations",
|
||||
"operationId": "get-organizations",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/codersdk.Organization"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
|
@ -865,6 +865,7 @@ func New(options *Options) *API {
|
||||
apiKeyMiddleware,
|
||||
)
|
||||
r.Post("/", api.postOrganizations)
|
||||
r.Get("/", api.organizations)
|
||||
r.Route("/{organization}", func(r chi.Router) {
|
||||
r.Use(
|
||||
httpmw.ExtractOrganizationParam(options.Database),
|
||||
|
@ -11,12 +11,38 @@ import (
|
||||
|
||||
"github.com/coder/coder/v2/coderd/audit"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/coderd/httpmw"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
// @Summary Get organizations
|
||||
// @ID get-organizations
|
||||
// @Security CoderSessionToken
|
||||
// @Produce json
|
||||
// @Tags Organizations
|
||||
// @Success 200 {object} []codersdk.Organization
|
||||
// @Router /organizations [get]
|
||||
func (api *API) organizations(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
organizations, err := api.Database.GetOrganizations(ctx)
|
||||
if httpapi.Is404Error(err) {
|
||||
httpapi.ResourceNotFound(rw)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Internal error fetching organizations.",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(organizations, convertOrganization))
|
||||
}
|
||||
|
||||
// @Summary Get organization by ID
|
||||
// @ID get-organization-by-id
|
||||
// @Security CoderSessionToken
|
||||
|
@ -27,10 +27,15 @@ func TestMultiOrgFetch(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
orgs, err := client.OrganizationsByUser(ctx, codersdk.Me)
|
||||
myOrgs, err := client.OrganizationsByUser(ctx, codersdk.Me)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, myOrgs)
|
||||
require.Len(t, myOrgs, len(makeOrgs)+1)
|
||||
|
||||
orgs, err := client.Organizations(ctx)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, orgs)
|
||||
require.Len(t, orgs, len(makeOrgs)+1)
|
||||
require.ElementsMatch(t, myOrgs, orgs)
|
||||
}
|
||||
|
||||
func TestOrganizationsByUser(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user