Initial
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { hostSchema } from '../forms/schemas.js';
|
||||
import { createHost, deleteHost, listHosts, updateHost } from '../models/hostModel.js';
|
||||
import { hostSchema, vcenterImportSchema, vcenterPreviewSchema } from '../forms/schemas.js';
|
||||
import { createHost, deleteHost, findVisibleHostByIdentity, listHosts, updateHost } from '../models/hostModel.js';
|
||||
import { buildHostPayloadFromVm, previewVCenterHosts, vcenterStatus } from '../services/vcenterService.js';
|
||||
|
||||
export function index(req, res) {
|
||||
res.json(listHosts(req.user.id));
|
||||
@@ -21,3 +22,45 @@ export function destroy(req, res) {
|
||||
deleteHost(req.params.id, req.user.id);
|
||||
res.status(204).end();
|
||||
}
|
||||
|
||||
export function vcenterImportStatus(req, res) {
|
||||
res.json(vcenterStatus());
|
||||
}
|
||||
|
||||
export async function previewVCenterImport(req, res) {
|
||||
const parsed = vcenterPreviewSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: 'Invalid vCenter preview payload' });
|
||||
try {
|
||||
res.json(await previewVCenterHosts(parsed.data));
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
export async function importVCenterHosts(req, res) {
|
||||
const parsed = vcenterImportSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: 'Invalid vCenter import payload' });
|
||||
try {
|
||||
const preview = await previewVCenterHosts(parsed.data);
|
||||
const selected = new Set(parsed.data.vmIds || []);
|
||||
const candidates = selected.size
|
||||
? preview.candidates.filter((candidate) => selected.has(candidate.id))
|
||||
: preview.candidates;
|
||||
const imported = [];
|
||||
const skipped = [];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const hostPayload = buildHostPayloadFromVm(candidate, parsed.data);
|
||||
const existing = findVisibleHostByIdentity(hostPayload, req.user.id);
|
||||
if (existing) {
|
||||
skipped.push({ vm: candidate, reason: `Existing host matched ${existing.name}` });
|
||||
continue;
|
||||
}
|
||||
imported.push(createHost(hostPayload, req.user.id));
|
||||
}
|
||||
|
||||
res.status(201).json({ imported, skipped, scanned: preview.count });
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user