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. 35
      striker-ui-api/src/lib/request_handlers/host/prepareHost.ts
  5. 39
      striker-ui-api/src/lib/request_handlers/host/setHostInstallTarget.ts
  6. 49
      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 { buildBranchRequestHandler } from '../buildBranchRequestHandler';
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.
const MAP_TO_CREATE_HANDLER: Record<string, RequestHandler | undefined> = {
export const createHost: RequestHandler = buildBranchRequestHandler({
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 './getHostConnection';
export * from './getHostDetail';
export * from './prepareHost';
export * from './updateHost';

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

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