Skip to content

Commit f8a844c

Browse files
committed
wrote test, added type
1 parent 769ea99 commit f8a844c

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

cli/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func create() *cobra.Command {
4949
workspaceName, err = cliui.Prompt(cmd, cliui.PromptOptions{
5050
Text: "Specify a name for your workspace:",
5151
Validate: func(workspaceName string) error {
52-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
52+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName, codersdk.WorkspaceByOwnerAndNameParams{})
5353
if err == nil {
5454
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
5555
}
@@ -61,7 +61,7 @@ func create() *cobra.Command {
6161
}
6262
}
6363

64-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
64+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName, codersdk.WorkspaceByOwnerAndNameParams{})
6565
if err == nil {
6666
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
6767
}

cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, identifier stri
214214
return codersdk.Workspace{}, xerrors.Errorf("invalid workspace name: %q", identifier)
215215
}
216216

217-
return client.WorkspaceByOwnerAndName(cmd.Context(), owner, name)
217+
return client.WorkspaceByOwnerAndName(cmd.Context(), owner, name, codersdk.WorkspaceByOwnerAndNameParams{})
218218
}
219219

220220
// createConfig consumes the global configuration flag to produce a config root.

coderd/workspaces_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
258258
t.Run("NotFound", func(t *testing.T) {
259259
t.Parallel()
260260
client := coderdtest.New(t, nil)
261-
_, err := client.WorkspaceByOwnerAndName(context.Background(), codersdk.Me, "something")
261+
_, err := client.WorkspaceByOwnerAndName(context.Background(), codersdk.Me, "something", codersdk.WorkspaceByOwnerAndNameParams{})
262262
var apiErr *codersdk.Error
263263
require.ErrorAs(t, err, &apiErr)
264264
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
@@ -271,9 +271,38 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
271271
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
272272
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
273273
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
274-
_, err := client.WorkspaceByOwnerAndName(context.Background(), codersdk.Me, workspace.Name)
274+
_, err := client.WorkspaceByOwnerAndName(context.Background(), codersdk.Me, workspace.Name, codersdk.WorkspaceByOwnerAndNameParams{})
275275
require.NoError(t, err)
276276
})
277+
t.Run("deletedGetWorkspaceByOwnerAndName", func(t *testing.T) {
278+
t.Parallel()
279+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
280+
user := coderdtest.CreateFirstUser(t, client)
281+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
282+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
283+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
284+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
285+
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
286+
287+
// Given:
288+
// We delete the workspace
289+
build, err := client.CreateWorkspaceBuild(context.Background(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
290+
Transition: codersdk.WorkspaceTransitionDelete,
291+
})
292+
require.NoError(t, err, "delete the workspace")
293+
coderdtest.AwaitWorkspaceBuildJob(t, client, build.ID)
294+
295+
// Then:
296+
// when we call without includes_deleted, we don't expect to get the workspace back
297+
_, err = client.WorkspaceByOwnerAndName(context.Background(), workspace.OwnerName, workspace.Name, codersdk.WorkspaceByOwnerAndNameParams{})
298+
require.ErrorContains(t, err, "403")
299+
300+
// Then:
301+
// When we call with includes_deleted, we should get the workspace back
302+
workspaceNew, err := client.WorkspaceByOwnerAndName(context.Background(), workspace.OwnerName, workspace.Name, codersdk.WorkspaceByOwnerAndNameParams{IncludeDeleted: true})
303+
require.NoError(t, err)
304+
require.Equal(t, workspace.ID, workspaceNew.ID)
305+
})
277306
}
278307

279308
func TestWorkspaceFilter(t *testing.T) {

codersdk/workspaces.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,17 @@ func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Work
258258
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
259259
}
260260

261+
type WorkspaceByOwnerAndNameParams struct {
262+
IncludeDeleted bool `json:"include_deleted,omitempty"`
263+
}
264+
261265
// WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name.
262-
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string) (Workspace, error) {
263-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil)
266+
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceByOwnerAndNameParams) (Workspace, error) {
267+
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, func(r *http.Request) {
268+
q := r.URL.Query()
269+
q.Set("include_deleted", fmt.Sprintf("%t", params.IncludeDeleted))
270+
r.URL.RawQuery = q.Encode()
271+
})
264272
if err != nil {
265273
return Workspace{}, err
266274
}

site/src/api/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ export const getWorkspaceByOwnerAndName = async (
142142
username = "me",
143143
workspaceName: string,
144144
): Promise<TypesGen.Workspace> => {
145-
const response = await axios.get<TypesGen.Workspace>(`/api/v2/users/${username}/workspace/${workspaceName}`)
145+
const response = await axios.get<TypesGen.Workspace>(`/api/v2/users/${username}/workspace/${workspaceName}`, {
146+
params: { include_deleted: true },
147+
})
146148
return response.data
147149
}
148150

site/src/api/typesGenerated.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ export interface WorkspaceBuildsRequest extends Pagination {
456456
readonly WorkspaceID: string
457457
}
458458

459+
// From codersdk/workspaces.go:261:6
460+
export interface WorkspaceByOwnerAndNameParams {
461+
readonly include_deleted?: boolean
462+
}
463+
459464
// From codersdk/workspaces.go:219:6
460465
export interface WorkspaceFilter {
461466
readonly organization_id?: string

0 commit comments

Comments
 (0)