Skip to content

feat: implement deprecated flag for templates to prevent new workspaces #10745

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 16 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add deprecated filter to template fetching
  • Loading branch information
Emyrk committed Nov 17, 2023
commit 33f54f87d0573251add4c221d8961e2d1811a9f2
3 changes: 3 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6888,6 +6888,9 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
if arg.ExactName != "" && !strings.EqualFold(template.Name, arg.ExactName) {
continue
}
if arg.Deprecated.Valid && arg.Deprecated.Bool == (template.Deprecated != "") {
continue
}

if len(arg.IDs) > 0 {
match := false
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
arg.OrganizationID,
arg.ExactName,
pq.Array(arg.IDs),
arg.Deprecated,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -87,6 +88,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
&i.AutostopRequirementWeeks,
&i.AutostartBlockDaysOfWeek,
&i.RequireActiveVersion,
&i.Deprecated,
&i.CreatedByAvatarURL,
&i.CreatedByUsername,
); err != nil {
Expand Down
21 changes: 17 additions & 4 deletions coderd/database/queries.sql.go

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

11 changes: 11 additions & 0 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ WHERE
id = ANY(@ids)
ELSE true
END
-- Filter by deprecated
AND CASE
WHEN sqlc.narg('deprecated') :: boolean IS NOT NULL THEN
CASE
WHEN sqlc.narg('deprecated') :: boolean THEN
deprecated != ''
ELSE
deprecated = ''
END
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedTemplates
-- @authorize_filter
ORDER BY (name, id) ASC
Expand Down
19 changes: 19 additions & 0 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,24 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
ctx := r.Context()
organization := httpmw.OrganizationParam(r)

p := httpapi.NewQueryParamParser()
values := r.URL.Query()

deprecated := sql.NullBool{}
if values.Has("deprecated") {
deprecated = sql.NullBool{
Bool: p.Boolean(values, false, "deprecated"),
Valid: true,
}
}
if len(p.Errors) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid query params.",
Validations: p.Errors,
})
return
}

prepared, err := api.HTTPAuth.AuthorizeSQLFilter(r, rbac.ActionRead, rbac.ResourceTemplate.Type)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand All @@ -449,6 +467,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
// Filter templates based on rbac permissions
templates, err := api.Database.GetAuthorizedTemplates(ctx, database.GetTemplatesWithFilterParams{
OrganizationID: organization.ID,
Deprecated: deprecated,
}, prepared)
if errors.Is(err, sql.ErrNoRows) {
err = nil
Expand Down
12 changes: 12 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,21 @@ export const getTemplate = async (

export const getTemplates = async (
organizationId: string,
deprecated?: boolean,
): Promise<TypesGen.Template[]> => {
const params = {} as Record<string,string>
if(deprecated !== undefined) {
// Just want to check if it isn't undefined. If it has
// a boolean value, convert it to a string and include
// it as a param.
params["deprecated"] = String(deprecated)
}

const response = await axios.get<TypesGen.Template[]>(
`/api/v2/organizations/${organizationId}/templates`,
{
params,
}
);
return response.data;
};
Expand Down
8 changes: 4 additions & 4 deletions site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export const templateByName = (
};
};

const getTemplatesQueryKey = (orgId: string) => [orgId, "templates"];
const getTemplatesQueryKey = (orgId: string, deprecated?: boolean) => [orgId, "templates", deprecated];

export const templates = (orgId: string) => {
export const templates = (orgId: string, deprecated?: boolean) => {
return {
queryKey: getTemplatesQueryKey(orgId),
queryFn: () => API.getTemplates(orgId),
queryKey: getTemplatesQueryKey(orgId, deprecated),
queryFn: () => API.getTemplates(orgId, deprecated),
};
};

Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const WorkspacesPage: FC = () => {
const pagination = usePagination({ searchParamsResult });

const organizationId = useOrganizationId();
const templatesQuery = useQuery(templates(organizationId));
const templatesQuery = useQuery(templates(organizationId, false));

const filterProps = useWorkspacesFilter({
searchParamsResult,
Expand Down
6 changes: 4 additions & 2 deletions site/src/pages/WorkspacesPage/filter/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const useTemplateFilterMenu = ({
value,
id: "template",
getSelectedOption: async () => {
const templates = await getTemplates(orgId);
// Show all templates including deprecated
const templates = await getTemplates(orgId, undefined);
const template = templates.find((template) => template.name === value);
if (template) {
return {
Expand All @@ -32,7 +33,8 @@ export const useTemplateFilterMenu = ({
return null;
},
getOptions: async (query) => {
const templates = await getTemplates(orgId);
// Show all templates including deprecated
const templates = await getTemplates(orgId, undefined);
const filteredTemplates = templates.filter(
(template) =>
template.name.toLowerCase().includes(query.toLowerCase()) ||
Expand Down