Skip to content

Commit 40123be

Browse files
add aliases
1 parent 4607933 commit 40123be

File tree

6 files changed

+392
-3
lines changed

6 files changed

+392
-3
lines changed

openapi.json

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,51 @@
7272
"title": "VMUpdateHibernationTimeoutRequest",
7373
"type": "object"
7474
},
75+
"VMAssignTagAliasResponse": {
76+
"allOf": [
77+
{
78+
"properties": {
79+
"errors": {
80+
"items": {
81+
"oneOf": [
82+
{ "type": "string" },
83+
{ "additionalProperties": true, "type": "object" }
84+
],
85+
"title": "Error"
86+
},
87+
"type": "array"
88+
},
89+
"success": { "type": "boolean" }
90+
},
91+
"title": "Response",
92+
"type": "object"
93+
},
94+
{
95+
"properties": {
96+
"data": {
97+
"properties": {
98+
"alias": { "type": "string" },
99+
"namespace": { "type": "string" },
100+
"tag_alias_id": { "type": "string" },
101+
"tag_id": { "type": "string" },
102+
"team_id": { "type": "string" }
103+
},
104+
"required": [
105+
"tag_alias_id",
106+
"team_id",
107+
"tag_id",
108+
"namespace",
109+
"alias"
110+
],
111+
"type": "object"
112+
}
113+
},
114+
"type": "object"
115+
}
116+
],
117+
"title": "VMAssignTagAliasResponse",
118+
"type": "object"
119+
},
75120
"PreviewToken": {
76121
"properties": {
77122
"expires_at": { "nullable": true, "type": "string" },
@@ -212,6 +257,54 @@
212257
"title": "VMCreateTagResponse",
213258
"type": "object"
214259
},
260+
"VMListRunningVMsResponse": {
261+
"allOf": [
262+
{
263+
"properties": {
264+
"errors": {
265+
"items": {
266+
"oneOf": [
267+
{ "type": "string" },
268+
{ "additionalProperties": true, "type": "object" }
269+
],
270+
"title": "Error"
271+
},
272+
"type": "array"
273+
},
274+
"success": { "type": "boolean" }
275+
},
276+
"title": "Response",
277+
"type": "object"
278+
},
279+
{
280+
"properties": {
281+
"data": {
282+
"properties": {
283+
"concurrent_vm_count": { "type": "integer" },
284+
"concurrent_vm_limit": { "type": "integer" },
285+
"vms": {
286+
"items": {
287+
"properties": { "id": { "type": "string" } },
288+
"type": "object"
289+
},
290+
"required": ["id"],
291+
"type": "array"
292+
}
293+
},
294+
"required": [
295+
"vms",
296+
"concurrent_vm_count",
297+
"concurrent_vm_limit"
298+
],
299+
"type": "object"
300+
}
301+
},
302+
"type": "object"
303+
}
304+
],
305+
"title": "VMListRunningVMsResponse",
306+
"type": "object"
307+
},
215308
"SandboxGetResponse": {
216309
"allOf": [
217310
{
@@ -442,6 +535,13 @@
442535
"title": "TokenUpdateRequest",
443536
"type": "object"
444537
},
538+
"VMAssignTagAliasRequest": {
539+
"description": "Assign a tag alias to a VM",
540+
"properties": { "tag_id": { "type": "string" } },
541+
"required": ["tag_id"],
542+
"title": "VMAssignTagAliasRequest",
543+
"type": "object"
544+
},
445545
"VMHibernateResponse": {
446546
"allOf": [
447547
{
@@ -1832,6 +1932,57 @@
18321932
"tags": []
18331933
}
18341934
},
1935+
"/vm/alias/{namespace}/{alias}": {
1936+
"put": {
1937+
"callbacks": {},
1938+
"description": "Assign a tag alias to a VM tag.\n",
1939+
"operationId": "vm/assign_tag_alias",
1940+
"parameters": [
1941+
{
1942+
"description": "Tag alias namespace",
1943+
"example": "my-project",
1944+
"in": "path",
1945+
"name": "namespace",
1946+
"required": true,
1947+
"schema": { "type": "string" }
1948+
},
1949+
{
1950+
"description": "Tag alias",
1951+
"example": "latest",
1952+
"in": "path",
1953+
"name": "alias",
1954+
"required": true,
1955+
"schema": { "type": "string" }
1956+
}
1957+
],
1958+
"requestBody": {
1959+
"content": {
1960+
"application/json": {
1961+
"schema": {
1962+
"$ref": "#/components/schemas/VMAssignTagAliasRequest"
1963+
}
1964+
}
1965+
},
1966+
"description": "VM Assign Tag Alias Request",
1967+
"required": false
1968+
},
1969+
"responses": {
1970+
"200": {
1971+
"content": {
1972+
"application/json": {
1973+
"schema": {
1974+
"$ref": "#/components/schemas/VMAssignTagAliasResponse"
1975+
}
1976+
}
1977+
},
1978+
"description": "VM Assign Tag Alias Response"
1979+
}
1980+
},
1981+
"security": [{ "authorization": ["vm:manage"] }],
1982+
"summary": "Assign a tag alias to a VM tag",
1983+
"tags": ["vm"]
1984+
}
1985+
},
18351986
"/vm/clusters": {
18361987
"get": {
18371988
"callbacks": {},
@@ -1855,6 +2006,29 @@
18552006
"tags": ["vm"]
18562007
}
18572008
},
2009+
"/vm/running": {
2010+
"get": {
2011+
"callbacks": {},
2012+
"description": "List information about currently running VMs. This information is updated roughly every 30 seconds, so this data is not guaranteed to be perfectly up-to-date.\n",
2013+
"operationId": "vm/list_running_vms",
2014+
"parameters": [],
2015+
"responses": {
2016+
"200": {
2017+
"content": {
2018+
"application/json": {
2019+
"schema": {
2020+
"$ref": "#/components/schemas/VMListRunningVMsResponse"
2021+
}
2022+
}
2023+
},
2024+
"description": "VM List Running VMs Response"
2025+
}
2026+
},
2027+
"security": [{ "authorization": ["vm:manage"] }],
2028+
"summary": "List information about currently running VMs",
2029+
"tags": ["vm"]
2030+
}
2031+
},
18582032
"/vm/tag": {
18592033
"post": {
18602034
"callbacks": {},

src/Sandboxes.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
SessionCreateOptions,
2727
} from "./types";
2828
import { PitcherManagerResponse } from "@codesandbox/pitcher-client";
29+
import { sleep } from "./utils/sleep";
2930

3031
export async function startVm(
3132
apiClient: Client,
@@ -144,8 +145,34 @@ export class Sandboxes {
144145
* Will resolve once the sandbox is restarted with its setup running.
145146
*/
146147
public async restart(sandboxId: string, opts?: StartSandboxOpts) {
147-
await this.shutdown(sandboxId);
148-
const startResponse = await startVm(this.apiClient, sandboxId, opts);
148+
let didRestart = false;
149+
150+
for (let attempt = 1; attempt <= 3; attempt++) {
151+
try {
152+
await this.shutdown(sandboxId);
153+
didRestart = true;
154+
} catch (e) {
155+
await sleep(500);
156+
}
157+
}
158+
159+
if (!didRestart) {
160+
throw new Error("Failed to shutdown VM after 3 attempts");
161+
}
162+
163+
let startResponse: PitcherManagerResponse | undefined;
164+
165+
for (let attempt = 1; attempt <= 3; attempt++) {
166+
try {
167+
startResponse = await startVm(this.apiClient, sandboxId, opts);
168+
} catch (e) {
169+
await sleep(500);
170+
}
171+
}
172+
173+
if (!startResponse) {
174+
throw new Error("Failed to start VM after 3 attempts");
175+
}
149176

150177
return new Sandbox(sandboxId, this.apiClient, startResponse);
151178
}

src/Session/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export class Session {
6161
}
6262
private disposable = new Disposable();
6363

64+
get workspacePath() {
65+
return this.agentClient.workspacePath;
66+
}
67+
6468
/**
6569
* Namespace for all filesystem operations on this Sandbox
6670
*/

src/api-clients/client/sdk.gen.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
4-
import type { MetaInfoData, MetaInfoResponse, WorkspaceCreateData, WorkspaceCreateResponse2, TokenCreateData, TokenCreateResponse2, TokenUpdateData, TokenUpdateResponse2, SandboxListData, SandboxListResponse2, SandboxCreateData, SandboxCreateResponse2, SandboxGetData, SandboxGetResponse2, SandboxForkData, SandboxForkResponse2, PreviewTokenRevokeAllData, PreviewTokenRevokeAllResponse2, PreviewTokenListData, PreviewTokenListResponse2, PreviewTokenCreateData, PreviewTokenCreateResponse2, PreviewTokenUpdateData, PreviewTokenUpdateResponse2, VmListClustersData, VmListClustersResponse2, VmCreateTagData, VmCreateTagResponse2, VmHibernateData, VmHibernateResponse2, VmUpdateHibernationTimeoutData, VmUpdateHibernationTimeoutResponse2, VmCreateSessionData, VmCreateSessionResponse2, VmShutdownData, VmShutdownResponse2, VmUpdateSpecsData, VmUpdateSpecsResponse2, VmStartData, VmStartResponse2, VmUpdateSpecs2Data, VmUpdateSpecs2Response, PreviewHostListData, PreviewHostListResponse2, PreviewHostCreateData, PreviewHostCreateResponse, PreviewHostUpdateData, PreviewHostUpdateResponse } from './types.gen';
4+
import type { MetaInfoData, MetaInfoResponse, WorkspaceCreateData, WorkspaceCreateResponse2, TokenCreateData, TokenCreateResponse2, TokenUpdateData, TokenUpdateResponse2, SandboxListData, SandboxListResponse2, SandboxCreateData, SandboxCreateResponse2, SandboxGetData, SandboxGetResponse2, SandboxForkData, SandboxForkResponse2, PreviewTokenRevokeAllData, PreviewTokenRevokeAllResponse2, PreviewTokenListData, PreviewTokenListResponse2, PreviewTokenCreateData, PreviewTokenCreateResponse2, PreviewTokenUpdateData, PreviewTokenUpdateResponse2, VmAssignTagAliasData, VmAssignTagAliasResponse2, VmListClustersData, VmListClustersResponse2, VmListRunningVmsData, VmListRunningVmsResponse2, VmCreateTagData, VmCreateTagResponse2, VmHibernateData, VmHibernateResponse2, VmUpdateHibernationTimeoutData, VmUpdateHibernationTimeoutResponse2, VmCreateSessionData, VmCreateSessionResponse2, VmShutdownData, VmShutdownResponse2, VmUpdateSpecsData, VmUpdateSpecsResponse2, VmStartData, VmStartResponse2, VmUpdateSpecs2Data, VmUpdateSpecs2Response, PreviewHostListData, PreviewHostListResponse2, PreviewHostCreateData, PreviewHostCreateResponse, PreviewHostUpdateData, PreviewHostUpdateResponse } from './types.gen';
55
import { client as _heyApiClient } from './client.gen';
66

77
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
@@ -250,6 +250,28 @@ export const previewTokenUpdate = <ThrowOnError extends boolean = false>(options
250250
});
251251
};
252252

253+
/**
254+
* Assign a tag alias to a VM tag
255+
* Assign a tag alias to a VM tag.
256+
*
257+
*/
258+
export const vmAssignTagAlias = <ThrowOnError extends boolean = false>(options: Options<VmAssignTagAliasData, ThrowOnError>) => {
259+
return (options.client ?? _heyApiClient).put<VmAssignTagAliasResponse2, unknown, ThrowOnError>({
260+
security: [
261+
{
262+
scheme: 'bearer',
263+
type: 'http'
264+
}
265+
],
266+
url: '/vm/alias/{namespace}/{alias}',
267+
...options,
268+
headers: {
269+
'Content-Type': 'application/json',
270+
...options?.headers
271+
}
272+
});
273+
};
274+
253275
/**
254276
* List all available clusters
255277
* List all available clusters.
@@ -268,6 +290,24 @@ export const vmListClusters = <ThrowOnError extends boolean = false>(options?: O
268290
});
269291
};
270292

293+
/**
294+
* List information about currently running VMs
295+
* List information about currently running VMs. This information is updated roughly every 30 seconds, so this data is not guaranteed to be perfectly up-to-date.
296+
*
297+
*/
298+
export const vmListRunningVms = <ThrowOnError extends boolean = false>(options?: Options<VmListRunningVmsData, ThrowOnError>) => {
299+
return (options?.client ?? _heyApiClient).get<VmListRunningVmsResponse2, unknown, ThrowOnError>({
300+
security: [
301+
{
302+
scheme: 'bearer',
303+
type: 'http'
304+
}
305+
],
306+
url: '/vm/running',
307+
...options
308+
});
309+
};
310+
271311
/**
272312
* Create a new tag for a VM
273313
* Creates a new tag for a VM.

0 commit comments

Comments
 (0)