[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]))*$`,
|
||||
);
|
||||
|
||||
function payloadFilter(req, res) {
|
||||
function backupPayloadFilter(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
const { name, host, version, serverType, memory, extraPorts } = serverSpec;
|
||||
const { backupHost, backupBucket, backupId, backupKey, backupInterval } =
|
||||
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
|
||||
if (
|
||||
!!backupHost ||
|
||||
|
@ -60,6 +42,27 @@ function payloadFilter(req, res) {
|
|||
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) {
|
||||
if (!serverSpec) throw new ExpressClientError({ c: 400 });
|
||||
if (!serverSpec.id)
|
||||
|
@ -68,6 +71,7 @@ function checkServerId(serverSpec) {
|
|||
|
||||
export async function createServer(req, res) {
|
||||
if (payloadFilter(req, res) !== "filtered") return;
|
||||
if (backupPayloadFilter(req, res) !== "filtered") return;
|
||||
const serverSpec = req.body;
|
||||
try {
|
||||
const serverEntry = await createServerEntry(serverSpec);
|
||||
|
@ -130,7 +134,12 @@ export async function getServer(req, res) {
|
|||
return sendError(res)(e);
|
||||
}
|
||||
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) {
|
||||
|
@ -139,8 +148,6 @@ export async function modifyServer(req, res) {
|
|||
try {
|
||||
checkServerId(serverSpec);
|
||||
const serverEntry = await modifyServerEntry(serverSpec);
|
||||
console.log("NEW ENTRY");
|
||||
console.log(serverEntry);
|
||||
// await createServerResources(serverEntry);
|
||||
res.sendStatus(200);
|
||||
} catch (e) {
|
||||
|
|
|
@ -35,7 +35,7 @@ export async function createServerEntry(serverSpec) {
|
|||
server_type,
|
||||
memory,
|
||||
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_bucket_path,
|
||||
backup_id,
|
||||
|
|
|
@ -6,7 +6,7 @@ export default function BackupBucketOption(props) {
|
|||
<TextField
|
||||
label="Bucket Path"
|
||||
onChange={onChange}
|
||||
defaultValue={value}
|
||||
value={value}
|
||||
helperText="Example: /minecraft-backups/example-backups"
|
||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||
required
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import TextField from "@mui/material/TextField";
|
||||
export default function BackupHostOption(props) {
|
||||
const { onChange } = props;
|
||||
const { value, onChange } = props;
|
||||
|
||||
return (
|
||||
<TextField
|
||||
label="Backup Host"
|
||||
onChange={onChange}
|
||||
value={value}
|
||||
helperText="Example: s3.mydomain.com"
|
||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||
required
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import TextField from "@mui/material/TextField";
|
||||
export default function BackupIdOption(props) {
|
||||
const { onChange } = props;
|
||||
const { value, onChange } = props;
|
||||
|
||||
return (
|
||||
<TextField
|
||||
label="S3 Access Key ID"
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
helperText="Example: s3-access-key-id"
|
||||
FormHelperTextProps={{ sx: { ml: 0 } }}
|
||||
|
|
|
@ -33,12 +33,15 @@ import BackupIntervalOption, {
|
|||
|
||||
export default function EditCoreOptions(props) {
|
||||
const { serverId } = props;
|
||||
const [backupEnabled, setBackupEnabled] = useState(false);
|
||||
const [spec, setSpec] = useState();
|
||||
const modifyServer = useModifyServer(spec);
|
||||
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 s = { ...spec };
|
||||
|
@ -49,10 +52,12 @@ export default function EditCoreOptions(props) {
|
|||
const coreUpdate = (attr) => (e) => updateSpec(attr, e.target.value);
|
||||
|
||||
const upsertSpec = () => {
|
||||
console.log(spec);
|
||||
modifyServer(spec);
|
||||
};
|
||||
|
||||
const toggleBackupEnabled = () =>
|
||||
updateSpec("backupEnabled", !spec.backupEnabled);
|
||||
|
||||
function validateSpec() {
|
||||
console.log("TODO CREATE VALIDATION");
|
||||
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")} />
|
||||
|
||||
<ExtraPortsOption extraPorts={spec.extraPorts} onChange={updateSpec} />
|
||||
<FormControlLabel
|
||||
control={
|
||||
<Switch
|
||||
checked={backupEnabled}
|
||||
inputProps={{ "aria-label": "controlled" }}
|
||||
/>
|
||||
}
|
||||
label="Enable Backups?"
|
||||
labelPlacement="start"
|
||||
sx={{ mr: "auto" }}
|
||||
/>
|
||||
{backupEnabled && (
|
||||
{spec.backupEnabled !== null && (
|
||||
<FormControlLabel
|
||||
control={
|
||||
<Switch
|
||||
checked={spec.backupEnabled}
|
||||
inputProps={{ "aria-label": "controlled" }}
|
||||
onClick={toggleBackupEnabled}
|
||||
/>
|
||||
}
|
||||
label="Enable Backups?"
|
||||
labelPlacement="start"
|
||||
sx={{ mr: "auto" }}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/*spec.backupEnabled && ( // TODO: Disabled while secrets are insecure
|
||||
<FormControl
|
||||
fullWidth
|
||||
sx={{ mt: "2rem", display: "flex", gap: ".5rem" }}
|
||||
|
@ -118,10 +127,10 @@ export default function EditCoreOptions(props) {
|
|||
/>
|
||||
<BackupIntervalOption onChange={coreUpdate("backupInterval")} />
|
||||
</FormControl>
|
||||
)}
|
||||
)*/}
|
||||
|
||||
<Button onClick={upsertSpec} variant="contained">
|
||||
Create
|
||||
Save Changes
|
||||
</Button>
|
||||
</FormControl>
|
||||
</Box>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue