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

Skip to content

chore: implement fetch all organizations endpoint #13941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 26 additions & 0 deletions coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions coderd/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ func (c *Client) OrganizationByName(ctx context.Context, name string) (Organizat
return organization, json.NewDecoder(res.Body).Decode(&organization)
}

func (c *Client) Organizations(ctx context.Context) ([]Organization, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/organizations", nil)
if err != nil {
return []Organization{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return []Organization{}, ReadBodyAsError(res)
}

var organizations []Organization
return organizations, json.NewDecoder(res.Body).Decode(&organizations)
}

func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization, error) {
// OrganizationByName uses the exact same endpoint. It accepts a name or uuid.
// We just provide this function for type safety.
Expand Down
56 changes: 56 additions & 0 deletions docs/api/organizations.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class ApiMethods {

getOrganizations = async (): Promise<TypesGen.Organization[]> => {
const response = await this.axios.get<TypesGen.Organization[]>(
"/api/v2/users/me/organizations",
"/api/v2/organizations",
);
return response.data;
};
Expand Down
17 changes: 13 additions & 4 deletions site/src/api/queries/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
CreateOrganizationRequest,
UpdateOrganizationRequest,
} from "api/typesGenerated";
import { meKey, myOrganizationsKey } from "./users";
import { meKey } from "./users";

export const createOrganization = (queryClient: QueryClient) => {
return {
Expand All @@ -13,7 +13,7 @@ export const createOrganization = (queryClient: QueryClient) => {

onSuccess: async () => {
await queryClient.invalidateQueries(meKey);
await queryClient.invalidateQueries(myOrganizationsKey);
await queryClient.invalidateQueries(organizationsKey);
},
};
};
Expand All @@ -29,7 +29,7 @@ export const updateOrganization = (queryClient: QueryClient) => {
API.updateOrganization(variables.orgId, variables.req),

onSuccess: async () => {
await queryClient.invalidateQueries(myOrganizationsKey);
await queryClient.invalidateQueries(organizationsKey);
},
};
};
Expand All @@ -40,7 +40,7 @@ export const deleteOrganization = (queryClient: QueryClient) => {

onSuccess: async () => {
await queryClient.invalidateQueries(meKey);
await queryClient.invalidateQueries(myOrganizationsKey);
await queryClient.invalidateQueries(organizationsKey);
},
};
};
Expand Down Expand Up @@ -78,3 +78,12 @@ export const removeOrganizationMember = (
},
};
};

export const organizationsKey = ["organizations", "me"] as const;

export const organizations = () => {
return {
queryKey: organizationsKey,
queryFn: () => API.getOrganizations(),
};
};
9 changes: 0 additions & 9 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,3 @@ export const updateAppearanceSettings = (
},
};
};

export const myOrganizationsKey = ["organizations", "me"] as const;

export const myOrganizations = () => {
return {
queryKey: myOrganizationsKey,
queryFn: () => API.getOrganizations(),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createContext, type FC, Suspense, useContext } from "react";
import { useQuery } from "react-query";
import { Outlet, useLocation, useParams } from "react-router-dom";
import { deploymentConfig } from "api/queries/deployment";
import { myOrganizations } from "api/queries/users";
import { organizations } from "api/queries/organizations";
import type { Organization } from "api/typesGenerated";
import { Loader } from "components/Loader/Loader";
import { Margins } from "components/Margins/Margins";
Expand Down Expand Up @@ -39,7 +39,7 @@ export const ManagementSettingsLayout: FC = () => {
const { experiments } = useDashboard();
const { organization } = useParams() as { organization: string };
const deploymentConfigQuery = useQuery(deploymentConfig());
const organizationsQuery = useQuery(myOrganizations());
const organizationsQuery = useQuery(organizations());

const multiOrgExperimentEnabled = experiments.includes("multi-organization");

Expand Down
Loading