Skip to content

feat: workspace filter query supported in backend #2232

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 38 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1a14a8c
feat: add support for template in workspace filter
f0ssel Jun 7, 2022
e3bd559
get working
f0ssel Jun 8, 2022
91d7d84
make gen
f0ssel Jun 8, 2022
2941b58
feat: Implement workspace search filter to support names
Emyrk Jun 10, 2022
70f4304
Use new query param parser for pagination fields
Emyrk Jun 10, 2022
0f7bb21
Fix fake db implementation
Emyrk Jun 10, 2022
5687dbe
Add unit test for parsing query params
Emyrk Jun 10, 2022
18e5247
Fix linting
Emyrk Jun 10, 2022
9a1b182
Fix search
Emyrk Jun 10, 2022
fab5d8c
Maintain old behavior
Emyrk Jun 10, 2022
15754c5
Linting
Emyrk Jun 10, 2022
f3123fe
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
d1c6319
Remove excessive calls, use filters on a single query
Emyrk Jun 10, 2022
e5ec365
Remove unused code
Emyrk Jun 10, 2022
21683d2
Add unit test to keep fake db clean
Emyrk Jun 10, 2022
fc48397
Fix typo
Emyrk Jun 10, 2022
5310164
Drop like name search on template
Emyrk Jun 10, 2022
c64ed18
Fix linting
Emyrk Jun 10, 2022
c6e3a57
Move WorkspaceSearchQuery to workspaces.go
Emyrk Jun 10, 2022
7821aa4
Search all templates with name
Emyrk Jun 10, 2022
d3091f0
Add more complex filter unit test
Emyrk Jun 10, 2022
ebcb831
Fix unit test to not violate pg constraint
Emyrk Jun 10, 2022
9368c48
Drop owner_id from query params
Emyrk Jun 10, 2022
b5f5705
Remove unused code/params
Emyrk Jun 10, 2022
9c9a5e2
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
4512a9b
Remove field from ts
Emyrk Jun 10, 2022
e9e913d
Address PR comments
Emyrk Jun 10, 2022
2aac32b
Fix js test
Emyrk Jun 10, 2022
c8813cc
Fix unit test
Emyrk Jun 10, 2022
ee35935
PR feedback
Emyrk Jun 13, 2022
4ad585e
Rename vague function name
Emyrk Jun 13, 2022
6251493
WorkspaceSearchQuery now returns db filter
Emyrk Jun 13, 2022
ac8dddd
Correct unit test and typescript
Emyrk Jun 13, 2022
727239e
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 13, 2022
92dcbc8
fmt
Emyrk Jun 13, 2022
0a51e02
Use !== over !=
Emyrk Jun 13, 2022
5a1272c
Fix js unit test
Emyrk Jun 13, 2022
5b5039b
Js linting
Emyrk Jun 13, 2022
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
feat: Implement workspace search filter to support names
  • Loading branch information
Emyrk committed Jun 10, 2022
commit 2941b58b4b6c93f285f4a800b604b5f95a70df2d
36 changes: 27 additions & 9 deletions coderd/database/queries.sql.go

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

34 changes: 24 additions & 10 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FROM
workspaces
WHERE
-- Optionally include deleted workspaces
deleted = @deleted
workspaces.deleted = @deleted
-- Filter by organization_id
AND CASE
WHEN @organization_id :: uuid != '00000000-00000000-00000000-00000000' THEN
Expand All @@ -24,21 +24,35 @@ WHERE
END
-- Filter by owner_id
AND CASE
WHEN @owner_id :: uuid != '00000000-00000000-00000000-00000000' THEN
owner_id = @owner_id
ELSE true
WHEN @owner_id :: uuid != '00000000-00000000-00000000-00000000' THEN
owner_id = @owner_id
ELSE true
END
-- Filter by owner_name
AND CASE
WHEN @owner_username :: text != '' THEN
owner_id = (SELECT id FROM users WHERE username = @owner_username)
ELSE true
END
-- Filter by template_name
-- There can be more than 1 template with the same name across organizations.
-- Use the organization filter to restrict to 1 org if needed.
AND CASE
WHEN @template_name :: text != '' THEN
template_id = (SELECT id FROM templates WHERE name = @template_name)
ELSE true
END
-- Filter by template_ids
AND CASE
WHEN array_length(@template_ids :: uuid[], 1) > 0 THEN
template_id = ANY(@template_ids)
ELSE true
WHEN array_length(@template_ids :: uuid[], 1) > 0 THEN
template_id = ANY(@template_ids)
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN @name :: text != '' THEN
LOWER(name) LIKE '%' || LOWER(@name) || '%'
ELSE true
WHEN @name :: text != '' THEN
LOWER(name) LIKE '%' || LOWER(@name) || '%'
ELSE true
END
;

Expand Down
105 changes: 105 additions & 0 deletions coderd/httpapi/queryparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package httpapi

import (
"fmt"
"net/http"
"strings"

"github.com/google/uuid"

"golang.org/x/xerrors"
)

// QueryParamParser is a helper for parsing all query params and gathering all
// errors in 1 sweep. This means all invalid fields are returned at once,
// rather than only returning the first error
type QueryParamParser struct {
errors []Error
}

func NewQueryParamParser() *QueryParamParser {
return &QueryParamParser{
errors: []Error{},
}
}

// ValidationErrors is the set of errors to return via the API. If the length
// of this set is 0, there was no errors.
func (p QueryParamParser) ValidationErrors() []Error {
return p.errors
}

func (p *QueryParamParser) ParseUUIDorMe(r *http.Request, def uuid.UUID, me uuid.UUID, queryParam string) uuid.UUID {
if r.URL.Query().Get(queryParam) == "me" {
return me
}

v, err := parse(r, uuid.Parse, def, queryParam)
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q must be a valid uuid", queryParam),
})
}
return v
}

