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 7 commits
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
49 changes: 49 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,41 @@ func (q *fakeQuerier) GetWorkspacesWithFilter(_ context.Context, arg database.Ge
if arg.OwnerID != uuid.Nil && workspace.OwnerID != arg.OwnerID {
continue
}
if arg.OwnerUsername != "" {
owner, err := q.GetUserByID(context.Background(), workspace.OwnerID)
if err == nil && arg.OwnerUsername != owner.Username {
continue
}
}
if arg.TemplateName != "" {
templates, err := q.GetTemplatesByName(context.Background(), database.GetTemplatesByNameParams{
Name: arg.TemplateName,
})
// Add to later param
if err == nil {
for _, t := range templates {
arg.TemplateIds = append(arg.TemplateIds, t.ID)
}
}
}
if !arg.Deleted && workspace.Deleted {
continue
}
if arg.Name != "" && !strings.Contains(workspace.Name, arg.Name) {
continue
}
if len(arg.TemplateIds) > 0 {
match := false
for _, id := range arg.TemplateIds {
if workspace.TemplateID == id {
match = true
break
}
}
if !match {
continue
}
}
workspaces = append(workspaces, workspace)
}

Expand Down Expand Up @@ -761,6 +790,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.

96 changes: 86 additions & 10 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
34 changes: 27 additions & 7 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,15 +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
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
109 changes: 109 additions & 0 deletions coderd/httpapi/queryparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package httpapi

import (
"fmt"
"net/http"
"strconv"
"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) ParseInteger(r *http.Request, def int, queryParam string) int {
v, err := parse(r, strconv.Atoi, def, queryParam)
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q must be a valid integer (%s)", queryParam, err.Error()),
})
}
return v
}

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
}
return p.ParseUUID(r, def, queryParam)
}

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(strings.TrimSpace(s))
if err != nil {
badValues = append(badValues, v)
continue
}
ids = append(ids, id)
}

if len(badValues) > 0 {
return []uuid.UUID{}, 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", queryParam, 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)
}
Loading