Skip to content

Commit

Permalink
feat: add support for updating hibernation timeout (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
CompuIves authored Jan 10, 2025
1 parent 6cd070e commit efd2051
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 7 deletions.
137 changes: 136 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,57 @@
],
"title": "VMStartResponse"
},
"VMUpdateHibernationTimeoutRequest": {
"properties": {
"hibernation_timeout_seconds": {
"description": "The new hibernation timeout in seconds.\n\nMust be greater than 0 and less than or equal to 86400 (24 hours).\n",
"example": 300,
"maximum": 86400,
"minimum": 1,
"type": "integer"
}
},
"required": ["hibernation_timeout_seconds"],
"title": "VMUpdateHibernationTimeoutRequest",
"type": "object"
},
"VMUpdateHibernationTimeoutResponse": {
"allOf": [
{
"properties": {
"errors": {
"items": [
{
"oneOf": [
{ "type": "string" },
{ "additionalProperties": true, "type": "object" }
],
"title": "Error"
}
],
"type": "array"
},
"success": { "type": "boolean" }
},
"title": "Response",
"type": "object"
},
{
"properties": {
"data": {
"properties": {
"hibernation_timeout_seconds": { "type": "integer" },
"id": { "type": "string" }
},
"required": ["id", "hibernation_timeout_seconds"],
"type": "object"
}
},
"type": "object"
}
],
"title": "VMUpdateHibernationTimeoutResponse"
},
"VMUpdateSpecsRequest": {
"properties": {
"tier": {
Expand Down Expand Up @@ -1181,6 +1232,49 @@
"tags": ["vm"]
}
},
"/vm/{id}/hibernation_timeout": {
"put": {
"callbacks": {},
"description": "Updates the hibernation timeout of a running VM.\n\nThis endpoint can only be used on VMs that belong to your team's workspace.\nThe new timeout must be greater than 0 and less than or equal to 86400 seconds (24 hours).\n",
"operationId": "vm/update_hibernation_timeout",
"parameters": [
{
"description": "Sandbox ID",
"example": "new",
"in": "path",
"name": "id",
"required": true,
"schema": { "type": "string" }
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VMUpdateHibernationTimeoutRequest"
}
}
},
"description": "VM Update Hibernation Timeout Request",
"required": false
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VMUpdateHibernationTimeoutResponse"
}
}
},
"description": "VM Update Hibernation Timeout Response"
}
},
"security": [{ "authorization": ["vm:manage"] }],
"summary": "Update VM Hibernation Timeout",
"tags": ["vm"]
}
},
"/vm/{id}/shutdown": {
"post": {
"callbacks": {},
Expand Down Expand Up @@ -1220,6 +1314,47 @@
"tags": ["vm"]
}
},
"/vm/{id}/specs": {
"put": {
"callbacks": {},
"description": "Updates the specifications (CPU, memory, storage) of a running VM.\n\nThis endpoint can only be used on VMs that belong to your team's workspace.\nThe new tier must not exceed your team's maximum allowed tier.\n",
"operationId": "vm/update_specs",
"parameters": [
{
"description": "Sandbox ID",
"example": "new",
"in": "path",
"name": "id",
"required": true,
"schema": { "type": "string" }
}
],
"requestBody": {
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/VMUpdateSpecsRequest" }
}
},
"description": "VM Update Specs Request",
"required": false
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VMUpdateSpecsResponse"
}
}
},
"description": "VM Update Specs Response"
}
},
"security": [{ "authorization": ["vm:manage"] }],
"summary": "Update VM Specs",
"tags": ["vm"]
}
},
"/vm/{id}/start": {
"post": {
"callbacks": {},
Expand Down Expand Up @@ -1263,7 +1398,7 @@
"post": {
"callbacks": {},
"description": "Updates the specifications (CPU, memory, storage) of a running VM.\n\nThis endpoint can only be used on VMs that belong to your team's workspace.\nThe new tier must not exceed your team's maximum allowed tier.\n",
"operationId": "vm/update_specs",
"operationId": "vm/update_specs (2)",
"parameters": [
{
"description": "Sandbox ID",
Expand Down
36 changes: 33 additions & 3 deletions src/client/sdk.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type OptionsLegacyParser } from '@hey-api/client-fetch';
import type { MetaInfoError, MetaInfoResponse, WorkspaceCreateData, WorkspaceCreateError, WorkspaceCreateResponse2, TokenCreateData, TokenCreateError, TokenCreateResponse2, TokenUpdateData, TokenUpdateError, TokenUpdateResponse2, SandboxListData, SandboxListError, SandboxListResponse2, SandboxCreateData, SandboxCreateError, SandboxCreateResponse2, SandboxGetData, SandboxGetError, SandboxGetResponse2, SandboxForkData, SandboxForkError, SandboxForkResponse2, VmHibernateData, VmHibernateError, VmHibernateResponse, VmShutdownData, VmShutdownError, VmShutdownResponse, VmStartData, VmStartError, VmStartResponse, VmUpdateSpecsData, VmUpdateSpecsError, VmUpdateSpecsResponse } from './types.gen';
import type { MetaInfoError, MetaInfoResponse, WorkspaceCreateData, WorkspaceCreateError, WorkspaceCreateResponse2, TokenCreateData, TokenCreateError, TokenCreateResponse2, TokenUpdateData, TokenUpdateError, TokenUpdateResponse2, SandboxListData, SandboxListError, SandboxListResponse2, SandboxCreateData, SandboxCreateError, SandboxCreateResponse2, SandboxGetData, SandboxGetError, SandboxGetResponse2, SandboxForkData, SandboxForkError, SandboxForkResponse2, VmHibernateData, VmHibernateError, VmHibernateResponse, VmUpdateHibernationTimeoutData, VmUpdateHibernationTimeoutError, VmUpdateHibernationTimeoutResponse, VmShutdownData, VmShutdownError, VmShutdownResponse, VmUpdateSpecsData, VmUpdateSpecsError, VmUpdateSpecsResponse, VmStartData, VmStartError, VmStartResponse, VmUpdateSpecs2Data, VmUpdateSpecs2Error, VmUpdateSpecs2Response } from './types.gen';

export const client = createClient(createConfig());

Expand Down Expand Up @@ -118,6 +118,21 @@ export const vmHibernate = <ThrowOnError extends boolean = false>(options: Optio
});
};

/**
* Update VM Hibernation Timeout
* Updates the hibernation timeout of a running VM.
*
* This endpoint can only be used on VMs that belong to your team's workspace.
* The new timeout must be greater than 0 and less than or equal to 86400 seconds (24 hours).
*
*/
export const vmUpdateHibernationTimeout = <ThrowOnError extends boolean = false>(options: OptionsLegacyParser<VmUpdateHibernationTimeoutData, ThrowOnError>) => {
return (options?.client ?? client).put<VmUpdateHibernationTimeoutResponse, VmUpdateHibernationTimeoutError, ThrowOnError>({
...options,
url: '/vm/{id}/hibernation_timeout'
});
};

/**
* Shutdown a VM
* Stops a running VM, ending all currently running processes
Expand All @@ -135,6 +150,21 @@ export const vmShutdown = <ThrowOnError extends boolean = false>(options: Option
});
};

/**
* Update VM Specs
* Updates the specifications (CPU, memory, storage) of a running VM.
*
* This endpoint can only be used on VMs that belong to your team's workspace.
* The new tier must not exceed your team's maximum allowed tier.
*
*/
export const vmUpdateSpecs = <ThrowOnError extends boolean = false>(options: OptionsLegacyParser<VmUpdateSpecsData, ThrowOnError>) => {
return (options?.client ?? client).put<VmUpdateSpecsResponse, VmUpdateSpecsError, ThrowOnError>({
...options,
url: '/vm/{id}/specs'
});
};

/**
* Start a VM
* Start a virtual machine for the sandbox (devbox) with the given ID
Expand Down Expand Up @@ -162,8 +192,8 @@ export const vmStart = <ThrowOnError extends boolean = false>(options: OptionsLe
* The new tier must not exceed your team's maximum allowed tier.
*
*/
export const vmUpdateSpecs = <ThrowOnError extends boolean = false>(options: OptionsLegacyParser<VmUpdateSpecsData, ThrowOnError>) => {
return (options?.client ?? client).post<VmUpdateSpecsResponse, VmUpdateSpecsError, ThrowOnError>({
export const vmUpdateSpecs2 = <ThrowOnError extends boolean = false>(options: OptionsLegacyParser<VmUpdateSpecs2Data, ThrowOnError>) => {
return (options?.client ?? client).post<VmUpdateSpecs2Response, VmUpdateSpecs2Error, ThrowOnError>({
...options,
url: '/vm/{id}/update_specs'
});
Expand Down
62 changes: 59 additions & 3 deletions src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,28 @@ export type VMStartResponse = {
};
};

export type VMUpdateHibernationTimeoutRequest = {
/**
* The new hibernation timeout in seconds.
*
* Must be greater than 0 and less than or equal to 86400 (24 hours).
*
*/
hibernation_timeout_seconds: number;
};

export type VMUpdateHibernationTimeoutResponse = {
errors?: Array<((string | {
[key: string]: unknown;
}))>;
success?: boolean;
} & {
data?: {
hibernation_timeout_seconds: number;
id: string;
};
};

export type VMUpdateSpecsRequest = {
/**
* Determines which specs to update the VM with.
Expand Down Expand Up @@ -555,6 +577,23 @@ export type VmHibernateResponse = (VMHibernateResponse);

export type VmHibernateError = unknown;

export type VmUpdateHibernationTimeoutData = {
/**
* VM Update Hibernation Timeout Request
*/
body?: VMUpdateHibernationTimeoutRequest;
path: {
/**
* Sandbox ID
*/
id: string;
};
};

export type VmUpdateHibernationTimeoutResponse = (VMUpdateHibernationTimeoutResponse);

export type VmUpdateHibernationTimeoutError = unknown;

export type VmShutdownData = {
/**
* VM Shutdown Request
Expand All @@ -572,6 +611,23 @@ export type VmShutdownResponse = (VMShutdownResponse);

export type VmShutdownError = unknown;

export type VmUpdateSpecsData = {
/**
* VM Update Specs Request
*/
body?: VMUpdateSpecsRequest;
path: {
/**
* Sandbox ID
*/
id: string;
};
};

export type VmUpdateSpecsResponse = (VMUpdateSpecsResponse);

export type VmUpdateSpecsError = unknown;

export type VmStartData = {
/**
* VM Start Request
Expand All @@ -589,7 +645,7 @@ export type VmStartResponse = (VMStartResponse);

export type VmStartError = unknown;

export type VmUpdateSpecsData = {
export type VmUpdateSpecs2Data = {
/**
* VM Update Specs Request
*/
Expand All @@ -602,6 +658,6 @@ export type VmUpdateSpecsData = {
};
};

export type VmUpdateSpecsResponse = (VMUpdateSpecsResponse);
export type VmUpdateSpecs2Response = (VMUpdateSpecsResponse);

export type VmUpdateSpecsError = unknown;
export type VmUpdateSpecs2Error = unknown;
23 changes: 23 additions & 0 deletions src/sandbox-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
vmHibernate,
vmShutdown,
vmStart,
vmUpdateHibernationTimeout,
vmUpdateSpecs,
} from "./client";
import { Sandbox } from "./sandbox";
Expand Down Expand Up @@ -371,6 +372,28 @@ export class SandboxClient {
handleResponse(response, `Failed to update sandbox tier ${id}`);
}

/**
* Updates the hibernation timeout of a sandbox.
*
* @param id The ID of the sandbox to update
* @param timeoutSeconds The new hibernation timeout in seconds
*/
async updateHibernationTimeout(
id: string,
timeoutSeconds: number
): Promise<void> {
const response = await vmUpdateHibernationTimeout({
client: this.apiClient,
path: { id },
body: { hibernation_timeout_seconds: timeoutSeconds },
});

handleResponse(
response,
`Failed to update hibernation timeout for sandbox ${id}`
);
}

private async connectToSandbox(
id: string,
startVm: () => Promise<
Expand Down
9 changes: 9 additions & 0 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,13 @@ export class Sandbox extends SandboxWithoutClient {
public async updateTier(tier: VMTier): Promise<void> {
await this.sandboxClient.updateTier(this.id, tier);
}

/**
* Updates the hibernation timeout of a sandbox.
*
* @param timeoutSeconds The new hibernation timeout in seconds
*/
public async updateHibernationTimeout(timeoutSeconds: number): Promise<void> {
await this.sandboxClient.updateHibernationTimeout(this.id, timeoutSeconds);
}
}

0 comments on commit efd2051

Please sign in to comment.