Skip to content

Commit 18576fa

Browse files
committed
switch frontend use of compute server api to use conat instead of nextjs
1 parent f24ea1d commit 18576fa

File tree

3 files changed

+59
-70
lines changed

3 files changed

+59
-70
lines changed

src/packages/conat/hub/api/compute.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ export interface Compute {
6767
action: Action;
6868
}) => Promise<void>;
6969

70+
// Get servers across potentially different projects by their global unique id.
71+
// Use the fields parameter to restrict to a much smaller subset of information
72+
// about each server (e.g., just the state field). Caller must be a collaborator
73+
// on each project containing the servers.
74+
// If you give an id of a server that doesn't exist, it'll just be excluded in the result.
75+
// Similarly, if you give a field that doesn't exist, it is excluded.
76+
// The order of the returned servers and count probably will NOT match that in
77+
// ids, so you should include 'id' in fields.
7078
getServersById: (opts: {
7179
account_id?: string;
7280
ids: number[];

src/packages/frontend/compute/api.ts

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import api from "@cocalc/frontend/client/api";
21
import type {
3-
Action,
4-
Cloud,
52
ComputeServerTemplate,
63
ComputeServerUserInfo,
74
Configuration,
@@ -16,94 +13,76 @@ import type {
1613
} from "@cocalc/util/compute/templates";
1714
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
1815
import TTL from "@isaacs/ttlcache";
16+
import { webapp_client } from "@cocalc/frontend/webapp-client";
17+
import { type Compute } from "@cocalc/conat/hub/api/compute";
1918

20-
export async function createServer(opts: {
21-
project_id: string;
22-
title?: string;
23-
color?: string;
24-
autorestart?: boolean;
25-
cloud?: Cloud;
26-
configuration?: Configuration;
27-
notes?: string;
28-
course_project_id?: string;
29-
course_server_id?: number;
30-
}): Promise<number> {
31-
return await api("compute/create-server", opts);
19+
function compute(): Compute {
20+
return webapp_client.conat_client.hub.compute;
3221
}
3322

34-
export async function computeServerAction(opts: {
35-
id: number;
36-
action: Action;
37-
}) {
38-
await api("compute/compute-server-action", opts);
23+
export async function createServer(opts): Promise<number> {
24+
return await compute().createServer(opts);
3925
}
4026

41-
// Get servers across potentially different projects by their global unique id.
42-
// Use the fields parameter to restrict to a much smaller subset of information
43-
// about each server (e.g., just the state field). Caller must be a collaborator
44-
// on each project containing the servers.
45-
// If you give an id of a server that doesn't exist, it'll just be excluded in the result.
46-
// Similarly, if you give a field that doesn't exist, it is excluded.
47-
// The order of the returned servers and count probably will NOT match that in
48-
// ids, so you should include 'id' in fields.
49-
export async function getServersById(opts: {
50-
ids: number[];
51-
fields?: string[];
52-
}): Promise<Partial<ComputeServerUserInfo>[]> {
53-
return await api("compute/get-servers-by-id", opts);
27+
export async function computeServerAction(opts): Promise<void> {
28+
await compute().computeServerAction(opts);
5429
}
5530

56-
export async function getServers(opts: {
57-
id?: number;
58-
project_id: string;
59-
}): Promise<ComputeServerUserInfo[]> {
60-
return await api("compute/get-servers", opts);
31+
export async function getServersById(
32+
opts,
33+
): Promise<Partial<ComputeServerUserInfo>[]> {
34+
return await compute().getServersById(opts);
6135
}
6236

63-
export async function getServerState(id: number) {
64-
return await api("compute/get-server-state", { id });
37+
export async function getServers(opts): Promise<ComputeServerUserInfo[]> {
38+
return await compute().getServers(opts);
6539
}
6640

67-
export async function getSerialPortOutput(id: number) {
68-
return await api("compute/get-serial-port-output", { id });
41+
export async function getServerState(
42+
id: number,
43+
): Promise<ComputeServerUserInfo["state"]> {
44+
return await compute().getServerState({ id });
45+
}
46+
47+
export async function getSerialPortOutput(id: number): Promise<string> {
48+
return await compute().getSerialPortOutput({ id });
6949
}
7050

7151
export async function deleteServer(id: number) {
72-
return await api("compute/delete-server", { id });
52+
return await compute().deleteServer({ id });
7353
}
7454

7555
export async function isDnsAvailable(dns: string) {
76-
const { isAvailable } = await api("compute/is-dns-available", { dns });
77-
return isAvailable;
56+
return await compute().isDnsAvailable({ dns });
7857
}
7958

8059
export async function undeleteServer(id: number) {
81-
await api("compute/undelete-server", { id });
60+
return await compute().undeleteServer({ id });
8261
}
8362

8463
// only owner can change properties of a compute server.
8564

8665
export async function setServerColor(opts: { id: number; color: string }) {
87-
return await api("compute/set-server-color", opts);
66+
await compute().setServerColor(opts);
8867
}
8968

9069
export async function setServerTitle(opts: { id: number; title: string }) {
91-
return await api("compute/set-server-title", opts);
70+
await compute().setServerTitle(opts);
9271
}
9372

9473
export async function setServerConfiguration(opts: {
9574
id: number;
9675
configuration: Partial<Configuration>;
9776
}) {
98-
return await api("compute/set-server-configuration", opts);
77+
await compute().setServerConfiguration(opts);
9978
}
10079

10180
// only for admins!
10281
export async function setTemplate(opts: {
10382
id: number;
10483
template: ComputeServerTemplate;
10584
}) {
106-
return await api("compute/set-template", opts);
85+
await compute().setTemplate(opts);
10786
}
10887

10988
// 5-minute client side ttl cache of all and specific template, since
@@ -115,7 +94,7 @@ export async function getTemplate(id: number): Promise<ConfigurationTemplate> {
11594
if (templatesCache.has(id)) {
11695
return templatesCache.get(id)!;
11796
}
118-
const x = await api("compute/get-template", { id });
97+
const x = await compute().getTemplate({ id });
11998
templatesCache.set(id, x);
12099
return x;
121100
}
@@ -124,20 +103,20 @@ export async function getTemplates(): Promise<ConfigurationTemplates> {
124103
if (templatesCache.has("templates")) {
125104
return templatesCache.get("templates")!;
126105
}
127-
const x = await api("compute/get-templates");
106+
const x = await compute().getTemplates();
128107
templatesCache.set("templates", x);
129108
return x;
130109
}
131110

132111
export async function setServerCloud(opts: { id: number; cloud: string }) {
133-
return await api("compute/set-server-cloud", opts);
112+
await compute().setServerCloud(opts);
134113
}
135114

136115
export async function setServerOwner(opts: {
137116
id: number;
138117
new_account_id: string;
139118
}) {
140-
return await api("compute/set-server-owner", opts);
119+
await compute().setServerOwner(opts);
141120
}
142121

143122
// Cache for 12 hours
@@ -149,7 +128,7 @@ export const getGoogleCloudPriceData = reuseInFlight(
149128
googleCloudPriceData == null ||
150129
Date.now() >= googleCloudPriceDataExpire
151130
) {
152-
googleCloudPriceData = await api("compute/google-cloud/get-pricing-data");
131+
googleCloudPriceData = await compute().getGoogleCloudPriceData();
153132
googleCloudPriceDataExpire = Date.now() + 1000 * 60 * 60 * 12; // 12 hour cache
154133
}
155134
if (googleCloudPriceData == null) {
@@ -186,7 +165,7 @@ export const getHyperstackPriceData = reuseInFlight(
186165
hyperstackPriceData == null ||
187166
Date.now() >= hyperstackPriceDataExpire
188167
) {
189-
hyperstackPriceData = await api("compute/get-hyperstack-pricing-data");
168+
hyperstackPriceData = await compute().getHyperstackPriceData();
190169
hyperstackPriceDataExpire = Date.now() + 1000 * 60 * 5; // 5 minute cache
191170
}
192171
if (hyperstackPriceData == null) {
@@ -203,22 +182,22 @@ export async function getNetworkUsage(opts: {
203182
start: Date;
204183
end: Date;
205184
}): Promise<{ amount: number; cost: number }> {
206-
return await api("compute/get-network-usage", opts);
185+
return await compute().getNetworkUsage(opts);
207186
}
208187

209188
// Get the current api key for a specific (on prem) server.
210189
// We only need this for on prem, so we are restricting to that right now.
211190
// If no key is allocated, one will be created.
212191
export async function getApiKey(opts: { id }): Promise<string> {
213-
return await api("compute/get-api-key", opts);
192+
return await compute().getApiKey(opts);
214193
}
215194
export async function deleteApiKey(opts: { id }): Promise<void> {
216-
await api("compute/delete-api-key", opts);
195+
await compute().deleteApiKey(opts);
217196
}
218197

219198
// Get the project log entries directly for just one compute server
220199
export async function getLog(opts: { id; type: "activity" | "files" }) {
221-
return await api("compute/get-log", opts);
200+
return await compute().getLog(opts);
222201
}
223202

224203
export const getTitle = reuseInFlight(
@@ -229,7 +208,7 @@ export const getTitle = reuseInFlight(
229208
color: string;
230209
project_specific_id: number;
231210
}> => {
232-
return await api("compute/get-server-title", opts);
211+
return await compute().getTitle(opts);
233212
},
234213
);
235214

@@ -243,7 +222,7 @@ export async function setDetailedState(opts: {
243222
timeout?: number;
244223
progress?: number;
245224
}) {
246-
return await api("compute/set-detailed-state", opts);
225+
await compute().setDetailedState(opts);
247226
}
248227

249228
// We cache images for 5 minutes.
@@ -271,7 +250,6 @@ function cacheGet(cloud) {
271250
function cacheSet(cloud, images) {
272251
imagesCache[cloud] = { images, timestamp: Date.now() };
273252
}
274-
275253
async function getImagesFor({
276254
cloud,
277255
endpoint,
@@ -286,11 +264,14 @@ async function getImagesFor({
286264
}
287265

288266
try {
289-
const images = await api(
290-
endpoint,
291-
// admin reload forces fetch data from github and/or google cloud - normal users just have their cache ignored above
292-
reload ? { noCache: true } : undefined,
293-
);
267+
let images;
268+
if (endpoint == "compute/get-images") {
269+
images = await compute().getImages({ noCache: !!reload });
270+
} else if (endpoint == "compute/get-images-google") {
271+
images = await compute().getGoogleCloudImages({ noCache: !!reload });
272+
} else {
273+
throw Error(`unknown endpoint ${endpoint}`);
274+
}
294275
cacheSet(cloud, images);
295276
return images;
296277
} catch (err) {
@@ -328,5 +309,5 @@ export async function setImageTested(opts: {
328309
id: number; // server id
329310
tested: boolean;
330311
}) {
331-
return await api("compute/set-image-tested", opts);
312+
await compute().setImageTested(opts);
332313
}

src/packages/server/compute/dns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export async function get({ name }: { name: string }) {
185185

186186
// Throw error if the given dns name is currently set for any compute server's configuration.
187187
// TODO: we may someday need an index on the configuration jsonb?
188-
export async function isDnsAvailable(dns: string) {
188+
export async function isDnsAvailable(dns: string): Promise<boolean> {
189189
try {
190190
checkValidDomain(dns);
191191
} catch (_) {

0 commit comments

Comments
 (0)