Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/projects/set-admin-quotas.ts
1698 views
1
/*
2
API endpoint to set project quotas as admin.
3
4
This requires the user to be an admin.
5
*/
6
7
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
8
import { setQuotas } from "@cocalc/server/conat/api/projects";
9
import getAccountId from "lib/account/get-account";
10
import { apiRoute, apiRouteOperation } from "lib/api";
11
import getParams from "lib/api/get-params";
12
import {
13
SetAdminQuotasInputSchema,
14
SetAdminQuotasOutputSchema,
15
} from "lib/api/schema/projects/set-admin-quotas";
16
import { SuccessStatus } from "lib/api/status";
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 == null) {
30
throw Error("must be signed in");
31
}
32
// This user MUST be an admin:
33
if (!(await userIsInGroup(account_id, "admin"))) {
34
throw Error("only admins can set project quotas");
35
}
36
37
const {
38
project_id,
39
memory_limit,
40
memory_request,
41
cpu_request,
42
cpu_limit,
43
disk_quota,
44
idle_timeout,
45
internet,
46
member_host,
47
always_running,
48
} = getParams(req);
49
50
await setQuotas({
51
account_id,
52
project_id,
53
memory: memory_limit,
54
memory_request,
55
cpu_shares:
56
cpu_request != null ? Math.round(cpu_request * 1024) : undefined,
57
cores: cpu_limit,
58
disk_quota,
59
mintime: idle_timeout,
60
network: internet != null ? (internet ? 1 : 0) : undefined,
61
member_host: member_host != null ? (member_host ? 1 : 0) : undefined,
62
always_running:
63
always_running != null ? (always_running ? 1 : 0) : undefined,
64
});
65
66
return SuccessStatus;
67
}
68
69
export default apiRoute({
70
setAdminQuotas: apiRouteOperation({
71
method: "POST",
72
openApiOperation: {
73
tags: ["Projects", "Admin"],
74
},
75
})
76
.input({
77
contentType: "application/json",
78
body: SetAdminQuotasInputSchema,
79
})
80
.outputs([
81
{
82
status: 200,
83
contentType: "application/json",
84
body: SetAdminQuotasOutputSchema,
85
},
86
])
87
.handler(handle),
88
});
89
90