fix(striker-ui-api): add PUT /host/prepare and revise PUT /host/:hostUUID

main
Tsu-ba-me 2 years ago
parent 54b78e9f25
commit 5e9c38b008
  1. 34
      striker-ui-api/src/lib/request_handlers/buildBranchRequestHandler.ts
  2. 22
      striker-ui-api/src/lib/request_handlers/host/createHost.ts
  3. 1
      striker-ui-api/src/lib/request_handlers/host/index.ts
  4. 19
      striker-ui-api/src/lib/request_handlers/host/prepareHost.ts
  5. 39
      striker-ui-api/src/lib/request_handlers/host/setHostInstallTarget.ts
  6. 47
      striker-ui-api/src/lib/request_handlers/host/updateHost.ts
  7. 2
      striker-ui-api/src/routes/host.ts
  8. 19
      striker-ui-api/src/types/UpdateHostRequestBody.d.ts

@ -0,0 +1,34 @@
import { RequestHandler } from 'express';
import { sanitize } from '../sanitize';
import { stderr, stdout } from '../shell';
export const buildBranchRequestHandler: (map: {
[handler: string]: RequestHandler | undefined;
}) => RequestHandler =
(map) =>
(...args) => {
const [
{
query: { handler: rawHandler },
},
response,
] = args;
const handlerKey = sanitize(rawHandler, 'string');
stdout(`Create host handler: ${handlerKey}`);
// Ensure each handler sends a response at the end of any branch.
const handler = map[handlerKey];
if (handler) {
handler(...args);
} else {
stderr(`Handler is not registered; got [${handlerKey}]`);
response.status(400).send();
return;
}
};

@ -1,24 +1,8 @@
import { RequestHandler } from 'express'; import { RequestHandler } from 'express';
import { buildBranchRequestHandler } from '../buildBranchRequestHandler';
import { configStriker } from './configStriker'; import { configStriker } from './configStriker';
import { sanitize } from '../../sanitize';
import { stdout } from '../../shell';
// Ensure each create handler sends a response at the end of any branch. export const createHost: RequestHandler = buildBranchRequestHandler({
const MAP_TO_CREATE_HANDLER: Record<string, RequestHandler | undefined> = {
striker: configStriker, striker: configStriker,
}; });
export const createHost: RequestHandler = (...args) => {
const [
{
query: { hostType: rawHostType },
},
] = args;
const hostType = sanitize(rawHostType, 'string');
stdout(`hostType=[${hostType}]`);
MAP_TO_CREATE_HANDLER[hostType]?.call(null, ...args);
};

@ -2,4 +2,5 @@ export * from './createHost';
export * from './getHost'; export * from './getHost';
export * from './getHostConnection'; export * from './getHostConnection';
export * from './getHostDetail'; export * from './getHostDetail';
export * from './prepareHost';
export * from './updateHost'; export * from './updateHost';

