CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/compute/set-detailed-state.ts
Views: 687
1
/*
2
Set the state of a component. This is mainly used from the backend to convey information to user
3
about what is going on in a compute server.
4
5
Example use, where 'sk-eTUKbl2lkP9TgvFJ00001n' is a project api key.
6
7
curl -sk -u sk-eTUKbl2lkP9TgvFJ00001n: -d '{"id":"13","name":"foo","value":"bar389"}' -H 'Content-Type: application/json' https://cocalc.com/api/v2/compute/set-detailed-state
8
*/
9
10
import getProjectOrAccountId from "lib/account/get-account";
11
import setDetailedState from "@cocalc/server/compute/set-detailed-state";
12
import getParams from "lib/api/get-params";
13
import isCollaborator from "@cocalc/server/projects/is-collaborator";
14
15
import { apiRoute, apiRouteOperation } from "lib/api";
16
import { OkStatus } from "lib/api/status";
17
import {
18
SetDetailedServerStateInputSchema,
19
SetDetailedServerStateOutputSchema,
20
} from "lib/api/schema/compute/set-detailed-state";
21
22
async function handle(req, res) {
23
try {
24
res.json(await get(req));
25
} catch (err) {
26
res.json({ error: `${err.message}` });
27
return;
28
}
29
}
30
31
async function get(req) {
32
// This is a bit complicated because it can be used by a project api key,
33
// in which case project_id must not be passed in, or it can be auth'd
34
// by a normal api key or account, in which case project_id must be passed in.
35
// TODO: I don't think this is ever in practice used by anything but a project -- maybe by
36
// account just for testing?
37
const project_or_account_id = await getProjectOrAccountId(req);
38
if (!project_or_account_id) {
39
throw Error("invalid auth");
40
}
41
const {
42
id,
43
name,
44
state,
45
extra,
46
timeout,
47
progress,
48
project_id: project_id0,
49
} = getParams(req);
50
51
let project_id;
52
if (!project_id0) {
53
project_id = project_or_account_id;
54
} else {
55
if (
56
!(await isCollaborator({
57
account_id: project_or_account_id,
58
project_id: project_id0,
59
}))
60
) {
61
throw Error("must be a collaborator on project with compute server");
62
}
63
project_id = project_id0;
64
}
65
66
await setDetailedState({
67
project_id,
68
id,
69
name,
70
state,
71
extra,
72
timeout,
73
progress,
74
});
75
return OkStatus;
76
}
77
78
export default apiRoute({
79
setDetailedState: apiRouteOperation({
80
method: "POST",
81
openApiOperation: {
82
tags: ["Compute"],
83
},
84
})
85
.input({
86
contentType: "application/json",
87
body: SetDetailedServerStateInputSchema,
88
})
89
.outputs([
90
{
91
status: 200,
92
contentType: "application/json",
93
body: SetDetailedServerStateOutputSchema,
94
},
95
])
96
.handler(handle),
97
});
98
99