Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added include_deleted to getWorkspaceByOwnerAndName #2164

Merged
merged 10 commits into from
Jun 8, 2022
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")
johnstcn marked this conversation as resolved.
Show resolved Hide resolved
includeDeleted = false
)
if includeDeletedStr != "" {
var err error
includeDeleted, err = strconv.ParseBool(includeDeletedStr)
Kira-Pilot marked this conversation as resolved.
Show resolved Hide resolved
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,
})

Kira-Pilot marked this conversation as resolved.
Show resolved Hide resolved
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!

})
}

Kira-Pilot marked this conversation as resolved.
Show resolved Hide resolved
if errors.Is(err, sql.ErrNoRows) {
// Do not leak information if the workspace exists or not
httpapi.Forbidden(rw)
Expand Down