@ -11,22 +11,12 @@ import SERVER_PATHS from '../../consts/SERVER_PATHS';
import { job, variable } from '../../accessModule'; import { job, variable } from '../../accessModule';
import { sanitize } from '../../sanitize'; import { sanitize } from '../../sanitize';
import { stderr } from '../../shell'; import { stderr, stdout } from '../../shell';
export const prepareHost: RequestHandler< export const prepareHost: RequestHandler<
unknown, unknown,
undefined, undefined,
{ PrepareHostRequestBody
hostIPAddress: string;
hostName: string;
hostPassword: string;
hostSSHPort?: number;
hostType: string;
hostUser?: string;
hostUUID?: string;
redhatPassword: string;
redhatUser: string;
}
> = (request, response) => { > = (request, response) => {
const { const {
body: { body: {
@ -43,6 +33,8 @@ export const prepareHost: RequestHandler<
} = request; } = request;
const isHostUUIDProvided = hostUUID !== undefined; const isHostUUIDProvided = hostUUID !== undefined;
const isRedhatAccountProvided =
redhatPassword !== undefined || redhatUser !== undefined;
const dataHostIPAddress = sanitize(hostIPAddress, 'string'); const dataHostIPAddress = sanitize(hostIPAddress, 'string');
const dataHostName = sanitize(hostName, 'string'); const dataHostName = sanitize(hostName, 'string');
@ -87,6 +79,7 @@ export const prepareHost: RequestHandler<
); );
} }
if (isRedhatAccountProvided) {
assert( assert(
REP_PEACEFUL_STRING.test(dataRedhatPassword), REP_PEACEFUL_STRING.test(dataRedhatPassword),
`Data redhat password must be a peaceful string; got [${dataRedhatPassword}]`, `Data redhat password must be a peaceful string; got [${dataRedhatPassword}]`,
@ -96,6 +89,7 @@ export const prepareHost: RequestHandler<
REP_PEACEFUL_STRING.test(dataRedhatUser), REP_PEACEFUL_STRING.test(dataRedhatUser),
`Data redhat user must be a peaceful string; got [${dataRedhatUser}]`, `Data redhat user must be a peaceful string; got [${dataRedhatUser}]`,
); );
}
} catch (assertError) { } catch (assertError) {
stderr( stderr(
`Failed to assert value when trying to prepare host; CAUSE: ${assertError}`, `Failed to assert value when trying to prepare host; CAUSE: ${assertError}`,
@ -130,7 +124,6 @@ ssh_port=${dataHostSSHPort}
type=${dataHostType}`, type=${dataHostType}`,
job_description: 'job_0022', job_description: 'job_0022',
job_name: `initialize::${dataHostType}::${dataHostIPAddress}`, job_name: `initialize::${dataHostType}::${dataHostIPAddress}`,
job_progress: 100,
job_title: `job_002${dataHostType === 'dr' ? '1' : '0'}`, job_title: `job_002${dataHostType === 'dr' ? '1' : '0'}`,
}); });
} catch (subError) { } catch (subError) {

@ -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 { stderr, stdout } from '../../shell';
export const setHostInstallTarget: RequestHandler = (request, response) => {
stdout(
`Begin set host install target.\n${JSON.stringify(request.body, null, 2)}`,
);
const { isEnableInstallTarget } =
request.body as SetHostInstallTargetRequestBody;
const { hostUUID: rawHostUUID } = request.params as UpdateHostParams;
const hostUUID: string | undefined =
rawHostUUID === LOCAL ? undefined : rawHostUUID;
const task = isEnableInstallTarget ? 'enable' : 'disable';
try {
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',
});
} catch (subError) {
stderr(`Failed to ${task} install target; CAUSE: ${subError}`);
response.status(500).send();
return;
}
response.status(200).send();
};

@ -1,47 +1,8 @@
import { RequestHandler } from 'express'; import { RequestHandler } from 'express';
import { LOCAL } from '../../consts/LOCAL'; import { buildBranchRequestHandler } from '../buildBranchRequestHandler';
import SERVER_PATHS from '../../consts/SERVER_PATHS'; import { setHostInstallTarget } from './setHostInstallTarget';
import { job } from '../../accessModule'; export const updateHost: RequestHandler = buildBranchRequestHandler({
import { stderr, stdout } from '../../shell'; 'install-target': setHostInstallTarget,
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';
try {
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',
}); });
} catch (subError) {
stderr(`Failed to ${task} install target; CAUSE: ${subError}`);
response.status(500).send();
return;
}
}
response.status(200).send();
};

@ -5,6 +5,7 @@ import {
getHost, getHost,
getHostConnection, getHostConnection,
getHostDetail, getHostDetail,
prepareHost,
updateHost, updateHost,
} from '../lib/request_handlers/host'; } from '../lib/request_handlers/host';
@ -15,6 +16,7 @@ router
.get('/connection', getHostConnection) .get('/connection', getHostConnection)
.get('/:hostUUID', getHostDetail) .get('/:hostUUID', getHostDetail)
.post('/', createHost) .post('/', createHost)
.put('/prepare', prepareHost)
.put('/:hostUUID', updateHost); .put('/:hostUUID', updateHost);
export default router; export default router;

@ -0,0 +1,19 @@
type SetHostInstallTargetRequestBody = {
isEnableInstallTarget: boolean;
};
type PrepareHostRequestBody = {
hostIPAddress: string;
hostName: string;
hostPassword: string;
hostSSHPort?: number;
hostType: string;
hostUser?: string;
hostUUID?: string;
redhatPassword: string;
redhatUser: string;
};
type UpdateHostParams = {
hostUUID: string;
};
Loading…
Cancel
Save