From f56f931599e751ae37eaa3e118e41d1b20f7971e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 8 Jun 2023 00:21:23 -0400 Subject: [PATCH] fix(striker-ui-api): add set host config::map_network variable command endpoint --- .../src/lib/request_handlers/command/index.ts | 1 + .../request_handlers/command/setMapNetwork.ts | 70 +++++++++++++++++++ striker-ui-api/src/routes/command.ts | 2 + striker-ui-api/src/types/ApiCommand.d.ts | 4 ++ .../types/InsertOrUpdateVariableFunction.d.ts | 6 +- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/command/setMapNetwork.ts diff --git a/striker-ui-api/src/lib/request_handlers/command/index.ts b/striker-ui-api/src/lib/request_handlers/command/index.ts index 92feabe7..923d60fd 100644 --- a/striker-ui-api/src/lib/request_handlers/command/index.ts +++ b/striker-ui-api/src/lib/request_handlers/command/index.ts @@ -4,6 +4,7 @@ export * from './leaveAn'; export * from './poweroffStriker'; export * from './rebootStriker'; export * from './runManifest'; +export * from './setMapNetwork'; export * from './startAn'; export * from './startSubnode'; export * from './stopAn'; diff --git a/striker-ui-api/src/lib/request_handlers/command/setMapNetwork.ts b/striker-ui-api/src/lib/request_handlers/command/setMapNetwork.ts new file mode 100644 index 00000000..a8101853 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/setMapNetwork.ts @@ -0,0 +1,70 @@ +import assert from 'assert'; +import { RequestHandler } from 'express'; + +import { LOCAL, REP_UUID } from '../../consts'; + +import { variable } from '../../accessModule'; +import { toHostUUID } from '../../convertHostUUID'; +import { sanitize } from '../../sanitize'; +import { stderr, stdoutVar } from '../../shell'; + +export const setMapNetwork: RequestHandler< + { uuid: string }, + undefined, + SetMapNetworkRequestBody +> = async (request, response) => { + const { + body: { value: rValue } = {}, + params: { uuid: rHostUuid }, + } = request; + + const value = sanitize(rValue, 'number'); + + let hostUuid = sanitize(rHostUuid, 'string', { fallback: LOCAL }); + + hostUuid = toHostUUID(hostUuid); + + stdoutVar({ hostUuid, value }, `Set map network variable with: `); + + try { + assert( + value in [0, 1], + `Variable value must be a number boolean (0 or 1); got [${value}]`, + ); + + assert( + REP_UUID.test(hostUuid), + `Host UUID must be a valid UUIDv4; got [${hostUuid}]`, + ); + } catch (error) { + stderr(`Assert failed when set map network variable; CAUSE: ${error}`); + + return response.status(400).send(); + } + + try { + const result = await variable({ + file: __filename, + variable_default: 0, + varaible_description: 'striker_0202', + variable_name: 'config::map_network', + variable_section: 'config', + variable_source_table: 'hosts', + variable_source_uuid: hostUuid, + variable_value: value, + }); + + assert( + REP_UUID.test(result), + `Result must be UUID of modified record; got: [${result}]`, + ); + } catch (error) { + stderr( + `Failed to set map network variable for host ${hostUuid}; CAUSE: ${error}`, + ); + + return response.status(500).send(); + } + + return response.status(204).send(); +}; diff --git a/striker-ui-api/src/routes/command.ts b/striker-ui-api/src/routes/command.ts index 0aac6ecf..cff7de3c 100644 --- a/striker-ui-api/src/routes/command.ts +++ b/striker-ui-api/src/routes/command.ts @@ -7,6 +7,7 @@ import { poweroffStriker, rebootStriker, runManifest, + setMapNetwork, startAn, startSubnode, stopAn, @@ -23,6 +24,7 @@ router .put('/poweroff-host', poweroffStriker) .put('/reboot-host', rebootStriker) .put('/run-manifest/:manifestUuid', runManifest) + .put('/set-map-network/:uuid', setMapNetwork) .put('/start-an/:uuid', startAn) .put('/start-subnode/:uuid', startSubnode) .put('/stop-an/:uuid', stopAn) diff --git a/striker-ui-api/src/types/ApiCommand.d.ts b/striker-ui-api/src/types/ApiCommand.d.ts index d3d03adf..0b907ef9 100644 --- a/striker-ui-api/src/types/ApiCommand.d.ts +++ b/striker-ui-api/src/types/ApiCommand.d.ts @@ -13,3 +13,7 @@ type GetHostSshResponseBody = { isInetConnected: boolean; isOSRegistered: boolean; }; + +type SetMapNetworkRequestBody = { + value: number; +}; diff --git a/striker-ui-api/src/types/InsertOrUpdateVariableFunction.d.ts b/striker-ui-api/src/types/InsertOrUpdateVariableFunction.d.ts index 620ea292..09b219ee 100644 --- a/striker-ui-api/src/types/InsertOrUpdateVariableFunction.d.ts +++ b/striker-ui-api/src/types/InsertOrUpdateVariableFunction.d.ts @@ -1,13 +1,15 @@ +type VariableValue = number | string; + type VariableParams = InsertOrUpdateFunctionCommonParams & { update_value_only?: NumberBoolean; - variable_default?: string; + variable_default?: VariableValue; varaible_description?: string; variable_name?: string; variable_section?: string; variable_source_table?: string; variable_source_uuid?: string; variable_uuid?: string; - variable_value?: number | string; + variable_value?: VariableValue; }; type InsertOrUpdateVariableFunction = (