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-template.ts
Views: 687
1
/*
2
Set a specific compute server template by id. This operation is designed for
3
administrators only.
4
*/
5
6
import getAccountId from "lib/account/get-account";
7
import { setTemplate } from "@cocalc/server/compute/templates";
8
import getParams from "lib/api/get-params";
9
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
10
11
import { apiRoute, apiRouteOperation } from "lib/api";
12
import { OkStatus } from "lib/api/status";
13
import {
14
SetComputeServerTemplateInputSchema,
15
SetComputeServerTemplateOutputSchema,
16
} from "lib/api/schema/compute/set-template";
17
18
async function handle(req, res) {
19
try {
20
res.json(await get(req));
21
} catch (err) {
22
res.json({ error: `${err.message}` });
23
return;
24
}
25
}
26
27
async function get(req) {
28
const account_id = await getAccountId(req);
29
if (!account_id) {
30
throw Error("must be signed in");
31
}
32
if (!(await userIsInGroup(account_id, "admin"))) {
33
// admin only functionality for now.
34
throw Error(
35
"only admin are allowed to set compute server configuration templates",
36
);
37
}
38
const { id, template } = getParams(req);
39
await setTemplate({ account_id, id, template });
40
return OkStatus;
41
}
42
43
export default apiRoute({
44
setTemplate: apiRouteOperation({
45
method: "POST",
46
openApiOperation: {
47
tags: ["Compute", "Admin"],
48
},
49
})
50
.input({
51
contentType: "application/json",
52
body: SetComputeServerTemplateInputSchema,
53
})
54
.outputs([
55
{
56
status: 200,
57
contentType: "application/json",
58
body: SetComputeServerTemplateOutputSchema,
59
},
60
])
61
.handler(handle),
62
});
63
64