Skip to content

Commit 41ce5f9

Browse files
authored
Merge pull request #7623 from schrodingersket/openapi/compute
OpenAPI doc: Changed `query` spec field to `body` for Compute OpenAPI docs.
2 parents cd51357 + 8aa8459 commit 41ce5f9

40 files changed

+528
-608
lines changed

src/packages/next/lib/api/get-params.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
import type { Request } from "express";
22

3-
export default function getParams(
4-
req: Request,
5-
{ allowGet }: { allowGet?: boolean } = {}
6-
): { [param: string]: any } {
3+
export default function getParams(req: Request): { [param: string]: any } {
74
if (req?.method == "POST") {
85
return new Proxy(
96
{},
107
{
118
get(_, key) {
129
return req.body?.[key];
1310
},
14-
}
15-
);
16-
} else if (allowGet && req?.method == "GET") {
17-
// allowGet is NOT enabled by default, since this could lead to a sneaky click on a link attack.
18-
// Should only be enabled for dev purposes or for specific endpoints where making the api call
19-
// doesn't potential leak private information.
20-
return new Proxy(
21-
{},
22-
{
23-
get(_, key) {
24-
if (typeof key != "string") {
25-
return undefined;
26-
}
27-
return req.query?.[key];
28-
},
29-
}
11+
},
3012
);
3113
} else {
3214
// only support params for POST requests.

src/packages/next/lib/api/schema/compute/check-in.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@ import { z } from "../../framework";
22

33
import { FailedAPIOperationSchema } from "../common";
44

5-
import { ComputeServerIdBodySchema } from "./common";
5+
import { ComputeServerIdSchema } from "./common";
66

7-
const VpnSha1Schema = z
8-
.string()
9-
.describe("SHA1 VPN hash")
10-
.optional();
7+
const VpnSha1Schema = z.string().describe("SHA1 VPN hash").optional();
118

12-
const StorageSha1Schema = z
13-
.string()
14-
.describe("SHA-1 storage hash")
15-
.optional();
9+
const StorageSha1Schema = z.string().describe("SHA-1 storage hash").optional();
1610

1711
// OpenAPI spec
1812
//
1913
export const ComputeServerCheckInInputSchema = z
2014
.object({
21-
id: ComputeServerIdBodySchema,
15+
id: ComputeServerIdSchema,
2216
vpn_sha1: VpnSha1Schema,
2317
storage_sha1: StorageSha1Schema,
2418
})
@@ -30,20 +24,19 @@ export const ComputeServerCheckInInputSchema = z
3024
export const ComputeServerCheckInOutputSchema = z.union([
3125
FailedAPIOperationSchema,
3226
z.object({
33-
vpn: z
34-
.object({
35-
image: z
36-
.string()
37-
.describe("VPN image name and tag."),
38-
nodes: z.array(z.object({
39-
})),
40-
}),
41-
storage: z.array(z.object({
42-
})),
27+
vpn: z.object({
28+
image: z.string().describe("VPN image name and tag."),
29+
nodes: z.array(z.object({})),
30+
}),
31+
storage: z.array(z.object({})),
4332
vpn_sha1: VpnSha1Schema,
4433
storage_sha1: StorageSha1Schema,
4534
}),
4635
]);
4736

48-
export type ComputeServerCheckInInput = z.infer<typeof ComputeServerCheckInInputSchema>;
49-
export type ComputeServerCheckInOutput = z.infer<typeof ComputeServerCheckInOutputSchema>;
37+
export type ComputeServerCheckInInput = z.infer<
38+
typeof ComputeServerCheckInInputSchema
39+
>;
40+
export type ComputeServerCheckInOutput = z.infer<
41+
typeof ComputeServerCheckInOutputSchema
42+
>;
Lines changed: 93 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { z } from "../../framework";
22

3-
export const ComputeServerIdBodySchema = z
3+
export const ComputeServerIdSchema = z
44
.number()
55
.int()
66
.min(0)
7-
.describe("**Integer** compute server id.");
7+
.describe("Compute server id.");
88

9-
export const ComputeServerIdQueryParamSchema = z
10-
.string()
11-
.describe("**String** compute server id.");
12-
13-
export const ServerImageNoCacheQueryParamSchema = z
14-
.object({
15-
noCache: z
16-
.string()
17-
.describe("**Administrators only**. Disables database caching for this query.")
18-
.optional(),
19-
});
9+
export const ServerImageNoCacheSchema = z.object({
10+
noCache: z
11+
.string()
12+
.describe(
13+
"**Administrators only**. Disables database caching for this query.",
14+
)
15+
.optional(),
16+
});
2017

2118
export const ComputeServerStateSchema = z
2219
.enum([
@@ -31,129 +28,102 @@ export const ComputeServerStateSchema = z
3128
])
3229
.describe("The state of the compute server.");
3330

34-
export const ComputeServerColorSchema = z
35-
.string()
36-
.describe(
37-
`Compute server color in rgb(#,#,#) format. Used for color-coding compute servers in
31+
export const ComputeServerColorSchema = z.string().describe(
32+
`Compute server color in \`rgb(#,#,#)\` format. Used for color-coding compute servers in
3833
the CoCalc UI.`,
39-
);
34+
);
4035

41-
export const ComputeServerTitleSchema = z
42-
.string()
43-
.describe(
44-
`Title of this compute server. Used purely to make it easier for the user to keep
45-
track of it.`
46-
);
36+
export const ComputeServerTitleSchema = z.string().describe(
37+
`Title of this compute server. Used purely to make it easier for the user to keep
38+
track of it.`,
39+
);
4740

4841
export const ComputeServerCloudSchema = z
49-
.enum([
50-
"google-cloud",
51-
"hyperstack",
52-
"onprem",
53-
])
42+
.enum(["google-cloud", "hyperstack", "onprem"])
5443
.describe("The cloud provider used to run this compute server");
5544

56-
export const ComputeServerImageProxySchema = z
57-
.object({
58-
path: z
59-
.string(),
60-
target: z
61-
.string(),
62-
ws: z
63-
.boolean()
64-
.optional(),
65-
app: z
66-
.string()
67-
.optional(),
68-
name: z
69-
.string()
70-
.optional(),
71-
})
45+
export const ComputeServerImageProxySchema = z.object({
46+
path: z.string(),
47+
target: z.string(),
48+
ws: z.boolean().optional(),
49+
app: z.string().optional(),
50+
name: z.string().optional(),
51+
});
7252

73-
export const GoogleCloudServerConfigurationSchema = z
74-
.object({});
53+
export const GoogleCloudServerConfigurationSchema = z.object({});
7554

76-
export const HyperstackServerConfigurationSchema = z
77-
.object({});
55+
export const HyperstackServerConfigurationSchema = z.object({});
7856

79-
export const ServerConfigurationSchema = z
80-
.union([
81-
GoogleCloudServerConfigurationSchema,
82-
HyperstackServerConfigurationSchema,
83-
]);
57+
export const ServerConfigurationSchema = z.union([
58+
GoogleCloudServerConfigurationSchema,
59+
HyperstackServerConfigurationSchema,
60+
]);
8461

85-
export const BaseServerConfigurationSchema = z
86-
.object({
87-
cloud: ComputeServerCloudSchema,
88-
dns: z
89-
.string()
90-
.describe("DNS name"),
91-
spot: z
92-
.boolean()
93-
.describe("If true, provision a spot instance."),
94-
zone: z
95-
.string()
96-
.describe("Cloud provider zone to which this template defaults."),
97-
image: z
98-
.string()
99-
.describe("Compute server template image name."),
100-
region: z
101-
.string()
102-
.describe("Compute server template region name."),
103-
ephemeral: z
104-
.boolean()
105-
.describe("Indicates whether the compute server is ephemeral.")
106-
.optional(),
107-
diskType: z
108-
.string()
109-
.describe("Compute server template disk type."),
110-
diskSizeGb: z
111-
.number()
112-
.min(0)
113-
.describe("Compute server template disk image size in GB."),
114-
externalIp: z
115-
.boolean()
116-
.describe(
117-
`When true, the compute server is configured with an external IP address.`
118-
),
119-
tag_cocalc: z
120-
.string()
121-
.describe("CoCalc tag"),
122-
machineType: z
123-
.string()
124-
.describe(
125-
"Cloud-specific machine type for this template (e.g., `t2d-standard-1`)."
126-
),
127-
excludeFromSync: z
128-
.array(z.string())
129-
.describe("Array of top level directories to exclude from sync."),
130-
acceleratorType: z
131-
.string()
132-
.describe(
133-
"Number of hardware accelerators to be provisioned to this server."
134-
)
135-
.optional(),
136-
acceleratorCount: z
137-
.number()
138-
.int()
139-
.min(0)
140-
.describe(
141-
"Number of hardware accelerators to be provisioned with this server."
142-
)
143-
.optional(),
144-
proxy: ComputeServerImageProxySchema
145-
.optional()
146-
});
62+
export const BaseServerConfigurationSchema = z.object({
63+
cloud: ComputeServerCloudSchema,
64+
dns: z.string().describe("DNS name"),
65+
spot: z.boolean().describe("If true, provision a spot instance."),
66+
zone: z
67+
.string()
68+
.describe("Cloud provider zone to which this template defaults."),
69+
image: z.string().describe("Compute server template image name."),
70+
region: z.string().describe("Compute server template region name."),
71+
ephemeral: z
72+
.boolean()
73+
.describe("Indicates whether the compute server is ephemeral.")
74+
.optional(),
75+
diskType: z.string().describe("Compute server template disk type."),
76+
diskSizeGb: z
77+
.number()
78+
.min(0)
79+
.describe("Compute server template disk image size in GB."),
80+
externalIp: z
81+
.boolean()
82+
.describe(
83+
`When true, the compute server is configured with an external IP address.`,
84+
),
85+
tag_cocalc: z.string().describe("CoCalc tag"),
86+
machineType: z
87+
.string()
88+
.describe(
89+
"Cloud-specific machine type for this template (e.g., `t2d-standard-1`).",
90+
),
91+
excludeFromSync: z
92+
.array(z.string())
93+
.describe("Array of top level directories to exclude from sync."),
94+
acceleratorType: z
95+
.string()
96+
.describe(
97+
"Number of hardware accelerators to be provisioned to this server.",
98+
)
99+
.optional(),
100+
acceleratorCount: z
101+
.number()
102+
.int()
103+
.min(0)
104+
.describe(
105+
"Number of hardware accelerators to be provisioned with this server.",
106+
)
107+
.optional(),
108+
proxy: ComputeServerImageProxySchema.optional(),
109+
});
147110

148-
export type BaseServerConfiguration = z.infer<typeof BaseServerConfigurationSchema>;
149-
export type ComputeServerBodyId = z.infer<typeof ComputeServerIdBodySchema>;
111+
export type BaseServerConfiguration = z.infer<
112+
typeof BaseServerConfigurationSchema
113+
>;
114+
export type ComputeServerBodyId = z.infer<typeof ComputeServerIdSchema>;
150115
export type ComputeServerCloud = z.infer<typeof ComputeServerCloudSchema>;
151116
export type ComputeServerColor = z.infer<typeof ComputeServerColorSchema>;
152-
export type ComputeServerImageProxy = z.infer<typeof ComputeServerImageProxySchema>;
153-
export type ComputeServerQueryParamId = z.infer<typeof ComputeServerIdBodySchema>;
117+
export type ComputeServerImageProxy = z.infer<
118+
typeof ComputeServerImageProxySchema
119+
>;
154120
export type ComputeServerState = z.infer<typeof ComputeServerStateSchema>;
155121
export type ComputeServerTitle = z.infer<typeof ComputeServerTitleSchema>;
156-
export type GoogleCloudServerConfiguration = z.infer<typeof GoogleCloudServerConfigurationSchema>;
157-
export type HyperstackServerConfiguration = z.infer<typeof HyperstackServerConfigurationSchema>;
122+
export type GoogleCloudServerConfiguration = z.infer<
123+
typeof GoogleCloudServerConfigurationSchema
124+
>;
125+
export type HyperstackServerConfiguration = z.infer<
126+
typeof HyperstackServerConfigurationSchema
127+
>;
158128
export type ServerConfiguration = z.infer<typeof ServerConfigurationSchema>;
159-
export type ServerImageNoCache = z.infer<typeof ServerImageNoCacheQueryParamSchema>;
129+
export type ServerImageNoCache = z.infer<typeof ServerImageNoCacheSchema>;

src/packages/next/lib/api/schema/compute/compute-server-action.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@ import { z } from "../../framework";
22

33
import { FailedAPIOperationSchema } from "../common";
44

5-
import { ComputeServerIdBodySchema } from "./common";
5+
import { ComputeServerIdSchema } from "./common";
66

77
// OpenAPI spec
88
//
99
export const ComputeServerActionInputSchema = z
1010
.object({
11-
id: ComputeServerIdBodySchema,
11+
id: ComputeServerIdSchema,
1212
action: z
13-
.enum([
14-
"start",
15-
"stop",
16-
"reboot",
17-
"suspend",
18-
"resume",
19-
"deprovision",
20-
])
13+
.enum(["start", "stop", "reboot", "suspend", "resume", "deprovision"])
2114
.describe("Action to be performed on the compute server."),
2215
})
2316
.describe(
24-
"Perform various action on a specific compute server (e.g., power off, deprovision, etc.)."
17+
"Perform various action on a specific compute server (e.g., power off, deprovision, etc.).",
2518
);
2619

2720
export const ComputeServerActionOutputSchema = z.union([
@@ -35,5 +28,9 @@ export const ComputeServerActionOutputSchema = z.union([
3528
}),
3629
]);
3730

38-
export type ComputeServerActionInput = z.infer<typeof ComputeServerActionInputSchema>;
39-
export type ComputeServerActionOutput = z.infer<typeof ComputeServerActionOutputSchema>;
31+
export type ComputeServerActionInput = z.infer<
32+
typeof ComputeServerActionInputSchema
33+
>;
34+
export type ComputeServerActionOutput = z.infer<
35+
typeof ComputeServerActionOutputSchema
36+
>;

0 commit comments

Comments
 (0)