fix(striker-ui-api): add endpoint for delete server(s)

main
Tsu-ba-me 2 years ago
parent e638e2d388
commit 4e03e62dd4
  1. 1
      striker-ui-api/src/lib/consts/SERVER_PATHS.ts
  2. 66
      striker-ui-api/src/lib/request_handlers/server/deleteServer.ts
  3. 7
      striker-ui-api/src/lib/request_handlers/server/index.ts
  4. 3
      striker-ui-api/src/routes/server.ts

@ -26,6 +26,7 @@ const EMPTY_SERVER_PATHS: ServerPath = {
'anvil-access-module': {},
'anvil-boot-server': {},
'anvil-configure-host': {},
'anvil-delete-server': {},
'anvil-get-server-screenshot': {},
'anvil-join-anvil': {},
'anvil-manage-keys': {},

@ -0,0 +1,66 @@
import assert from 'assert';
import { RequestHandler } from 'express';
import { REP_UUID, SERVER_PATHS } from '../../consts';
import { job, query } from '../../accessModule';
import { sanitize } from '../../sanitize';
import { stderr, stdoutVar } from '../../shell';
export const deleteServer: RequestHandler<
{ serverUuid?: string },
undefined,
{ serverUuids: string[] }
> = async (request, response) => {
const {
body: { serverUuids: rServerUuids } = {},
params: { serverUuid: rServerUuid },
} = request;
const serverUuids = sanitize(rServerUuids, 'string[]', {
modifierType: 'sql',
});
if (rServerUuid) {
serverUuids.push(
sanitize(rServerUuid, 'string', {
modifierType: 'sql',
}),
);
}
stdoutVar(serverUuids, `Delete servers with: `);
for (const serverUuid of serverUuids) {
try {
assert(
REP_UUID.test(serverUuid),
`Server UUID must be a valid UUIDv4; got [${serverUuid}]`,
);
const rows: [[string]] = await query(
`SELECT server_host_uuid FROM servers WHERE server_uuid = '${serverUuid}';`,
);
assert.ok(rows.length, `Server ${serverUuid} not found`);
const [[serverHostUuid]] = rows;
job({
file: __filename,
job_command: `${SERVER_PATHS.usr.sbin['anvil-delete-server'].self}`,
job_data: `server_uuid=${serverUuid}`,
job_description: 'job_0209',
job_host_uuid: serverHostUuid,
job_name: 'server::delete',
job_title: 'job_0208',
});
} catch (error) {
stderr(`Failed to initiate delete server ${serverUuid}; CAUSE: ${error}`);
return response.status(500).send();
}
}
return response.status(204).send();
};

@ -1,3 +1,4 @@
export { createServer } from './createServer';
export { getServer } from './getServer';
export { getServerDetail } from './getServerDetail';
export * from './createServer';
export * from './deleteServer';
export * from './getServer';
export * from './getServerDetail';

@ -2,6 +2,7 @@ import express from 'express';
import {
createServer,
deleteServer,
getServer,
getServerDetail,
} from '../lib/request_handlers/server';
@ -9,6 +10,8 @@ import {
const router = express.Router();
router
.delete('/', deleteServer)
.delete('/:serverUuid', deleteServer)
.get('/', getServer)
.get('/:serverUUID', getServerDetail)
.post('/', createServer);

Loading…
Cancel
Save