Skip to content

feat: add support for template in workspace filter #2134

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

Closed
wants to merge 3 commits into from
Closed
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
Next Next commit
feat: add support for template in workspace filter
  • Loading branch information
f0ssel committed Jun 8, 2022
commit 1a14a8c71a2686df14ea0e042e6b304a82469b9d
20 changes: 20 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,26 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
return sql.ErrNoRows
}

func (q *fakeQuerier) GetTemplatesByName(_ context.Context, arg database.GetTemplatesByNameParams) ([]database.Template, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
var templates []database.Template
for _, template := range q.templates {
if !strings.EqualFold(template.Name, arg.Name) {
continue
}
if template.Deleted != arg.Deleted {
continue
}
templates = append(templates, template)
}
if len(templates) > 0 {
return templates, nil
}

return nil, sql.ErrNoRows
}

func (q *fakeQuerier) GetTemplateVersionsByTemplateID(_ context.Context, arg database.GetTemplateVersionsByTemplateIDParams) (version []database.TemplateVersion, err error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

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

62 changes: 60 additions & 2 deletions coderd/database/queries.sql.go

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

9 changes: 9 additions & 0 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ WHERE
LIMIT
1;

-- name: GetTemplatesByName :many
SELECT
*
FROM
templates
WHERE
deleted = @deleted
AND LOWER("name") = LOWER(@name);

-- name: GetTemplatesByOrganization :many
SELECT
*
Expand Down
6 changes: 6 additions & 0 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ WHERE
owner_id = @owner_id
ELSE true
END
-- Filter by template_id
AND CASE
WHEN @template_id :: uuid != '00000000-00000000-00000000-00000000' THEN
template_id = @template_id
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN @name :: text != '' THEN
Expand Down
58 changes: 33 additions & 25 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,48 +103,56 @@ func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
// Optional filters with query params
func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
filter := database.GetWorkspacesWithFilterParams{Deleted: false}

// Empty strings mean no filter
orgFilter := r.URL.Query().Get("organization_id")
ownerFilter := r.URL.Query().Get("owner")
nameFilter := r.URL.Query().Get("name")

filter := database.GetWorkspacesWithFilterParams{Deleted: false}
if orgFilter != "" {
orgID, err := uuid.Parse(orgFilter)
if err == nil {
filter.OrganizationID = orgID
}
}

ownerFilter := r.URL.Query().Get("owner")
if ownerFilter == "me" {
filter.OwnerID = apiKey.UserID
} else if ownerFilter != "" {
userID, err := uuid.Parse(ownerFilter)
if err != nil {
// Maybe it's a username
user, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
// Why not just accept 1 arg and use it for both in the sql?
Username: ownerFilter,
Email: ownerFilter,
})
if err == nil {
filter.OwnerID = user.ID
}
} else {
filter.OwnerID = userID
user, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
Username: ownerFilter,
})
if err == nil {
filter.OwnerID = user.ID
}
}

templateFilter := r.URL.Query().Get("template")
var templates []database.Template
if templateFilter != "" {
ts, err := api.Database.GetTemplatesByName(r.Context(), database.GetTemplatesByNameParams{
Name: templateFilter,
})
if err == nil {
templates = ts
}
}

nameFilter := r.URL.Query().Get("name")
if nameFilter != "" {
filter.Name = nameFilter
}

workspaces, err := api.Database.GetWorkspacesWithFilter(r.Context(), filter)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching workspaces.",
Detail: err.Error(),
})
return
var workspaces []database.Workspace
for _, template := range templates {
filter.TemplateID = template.ID
ws, err := api.Database.GetWorkspacesWithFilter(r.Context(), filter)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching workspaces.",
Detail: err.Error(),
})
return
}
workspaces = append(workspaces, ws...)
}

// Only return workspaces the user can read
Expand Down
7 changes: 5 additions & 2 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,12 @@ func (c *Client) PutExtendWorkspace(ctx context.Context, id uuid.UUID, req PutEx

type WorkspaceFilter struct {
OrganizationID uuid.UUID `json:"organization_id,omitempty"`
// Owner can be a user_id (uuid), "me", or a username
// Owner can be "me" or a username
Owner string `json:"owner,omitempty"`
Name string `json:"name,omitempty"`
// Template is a template name
Template string `json:"template,omitempty"`
// Name will return partial matches
Name string `json:"name,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).Request.
Expand Down