From 3a01052e03fdfb999adca8b5e69edade33f24e42 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 8 Nov 2022 23:39:27 -0500 Subject: [PATCH] feat(striker-ui-api): add PUT /host/:hostUUID --- striker-ui-api/src/lib/consts/SERVER_PATHS.ts | 1 + striker-ui-api/src/lib/convertHostUUID.ts | 11 ++++++ .../src/lib/request_handlers/host/getHost.ts | 5 +-- .../host/getHostConnection.ts | 10 ++--- .../request_handlers/host/getHostDetail.ts | 8 ++-- .../src/lib/request_handlers/host/index.ts | 1 + .../lib/request_handlers/host/updateHost.ts | 39 +++++++++++++++++++ .../src/lib/request_handlers/job/getJob.ts | 4 +- .../lib/request_handlers/ssh-key/getSSHKey.ts | 5 +-- striker-ui-api/src/routes/host.ts | 4 +- 10 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 striker-ui-api/src/lib/convertHostUUID.ts create mode 100644 striker-ui-api/src/lib/request_handlers/host/updateHost.ts diff --git a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts index 37c06a2f..cab4d9b2 100644 --- a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts +++ b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts @@ -21,6 +21,7 @@ const EMPTY_SERVER_PATHS: ServerPath = { 'anvil-manage-power': {}, 'anvil-provision-server': {}, 'anvil-sync-shared': {}, + 'striker-manage-install-target': {}, 'striker-parse-os-list': {}, }, }, diff --git a/striker-ui-api/src/lib/convertHostUUID.ts b/striker-ui-api/src/lib/convertHostUUID.ts new file mode 100644 index 00000000..d214bbb5 --- /dev/null +++ b/striker-ui-api/src/lib/convertHostUUID.ts @@ -0,0 +1,11 @@ +import { LOCAL } from './consts/LOCAL'; + +import { getLocalHostUUID } from './accessModule'; + +export const toHostUUID = (hostUUID: string) => + hostUUID === LOCAL ? getLocalHostUUID() : hostUUID; + +export const toLocal = ( + hostUUID: string, + localHostUUID: string = getLocalHostUUID(), +) => (hostUUID === localHostUUID ? LOCAL : hostUUID); diff --git a/striker-ui-api/src/lib/request_handlers/host/getHost.ts b/striker-ui-api/src/lib/request_handlers/host/getHost.ts index e7335a2a..23d7c5bf 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHost.ts @@ -1,8 +1,7 @@ -import { LOCAL } from '../../consts/LOCAL'; - import { getLocalHostUUID } from '../../accessModule'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { buildQueryHostDetail } from './buildQueryHostDetail'; +import { toLocal } from '../../convertHostUUID'; import { sanitizeQS } from '../../sanitizeQS'; export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { @@ -23,7 +22,7 @@ export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { if (output instanceof Array) { result = output.reduce>( (previous, [hostName, hostUUID]) => { - const key = hostUUID === localHostUUID ? LOCAL : hostUUID; + const key = toLocal(hostUUID, localHostUUID); previous[key] = { hostName, hostUUID }; diff --git a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts index 2da4ae38..eaf29ea2 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts @@ -1,9 +1,9 @@ -import { LOCAL } from '../../consts/LOCAL'; - import { getAnvilData, getLocalHostUUID } from '../../accessModule'; import { buildUnknownIDCondition } from '../../buildCondition'; import buildGetRequestHandler from '../buildGetRequestHandler'; +import { toLocal } from '../../convertHostUUID'; import { match } from '../../match'; +import { stdout } from '../../shell'; const buildHostConnections = ( fromHostUUID: string, @@ -63,9 +63,9 @@ export const getHostConnection = buildGetRequestHandler( beforeBuildIDCond.length > 0 ? beforeBuildIDCond : [localHostUUID]; const getConnectionKey = (hostUUID: string) => - hostUUID === localHostUUID ? LOCAL : hostUUID; + toLocal(hostUUID, localHostUUID); - process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]\n`); + stdout(`condHostUUIDs=[${condHostUUIDs}]`); try { ({ database: rawDatabaseData } = getAnvilData({ database: true })); @@ -83,7 +83,7 @@ export const getHostConnection = buildGetRequestHandler( return previous; }, {}); - process.stdout.write(`connections=[${JSON.stringify(connections)}]\n`); + stdout(`connections=[${JSON.stringify(connections, null, 2)}]`); if (buildQueryOptions) { buildQueryOptions.afterQueryReturn = (queryStdout) => { diff --git a/striker-ui-api/src/lib/request_handlers/host/getHostDetail.ts b/striker-ui-api/src/lib/request_handlers/host/getHostDetail.ts index 3c7c0419..676668fb 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHostDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHostDetail.ts @@ -1,13 +1,11 @@ -import { LOCAL } from '../../consts/LOCAL'; - -import { getLocalHostUUID } from '../../accessModule'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { buildQueryHostDetail } from './buildQueryHostDetail'; +import { toHostUUID } from '../../convertHostUUID'; import { sanitizeSQLParam } from '../../sanitizeSQLParam'; export const getHostDetail = buildGetRequestHandler( - ({ params: { hostUUID: host } }, buildQueryOptions) => { - const hostUUID = host === LOCAL ? getLocalHostUUID() : host; + ({ params: { hostUUID: rawHostUUID } }, buildQueryOptions) => { + const hostUUID = toHostUUID(rawHostUUID); const { afterQueryReturn, query } = buildQueryHostDetail({ keys: [sanitizeSQLParam(hostUUID)], }); diff --git a/striker-ui-api/src/lib/request_handlers/host/index.ts b/striker-ui-api/src/lib/request_handlers/host/index.ts index 81db7c31..672350e2 100644 --- a/striker-ui-api/src/lib/request_handlers/host/index.ts +++ b/striker-ui-api/src/lib/request_handlers/host/index.ts @@ -2,3 +2,4 @@ export * from './createHost'; export * from './getHost'; export * from './getHostConnection'; export * from './getHostDetail'; +export * from './updateHost'; diff --git a/striker-ui-api/src/lib/request_handlers/host/updateHost.ts b/striker-ui-api/src/lib/request_handlers/host/updateHost.ts new file mode 100644 index 00000000..d73b56bd --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/host/updateHost.ts @@ -0,0 +1,39 @@ +import { RequestHandler } from 'express'; + +import { LOCAL } from '../../consts/LOCAL'; +import SERVER_PATHS from '../../consts/SERVER_PATHS'; + +import { job } from '../../accessModule'; +import { stdout } from '../../shell'; + +export const updateHost: RequestHandler< + { hostUUID: string }, + undefined, + { isEnableInstallTarget?: boolean } +> = (request, response) => { + stdout( + `Begin edit host properties.\n${JSON.stringify(request.body, null, 2)}`, + ); + + const { + body: { isEnableInstallTarget }, + params: { hostUUID: rawHostUUID }, + } = request; + const hostUUID: string | undefined = + rawHostUUID === LOCAL ? undefined : rawHostUUID; + + if (isEnableInstallTarget !== undefined) { + const task = isEnableInstallTarget ? 'enable' : 'disable'; + + job({ + file: __filename, + job_command: `${SERVER_PATHS.usr.sbin['striker-manage-install-target'].self} --${task}`, + job_description: 'job_0016', + job_host_uuid: hostUUID, + job_name: `install-target::${task}`, + job_title: 'job_0015', + }); + } + + response.status(200).send(); +}; diff --git a/striker-ui-api/src/lib/request_handlers/job/getJob.ts b/striker-ui-api/src/lib/request_handlers/job/getJob.ts index 8592534e..a6ce2137 100644 --- a/striker-ui-api/src/lib/request_handlers/job/getJob.ts +++ b/striker-ui-api/src/lib/request_handlers/job/getJob.ts @@ -1,6 +1,6 @@ import buildGetRequestHandler from '../buildGetRequestHandler'; import { sanitizeQS } from '../../sanitizeQS'; -import { date } from '../../shell'; +import { date, stdout } from '../../shell'; export const getJob = buildGetRequestHandler((request, buildQueryOptions) => { const { start: rawStart } = request.query; @@ -19,7 +19,7 @@ export const getJob = buildGetRequestHandler((request, buildQueryOptions) => { ); } - process.stdout.write(`condModifiedDate=[${condModifiedDate}]\n`); + stdout(`condModifiedDate=[${condModifiedDate}]`); if (buildQueryOptions) { buildQueryOptions.afterQueryReturn = (queryStdout) => { diff --git a/striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKey.ts b/striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKey.ts index 1fc9f3af..46094e72 100644 --- a/striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKey.ts +++ b/striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKey.ts @@ -1,8 +1,7 @@ -import { LOCAL } from '../../consts/LOCAL'; - import { getLocalHostUUID } from '../../accessModule'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { buildQueryResultReducer } from '../../buildQueryResultModifier'; +import { toLocal } from '../../convertHostUUID'; import { match } from '../../match'; import { sanitizeQS } from '../../sanitizeQS'; @@ -30,7 +29,7 @@ const MAP_TO_HANDLER: Record = { }; }; }>((previous, [hostName, hostUUID, stateName, stateNote, stateUUID]) => { - const hostUUIDKey = hostUUID === localHostUUID ? LOCAL : hostUUID; + const hostUUIDKey = toLocal(hostUUID, localHostUUID); if (previous[hostUUIDKey] === undefined) { previous[hostUUIDKey] = {}; diff --git a/striker-ui-api/src/routes/host.ts b/striker-ui-api/src/routes/host.ts index 3618deb4..2edaa115 100644 --- a/striker-ui-api/src/routes/host.ts +++ b/striker-ui-api/src/routes/host.ts @@ -5,6 +5,7 @@ import { getHost, getHostConnection, getHostDetail, + updateHost, } from '../lib/request_handlers/host'; const router = express.Router(); @@ -13,6 +14,7 @@ router .get('/', getHost) .get('/connection', getHostConnection) .get('/:hostUUID', getHostDetail) - .post('/', createHost); + .post('/', createHost) + .put('/:hostUUID', updateHost); export default router;