func (p *QueryParamParser) ParseUUID(r *http.Request, def uuid.UUID, queryParam string) uuid.UUID {
v, err := parse(r, uuid.Parse, def, queryParam)
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q must be a valid uuid", queryParam),
})
}
return v
}

func (p *QueryParamParser) ParseUUIDArray(r *http.Request, def []uuid.UUID, queryParam string) []uuid.UUID {
v, err := parse(r, func(v string) ([]uuid.UUID, error) {
var badValues []string
strs := strings.Split(v, ",")
ids := make([]uuid.UUID, 0, len(strs))
for _, s := range strs {
id, err := uuid.Parse(s)
if err != nil {
badValues = append(badValues, v)
continue
}
ids = append(ids, id)
}

if len(badValues) > 0 {
return nil, xerrors.Errorf("%s", strings.Join(badValues, ","))
}
return ids, nil
}, def, queryParam)
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q has invalid uuids: %q", err.Error()),
})
}
return v
}

func (p *QueryParamParser) ParseString(r *http.Request, def string, queryParam string) string {
v, err := parse(r, func(v string) (string, error) {
return v, nil
}, def, queryParam)
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q must be a valid string", queryParam),
})
}
return v
}

func parse[T any](r *http.Request, parse func(v string) (T, error), def T, queryParam string) (T, error) {
if !r.URL.Query().Has(queryParam) {
return def, nil
}
str := r.URL.Query().Get(queryParam)
return parse(str)
}
64 changes: 64 additions & 0 deletions coderd/httpapi/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package httpapi

import (
"strings"

"golang.org/x/xerrors"
)

// WorkspaceSearchQuery takes a query string and breaks it into it's query
// params as a set of key=value.
func WorkspaceSearchQuery(query string) (map[string]string, error) {
searchParams := make(map[string]string)
elements := queryElements(query)
for _, element := range elements {
parts := strings.Split(element, ":")
switch len(parts) {
case 1:
// No key:value pair. It is a workspace name, and maybe includes an owner
parts = strings.Split(element, "/")
switch len(parts) {
case 1:
searchParams["name"] = parts[0]
case 2:
searchParams["owner"] = parts[0]
searchParams["name"] = parts[1]
default:
return nil, xerrors.Errorf("Query element %q can only contain 1 '/'", element)
}
case 2:
searchParams[parts[0]] = parts[1]
default:
return nil, xerrors.Errorf("Query element %q can only contain 1 ':'", element)
}
}

return searchParams, nil
}

// queryElements takes a query string and splits it into the individual elements
// of the query. Each element is separated by a space. All quoted strings are
// kept as a single element.
func queryElements(query string) []string {
var parts []string

quoted := false
var current strings.Builder
for _, c := range query {
switch c {
case '"':
quoted = !quoted
case ' ':
if quoted {
current.WriteRune(c)
} else {
parts = append(parts, current.String())
current = strings.Builder{}
}
default:
current.WriteRune(c)
}
}
parts = append(parts, current.String())
return parts
}
72 changes: 44 additions & 28 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,59 @@ 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}

orgFilter := r.URL.Query().Get("organization_id")
if orgFilter != "" {
orgID, err := uuid.Parse(orgFilter)
if err == nil {
filter.OrganizationID = orgID
}
queryStr := r.URL.Query().Get("q")
values, err := httpapi.WorkspaceSearchQuery(queryStr)
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "Invalid workspace search query",
Validations: []httpapi.Error{
{Field: "q", Detail: err.Error()},
},
})
return
}

ownerFilter := r.URL.Query().Get("owner")
if ownerFilter == "me" {
filter.OwnerID = apiKey.UserID
} else if ownerFilter != "" {
user, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
Username: ownerFilter,
})
if err == nil {
filter.OwnerID = user.ID
// Set all the query params from the "q" field.
for k, v := range values {
// Do not allow overriding if the user also set query param fields
// outside the query string.
if r.URL.Query().Has(k) {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Workspace filter %q cannot be set twice. In query params %q and %q", k, k, "q"),
})
return
}
r.URL.Query().Set(k, v)
}

nameFilter := r.URL.Query().Get("name")
if nameFilter != "" {
filter.Name = nameFilter
parser := httpapi.NewQueryParamParser()
filter := database.GetWorkspacesWithFilterParams{
Deleted: false,
OrganizationID: parser.ParseUUID(r, uuid.Nil, "organization_id"),
OwnerID: parser.ParseUUIDorMe(r, uuid.Nil, apiKey.UserID, "owner_id"),
OwnerUsername: parser.ParseString(r, "", "owner"),
TemplateName: parser.ParseString(r, "", "template"),
TemplateIds: parser.ParseUUIDArray(r, []uuid.UUID{}, "template_ids"),
Name: parser.ParseString(r, "", "name"),
}

templateFilter := r.URL.Query().Get("template")
if templateFilter != "" {
ts, err := api.Database.GetTemplatesByName(r.Context(), database.GetTemplatesByNameParams{
Name: templateFilter,
if len(parser.ValidationErrors()) > 0 {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Query parameters have invalid values"),
Validations: parser.ValidationErrors(),
})
if err == nil {
for _, t := range ts {
filter.TemplateIds = append(filter.TemplateIds, t.ID)
}
return
}

if filter.OwnerUsername == "me" {
if !(filter.OwnerID == uuid.Nil || filter.OwnerID == apiKey.UserID) {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Cannot set both \"me\" in \"owner_name\" and use \"owner_id\""),
})
return
}
filter.OwnerID = apiKey.UserID
filter.OwnerUsername = ""
}

workspaces, err := api.Database.GetWorkspacesWithFilter(r.Context(), filter)
Expand Down