[FEATURE] Initial Mutability Features
This commit is contained in:
parent
eb53e56dc7
commit
ea54ee6239
6 changed files with 61 additions and 43 deletions
|
@ -13,28 +13,10 @@ const dnsRegex = new RegExp(
|
||||||
`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`,
|
`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`,
|
||||||
);
|
);
|
||||||
|
|
||||||
function payloadFilter(req, res) {
|
function backupPayloadFilter(req, res) {
|
||||||
const serverSpec = req.body;
|
const serverSpec = req.body;
|
||||||
if (!serverSpec) return res.sendStatus(400);
|
|
||||||
const { name, host, version, serverType, memory, extraPorts } = serverSpec;
|
|
||||||
const { backupHost, backupBucket, backupId, backupKey, backupInterval } =
|
const { backupHost, backupBucket, backupId, backupKey, backupInterval } =
|
||||||
serverSpec;
|
serverSpec;
|
||||||
console.log("GOT VVV");
|
|
||||||
console.log(serverSpec);
|
|
||||||
if (!name) return res.status(400).send("Server name is required!");
|
|
||||||
if (!host) return res.status(400).send("Server host is required!");
|
|
||||||
if (!dnsRegex.test(host)) return res.status(400).send("Hostname invalid!");
|
|
||||||
if (!version) return res.status(400).send("Server version is required!");
|
|
||||||
if (!serverType) return res.status(400).send("Server type is required!");
|
|
||||||
if (!memory) return res.status(400).send("Memory is required!");
|
|
||||||
if (
|
|
||||||
!!extraPorts &&
|
|
||||||
(!Array.isArray(extraPorts) ||
|
|
||||||
extraPorts.find((e) => typeof e !== "string" || e.length > 5))
|
|
||||||
)
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.send("Extra ports must be a list of strings with length of 5!");
|
|
||||||
// TODO: Impliment non creation time backups
|
// TODO: Impliment non creation time backups
|
||||||
if (
|
if (
|
||||||
!!backupHost ||
|
!!backupHost ||
|
||||||
|
@ -60,6 +42,27 @@ function payloadFilter(req, res) {
|
||||||
return "filtered";
|
return "filtered";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function payloadFilter(req, res) {
|
||||||
|
const serverSpec = req.body;
|
||||||
|
if (!serverSpec) return res.sendStatus(400);
|
||||||
|
const { name, host, version, serverType, memory, extraPorts } = serverSpec;
|
||||||
|
if (!name) return res.status(400).send("Server name is required!");
|
||||||
|
if (!host) return res.status(400).send("Server host is required!");
|
||||||
|
if (!dnsRegex.test(host)) return res.status(400).send("Hostname invalid!");
|
||||||
|
if (!version) return res.status(400).send("Server version is required!");
|
||||||
|
if (!serverType) return res.status(400).send("Server type is required!");
|
||||||
|
if (!memory) return res.status(400).send("Memory is required!");
|
||||||
|
if (
|
||||||
|
!!extraPorts &&
|
||||||
|
(!Array.isArray(extraPorts) ||
|
||||||
|
extraPorts.find((e) => typeof e !== "string" || e.length > 5))
|
||||||
|
)
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.send("Extra ports must be a list of strings with length of 5!");
|
||||||
|
return "filtered";
|
||||||
|
}
|
||||||
|
|
||||||
function checkServerId(serverSpec) {
|
function checkServerId(serverSpec) {
|
||||||
if (!serverSpec) throw new ExpressClientError({ c: 400 });
|
if (!serverSpec) throw new ExpressClientError({ c: 400 });
|
||||||
if (!serverSpec.id)
|
if (!serverSpec.id)
|
||||||
|
@ -68,6 +71,7 @@ function checkServerId(serverSpec) {
|
||||||
|
|
||||||
export async function createServer(req, res) {
|
export async function createServer(req, res) {
|
||||||
if (payloadFilter(req, res) !== "filtered") return;
|
if (payloadFilter(req, res) !== "filtered") return;
|
||||||
|
if (backupPayloadFilter(req, res) !== "filtered") return;
|
||||||
const serverSpec = req.body;
|
const serverSpec = req.body;
|
||||||
try {
|
try {
|
||||||
const serverEntry = await createServerEntry(serverSpec);
|
const serverEntry = await createServerEntry(serverSpec);
|
||||||
|
@ -130,7 +134,12 @@ export async function getServer(req, res) {
|
||||||
return sendError(res)(e);
|
return sendError(res)(e);
|
||||||
}
|
}
|
||||||
const { id } = serverSpec;
|
const { id } = serverSpec;
|
||||||
getServerEntry(id).then((s) => res.json(s));
|
getServerEntry(id).then((s) => {
|
||||||
|
delete s.backupKey;
|
||||||
|
s.backupBucket = s.backupPath;
|
||||||
|
delete s.backupPath;
|
||||||
|
res.json(s);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function modifyServer(req, res) {
|
export async function modifyServer(req, res) {
|
||||||
|
@ -139,8 +148,6 @@ export async function modifyServer(req, res) {
|
||||||
try {
|
try {
|
||||||
checkServerId(serverSpec);
|
checkServerId(serverSpec);
|
||||||
const serverEntry = await modifyServerEntry(serverSpec);
|
const serverEntry = await modifyServerEntry(serverSpec);
|
||||||
console.log("NEW ENTRY");
|
|
||||||
console.log(serverEntry);
|
|
||||||
// await createServerResources(serverEntry);
|
// await createServerResources(serverEntry);
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -35,7 +35,7 @@ export async function createServerEntry(serverSpec) {
|
||||||
server_type,
|
server_type,
|
||||||
memory,
|
memory,
|
||||||
extra_ports,
|
extra_ports,
|
||||||
backup_enabled: !!backup_interval, // We already verified the payload, so any backup key will work
|
backup_enabled: !!backup_interval ? true : null, // We already verified the payload, so any backup key will work
|
||||||
backup_host,
|
backup_host,
|
||||||
backup_bucket_path,
|
backup_bucket_path,
|
||||||
backup_id,
|
backup_id,
|
||||||
|
|
|
@ -6,7 +6,7 @@ export default function BackupBucketOption(props) {
|
||||||
<TextField
|
<TextField
|
||||||
label="Bucket Path"
|
label="Bucket Path"
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
defaultValue={value}
|
value={value}
|
||||||
helperText="Example: /minecraft-backups/example-backups"
|
helperText="Example: /minecraft-backups/example-backups"
|
||||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||||
required
|
required
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import TextField from "@mui/material/TextField";
|
import TextField from "@mui/material/TextField";
|
||||||
export default function BackupHostOption(props) {
|
export default function BackupHostOption(props) {
|
||||||
const { onChange } = props;
|
const { value, onChange } = props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TextField
|
<TextField
|
||||||
label="Backup Host"
|
label="Backup Host"
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
|
value={value}
|
||||||
helperText="Example: s3.mydomain.com"
|
helperText="Example: s3.mydomain.com"
|
||||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||||
required
|
required
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import TextField from "@mui/material/TextField";
|
import TextField from "@mui/material/TextField";
|
||||||
export default function BackupIdOption(props) {
|
export default function BackupIdOption(props) {
|
||||||
const { onChange } = props;
|
const { value, onChange } = props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TextField
|
<TextField
|
||||||
label="S3 Access Key ID"
|
label="S3 Access Key ID"
|
||||||
|
value={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
helperText="Example: s3-access-key-id"
|
helperText="Example: s3-access-key-id"
|
||||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||||
|
|
|
@ -33,12 +33,15 @@ import BackupIntervalOption, {
|
||||||
|
|
||||||
export default function EditCoreOptions(props) {
|
export default function EditCoreOptions(props) {
|
||||||
const { serverId } = props;
|
const { serverId } = props;
|
||||||
const [backupEnabled, setBackupEnabled] = useState(false);
|
|
||||||
const [spec, setSpec] = useState();
|
const [spec, setSpec] = useState();
|
||||||
const modifyServer = useModifyServer(spec);
|
const modifyServer = useModifyServer(spec);
|
||||||
const { isLoading, data: serverBlueprint } = useGetServer(serverId);
|
const { isLoading, data: serverBlueprint } = useGetServer(serverId);
|
||||||
|
|
||||||
useEffect(() => setSpec(serverBlueprint), [serverBlueprint]);
|
useEffect(() => {
|
||||||
|
setSpec(serverBlueprint);
|
||||||
|
console.log("PUTTING BLUEPRINT:");
|
||||||
|
console.log(serverBlueprint);
|
||||||
|
}, [serverBlueprint]);
|
||||||
|
|
||||||
const updateSpec = (attr, val) => {
|
const updateSpec = (attr, val) => {
|
||||||
const s = { ...spec };
|
const s = { ...spec };
|
||||||
|
@ -49,10 +52,12 @@ export default function EditCoreOptions(props) {
|
||||||
const coreUpdate = (attr) => (e) => updateSpec(attr, e.target.value);
|
const coreUpdate = (attr) => (e) => updateSpec(attr, e.target.value);
|
||||||
|
|
||||||
const upsertSpec = () => {
|
const upsertSpec = () => {
|
||||||
console.log(spec);
|
|
||||||
modifyServer(spec);
|
modifyServer(spec);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const toggleBackupEnabled = () =>
|
||||||
|
updateSpec("backupEnabled", !spec.backupEnabled);
|
||||||
|
|
||||||
function validateSpec() {
|
function validateSpec() {
|
||||||
console.log("TODO CREATE VALIDATION");
|
console.log("TODO CREATE VALIDATION");
|
||||||
if (!spec.host) return alertValidationError("Host cannot be blank");
|
if (!spec.host) return alertValidationError("Host cannot be blank");
|
||||||
|
@ -83,18 +88,22 @@ export default function EditCoreOptions(props) {
|
||||||
<MemoryOption value={spec.memory} onChange={coreUpdate("memory")} />
|
<MemoryOption value={spec.memory} onChange={coreUpdate("memory")} />
|
||||||
|
|
||||||
<ExtraPortsOption extraPorts={spec.extraPorts} onChange={updateSpec} />
|
<ExtraPortsOption extraPorts={spec.extraPorts} onChange={updateSpec} />
|
||||||
<FormControlLabel
|
{spec.backupEnabled !== null && (
|
||||||
control={
|
<FormControlLabel
|
||||||
<Switch
|
control={
|
||||||
checked={backupEnabled}
|
<Switch
|
||||||
inputProps={{ "aria-label": "controlled" }}
|
checked={spec.backupEnabled}
|
||||||
/>
|
inputProps={{ "aria-label": "controlled" }}
|
||||||
}
|
onClick={toggleBackupEnabled}
|
||||||
label="Enable Backups?"
|
/>
|
||||||
labelPlacement="start"
|
}
|
||||||
sx={{ mr: "auto" }}
|
label="Enable Backups?"
|
||||||
/>
|
labelPlacement="start"
|
||||||
{backupEnabled && (
|
sx={{ mr: "auto" }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/*spec.backupEnabled && ( // TODO: Disabled while secrets are insecure
|
||||||
<FormControl
|
<FormControl
|
||||||
fullWidth
|
fullWidth
|
||||||
sx={{ mt: "2rem", display: "flex", gap: ".5rem" }}
|
sx={{ mt: "2rem", display: "flex", gap: ".5rem" }}
|
||||||
|
@ -118,10 +127,10 @@ export default function EditCoreOptions(props) {
|
||||||
/>
|
/>
|
||||||
<BackupIntervalOption onChange={coreUpdate("backupInterval")} />
|
<BackupIntervalOption onChange={coreUpdate("backupInterval")} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
)}
|
)*/}
|
||||||
|
|
||||||
<Button onClick={upsertSpec} variant="contained">
|
<Button onClick={upsertSpec} variant="contained">
|
||||||
Create
|
Save Changes
|
||||||
</Button>
|
</Button>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue