Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: add workspace option 'deleted' to options type #2095

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 3 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 0 additions & 6 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
})
return
}
if !workspace.Deleted && showDeleted {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Workspace %q is not deleted, please remove '?deleted=true' and try again", workspace.ID.String()),
})
return
}

build, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(r.Context(), workspace.ID)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ func TestWorkspace(t *testing.T) {
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

// Getting with deleted=true should fail.
// Getting with deleted=true should still work.
_, err := client.DeletedWorkspace(context.Background(), workspace.ID)
require.Error(t, err)
require.ErrorContains(t, err, "400") // bad request
require.NoError(t, err)

// Delete the workspace
build, err := client.CreateWorkspaceBuild(context.Background(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
Expand Down
8 changes: 0 additions & 8 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ type Client struct {

type requestOption func(*http.Request)

func queryParam(k, v string) requestOption {
return func(r *http.Request) {
q := r.URL.Query()
q.Set(k, v)
r.URL.RawQuery = q.Encode()
}
}

// Request performs an HTTP request with the body provided.
// The caller is responsible for closing the response body.
func (c *Client) Request(ctx context.Context, method, path string, body interface{}, opts ...requestOption) (*http.Response, error) {
Expand Down
21 changes: 20 additions & 1 deletion codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,33 @@ type CreateWorkspaceBuildRequest struct {
ProvisionerState []byte `json:"state,omitempty"`
}

type WorkspaceOptions struct {
Deleted bool `json:"deleted,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (o WorkspaceOptions) asRequestOption() requestOption {
return func(r *http.Request) {
q := r.URL.Query()
if o.Deleted {
q.Set("deleted", "true")
}
r.URL.RawQuery = q.Encode()
}
}

// Workspace returns a single workspace.
func (c *Client) Workspace(ctx context.Context, id uuid.UUID) (Workspace, error) {
return c.getWorkspace(ctx, id)
}

// DeletedWorkspace returns a single workspace that was deleted.
func (c *Client) DeletedWorkspace(ctx context.Context, id uuid.UUID) (Workspace, error) {
return c.getWorkspace(ctx, id, queryParam("deleted", "true"))
o := WorkspaceOptions{
Deleted: true,
}
return c.getWorkspace(ctx, id, o.asRequestOption())
}

func (c *Client) getWorkspace(ctx context.Context, id uuid.UUID, opts ...requestOption) (Workspace, error) {
Expand Down
15 changes: 10 additions & 5 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export interface ProvisionerJobLog {
readonly output: string
}

// From codersdk/workspaces.go:182:6
// From codersdk/workspaces.go:201:6
export interface PutExtendWorkspaceRequest {
readonly deadline: string
}
Expand Down Expand Up @@ -298,12 +298,12 @@ export interface UpdateUserProfileRequest {
readonly username: string
}

// From codersdk/workspaces.go:141:6
// From codersdk/workspaces.go:160:6
export interface UpdateWorkspaceAutostartRequest {
readonly schedule?: string
}

// From codersdk/workspaces.go:161:6
// From codersdk/workspaces.go:180:6
export interface UpdateWorkspaceTTLRequest {
readonly ttl_ms?: number
}
Expand Down Expand Up @@ -445,18 +445,23 @@ export interface WorkspaceBuild {
readonly deadline: string
}

// From codersdk/workspaces.go:64:6
// From codersdk/workspaces.go:83:6
export interface WorkspaceBuildsRequest extends Pagination {
readonly WorkspaceID: string
}

// From codersdk/workspaces.go:200:6
// From codersdk/workspaces.go:219:6
export interface WorkspaceFilter {
readonly organization_id?: string
readonly owner?: string
readonly name?: string
}

// From codersdk/workspaces.go:41:6
export interface WorkspaceOptions {
readonly deleted?: boolean
}

// From codersdk/workspaceresources.go:21:6
export interface WorkspaceResource {
readonly id: string
Expand Down