Skip to content
Merged
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: added include_deleted
relates to #1955
  • Loading branch information
Kira-Pilot committed Jun 8, 2022
commit 142ad4eeb8b188577c2dde35cd2ae78734f59c93
27 changes: 27 additions & 0 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,37 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
owner := httpmw.UserParam(r)
workspaceName := chi.URLParam(r, "workspacename")

var (
includeDeletedStr = r.URL.Query().Get("include_deleted")
includeDeleted = false
)
if includeDeletedStr != "" {
var err error
includeDeleted, err = strconv.ParseBool(includeDeletedStr)
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Invalid boolean value %q for \"include_deleted\" query param.", includeDeletedStr),
Validations: []httpapi.Error{
{Field: "include_deleted", Detail: "Must be a valid boolean"},
},
})
return
}
}

workspace, err := api.Database.GetWorkspaceByOwnerIDAndName(r.Context(), database.GetWorkspaceByOwnerIDAndNameParams{
OwnerID: owner.ID,
Name: workspaceName,
})

if includeDeleted && errors.Is(err, sql.ErrNoRows) {
workspace, err = api.Database.GetWorkspaceByOwnerIDAndName(r.Context(), database.GetWorkspaceByOwnerIDAndNameParams{
OwnerID: owner.ID,
Name: workspaceName,
Deleted: includeDeleted,
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this works as intended. The param here in the SQL matches for deleted = $includeDeleted.

So if you set this to true, the workspace will not show if the workspace is deleted. We will need to update the SQL too, or just always return deleted workspaces by default.

Copy link
Member

Choose a reason for hiding this comment

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

See how it's handled here:

if workspace.Deleted && !showDeleted {
httpapi.Write(rw, http.StatusGone, httpapi.Response{
Message: fmt.Sprintf("Workspace %q was deleted, you can view this workspace by specifying '?deleted=true' and trying again.", workspace.ID.String()),
})
return
}

I think the deleted query param is kinda annoying as you have to know the state of the workspace to query it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@Emyrk Hmm, testing it in the UI seems to work.

  1. if include_deleted is false, then we never enter this code block (because errors.Is(err, sql.ErrNoRows) is false
  2. otherwise we enter this code block and try to query for a deleted workspace

In either case, if we get nothing back, we then go into the httpapi.Forbidden(rw) block below. I agree editing the SQL would also work. I just didn't know what other ramifications that would have.

Let me know if I'm misunderstanding your comment!

})
}

if errors.Is(err, sql.ErrNoRows) {
// Do not leak information if the workspace exists or not
httpapi.Forbidden(rw)
Expand Down