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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filtering option for listing sandboxes #532

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,6 @@ cython_debug/
# SDK reference artifacts
sdk_ref/
sdkRefRoutes.json

# SDK Client generated spec
spec/openapi_generated.yml
182 changes: 182 additions & 0 deletions packages/js-sdk/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion packages/js-sdk/src/sandbox/sandboxApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export interface SandboxApiOpts
Pick<ConnectionOpts, 'apiKey' | 'debug' | 'domain' | 'requestTimeoutMs'>
> {}

export interface SandboxListOpts extends SandboxApiOpts {
/**
* Filter the list of sandboxes by metadata, e.g. `{"key": "value"}`, if there are multiple filters they are combined with AND.
*/
filters?: Record<string, string>
}

/**
* Information about a sandbox.
*/
Expand Down Expand Up @@ -87,11 +94,21 @@ export class SandboxApi {
*
* @returns list of running sandboxes.
*/
static async list(opts?: SandboxApiOpts): Promise<SandboxInfo[]> {
static async list(
opts?: SandboxListOpts): Promise<SandboxInfo[]> {
const config = new ConnectionConfig(opts)
const client = new ApiClient(config)

let query = undefined
if (opts?.filters) {
const encodedPairs: Record<string, string> = Object.fromEntries(Object.entries(opts.filters).map(([key, value]) => [encodeURIComponent(key),encodeURIComponent(value)]))
query = new URLSearchParams(encodedPairs).toString()
}

const res = await client.api.GET('/sandboxes', {
params: {
query: {query},
},
signal: config.getSignal(opts?.requestTimeoutMs),
})

Expand Down
18 changes: 18 additions & 0 deletions packages/js-sdk/tests/api/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@ sandboxTest.skipIf(isDebug)('list sandboxes', async ({ sandbox }) => {
)
}
})

sandboxTest.skipIf(isDebug)('list sandboxes with filter', async () => {
const uniqueId = Date.now().toString()
// Create an extra sandbox with a uniqueId
const extraSbx = await Sandbox.create({ })
try {
const sbx = await Sandbox.create({metadata: {uniqueId: uniqueId}})
try {
const sandboxes = await Sandbox.list({filters: {uniqueId}})
assert.equal(sandboxes.length, 1)
assert.equal(sandboxes[0].sandboxId, sbx.sandboxId)
} finally {
await sbx.kill()
}
} finally {
await extraSbx.kill()
}
})
Loading
Loading