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
Fix fake db implementation
  • Loading branch information
Emyrk committed Jun 10, 2022
commit 0f7bb21f651d9b7fd70b555f207d6079c4a18b80
17 changes: 17 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ 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
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/httpapi/queryparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *QueryParamParser) ParseUUIDArray(r *http.Request, def []uuid.UUID, quer
if err != nil {
p.errors = append(p.errors, Error{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q has invalid uuids: %q", err.Error()),
Detail: fmt.Sprintf("Query param %q has invalid uuids: %q", queryParam, err.Error()),
})
}
return v
Expand Down
27 changes: 18 additions & 9 deletions coderd/httpapi/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"golang.org/x/xerrors"
)

// WorkspaceSearchQuery takes a query string and breaks it into it's query
// params as a set of key=value.
// WorkspaceSearchQuery takes a query string and breaks it into it's queryparams
// as a set of key=value.
func WorkspaceSearchQuery(query string) (map[string]string, error) {
searchParams := make(map[string]string)
elements := queryElements(query)
if query == "" {
return searchParams, nil
}
elements := splitElements(query, ' ')
for _, element := range elements {
parts := strings.Split(element, ":")
parts := splitElements(query, ':')
switch len(parts) {
case 1:
// No key:value pair. It is a workspace name, and maybe includes an owner
parts = strings.Split(element, "/")
parts = splitElements(query, '/')
switch len(parts) {
case 1:
searchParams["name"] = parts[0]
Expand All @@ -36,10 +39,16 @@ func WorkspaceSearchQuery(query string) (map[string]string, error) {
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
// splitElements takes a query string and splits it into the individual elements
// of the query. Each element is separated by a delimiter. All quoted strings are
// kept as a single element.
func queryElements(query string) []string {
//
// Although all our names cannot have spaces, that is a validation error.
// We should still parse the quoted string as a single value so that validation
// can properly fail on the space. If we do not, a value of `template:"my name"`
// will search `template:"my name:name"`, which produces an empty list instead of
// an error.
func splitElements(query string, delimiter rune) []string {
var parts []string

quoted := false
Expand All @@ -48,7 +57,7 @@ func queryElements(query string) []string {
switch c {
case '"':
quoted = !quoted
case ' ':
case delimiter:
if quoted {
current.WriteRune(c)
} else {
Expand Down
123 changes: 123 additions & 0 deletions coderd/httpapi/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package httpapi_test

import (
"testing"

"github.com/coder/coder/coderd/httpapi"
"github.com/stretchr/testify/require"
)

func TestSearchWorkspace(t *testing.T) {
t.Parallel()
testCases := []struct {
Name string
Query string
Expected map[string]string
ExpectedErrorContains string
}{
{
Name: "Empty",
Query: "",
Expected: map[string]string{},
},
{
Name: "Owner/Name",
Query: "Foo/Bar",
Expected: map[string]string{
"owner": "Foo",
"name": "Bar",
},
},
{
Name: "Name",
Query: "workspace-name",
Expected: map[string]string{
"name": "workspace-name",
},
},
{
Name: "Name+Param",
Query: "workspace-name template:docker",
Expected: map[string]string{
"name": "workspace-name",
"template": "docker",
},
},
{
Name: "OnlyParams",
Query: "name:workspace-name template:docker owner:alice",
Expected: map[string]string{
"owner": "alice",
"name": "workspace-name",
"template": "docker",
},
},
{
Name: "QuotedParam",
Query: `name:workspace-name template:"docker template" owner:alice`,
Expected: map[string]string{
"owner": "alice",
"name": "workspace-name",
"template": "docker template",
},
},
{
Name: "QuotedKey",
Query: `"spaced key":"spaced value"`,
Expected: map[string]string{
"spaced key": "spaced value",
},
},
{
// This will not return an error
Name: "ExtraKeys",
Query: `foo:bar`,
Expected: map[string]string{
"foo": "bar",
},
},
{
// Quotes keep elements together
Name: "QuotedSpecial",
Query: `name:"workspace:name"`,
Expected: map[string]string{
"name": "workspace:name",
},
},
{
Name: "QuotedMadness",
Query: `"key:is:wild/a/b/c":"foo:bar/baz/zoo:zonk"`,
Expected: map[string]string{
"key:is:wild/a/b/c": "foo:bar/baz/zoo:zonk",
},
},

// Failures
{
Name: "ExtraSlashes",
Query: `foo/bar/baz`,
ExpectedErrorContains: "can only contain 1 '/'",
},
{
Name: "ExtraColon",
Query: `owner:name:extra`,
ExpectedErrorContains: "can only contain 1 ':'",
},
}

for _, c := range testCases {
c := c
t.Run(c.Name, func(t *testing.T) {
t.Parallel()
values, err := httpapi.WorkspaceSearchQuery(c.Query)
if c.ExpectedErrorContains != "" {
require.Error(t, err, "expected error")
require.ErrorContains(t, err, c.ExpectedErrorContains)
} else {
require.NoError(t, err, "expected no error")
require.Equal(t, c.Expected, values, "expected values")
}

})
}
}
6 changes: 4 additions & 2 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,19 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
}

// Set all the query params from the "q" field.
q := r.URL.Query()
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) {
if q.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)
q.Set(k, v)
}
r.URL.RawQuery = q.Encode()

parser := httpapi.NewQueryParamParser()
filter := database.GetWorkspacesWithFilterParams{
Expand Down
19 changes: 19 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,25 @@ func TestWorkspaceFilter(t *testing.T) {
require.Len(t, ws, 1)
require.Equal(t, workspace.ID, ws[0].ID)
})
t.Run("FilterQuery", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
template2 := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template2.ID)

// single workspace
ws, err := client.Workspaces(context.Background(), codersdk.WorkspaceFilter{
FilterQuery: fmt.Sprintf("template:%s %s/%s", template.Name, workspace.OwnerName, workspace.Name),
})
require.NoError(t, err)
require.Len(t, ws, 1)
require.Equal(t, workspace.ID, ws[0].ID)
})
}

func TestPostWorkspaceBuild(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ type WorkspaceFilter struct {
Template string `json:"template,omitempty"`
// Name will return partial matches
Name string `json:"name,omitempty"`
// FilterQuery supports a raw filter query string
FilterQuery string `json:"q,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).Request.
Expand All @@ -243,6 +245,9 @@ func (f WorkspaceFilter) asRequestOption() requestOption {
if f.Template != "" {
q.Set("template", f.Template)
}
if f.FilterQuery != "" {
q.Set("q", f.FilterQuery)
}
r.URL.RawQuery = q.Encode()
}
}
Expand Down