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

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 10, 2022

Continue of #2134

What this does

Adds workspace filter query support in backend. Adds template_name query param support as well.

Extra

  • Make 1 GetTemplatesWithFilter rather than multiple sql queries with different params
  • Make a unit test to keep the fake database methods to the set required (delete excessive methods)
  • Add a QueryParamParser to handle parsing query params better.

Future Work

Make the FE pass the query string as is to the BE. @f0ssel ?

// functions. The fake database is a manual implementation, so it is possible
// we forget to delete functions that we remove. This unit test just ensures
// we remove the extra methods.
func TestExactMethods(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps find which methods are not required on the fake. Just good for keeping that struct clean.

@Emyrk Emyrk marked this pull request as ready for review June 10, 2022 15:41
@Emyrk Emyrk requested a review from a team as a code owner June 10, 2022 15:41
@Emyrk Emyrk requested a review from kylecarbs June 10, 2022 19:03
Comment on lines 152 to 157
if !(filter.OwnerID == uuid.Nil || filter.OwnerID == apiKey.UserID) {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "Cannot set both \"me\" in \"owner_name\" and use \"owner_id\".",
})
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kinda hesitant to return 4xx errors in this route since it's powered by a filter. We can add support for validation error in the query bar on the dashboard, but right now we the behavior is we just take the first.

Could we just drop ownerID? I think we use owner name on the app everywhere anyways?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with dropping owner_id for now 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping it in the database filter because apiKey has userID, not their name. But I'll drop it from the query params.

@Emyrk
Copy link
Member Author

Emyrk commented Jun 10, 2022

It seems non-standard to have both from my perspective. What do you think about disallowing other filters in favor of the queryable format?

If we don't allow ?owner=XXX and only allow ?q="...", I still think we should keep the SDK at

type WorkspaceFilter struct {
	// Owner can be "me" or a username
	Owner string `json:"owner,omitempty"`
	// Template is a template name
	Template string `json:"template,omitempty"`
	// Name will return partial matches
	Name string `json:"name,omitempty"`
}

I'll need to add a function to convert these fields into the q string... but then our autogenerated type will be missing the q field for the frontend...

Do you prefer to do:

client.Workspaces(ctx, codersdk.WorkspaceFilter{
  FilterQuery: fmt.Sprintf("owner:%s name:%s", codersdk.Me, workspaceName),
}

// OR
client.Workspaces(ctx, codersdk.WorkspaceFilter{
  Owner: codersdk.Me,
  Name: workspaceName,
}

To me, it feels awkward to use the q param from within code. It really only makes sense from the UI, as the query has to be decomposed into it's elements anyway.

@Emyrk
Copy link
Member Author

Emyrk commented Jun 10, 2022

Also github issues does support labels as a query param alonside the q filter string. So they do a mix as ?q=label:backend and ?label=backend is both supported.

But it seems some filter strings are not their own query params.

@Emyrk Emyrk requested a review from kylecarbs June 10, 2022 21:13
@Emyrk Emyrk enabled auto-merge (squash) June 10, 2022 21:13

// WorkspaceSearchQuery takes a query string and breaks it into its queryparams
// as a set of key=value.
func WorkspaceSearchQuery(query string) (map[string]string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems odd that this would be exported from the coderd package. I'd personally prefer this be internal and tested from the endpoint itself to prevent any implementation mismatches. I'm open to thoughts here!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I noticed AuthorizeFilter is exposed too, and it'd be nice to make that internal as part of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment for the Authorize function. It's exposed on the primary API struct, but it doesn't need to be from what it seems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can unexport it, but I really much rather just test this pure function. Having to interpret what the results of this is based on the api feedback of the endpoint sound challenging. Testing this function as is is trivial.

Emyrk added 2 commits June 13, 2022 08:41
- Unexport workspace search query function
- move code into workspaces.go
- use strings.SplitFields
@Emyrk
Copy link
Member Author

Emyrk commented Jun 13, 2022

@kylecarbs I'll just drop non q query params. Not worth holding up the PR over, I'll make the SDK convert the struct to the filter string.

@Emyrk Emyrk requested a review from kylecarbs June 13, 2022 16:54
@@ -8,13 +8,33 @@ WHERE
LIMIT
1;

-- name: GetTemplatesByIDs :many
-- name: GetTemplatesWithFilter :many
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is really just GetTemplates (and that's how we use it from our API)

@Emyrk Emyrk merged commit dc1de58 into main Jun 14, 2022
@Emyrk Emyrk deleted the stevenmasley/workspace-template-search branch June 14, 2022 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants