Initial
This commit is contained in:
@@ -146,6 +146,22 @@ export function normalizeVCenterVm(vm, identity = null, interfacesPayload = null
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeVCenterSummaryVm(vm) {
|
||||
return {
|
||||
id: vm.vm || vm.id,
|
||||
name: vm.name || vm.vm || vm.id || '',
|
||||
fqdn: '',
|
||||
address: vm.name || vm.vm || vm.id || '',
|
||||
ipAddress: '',
|
||||
ipAddresses: [],
|
||||
powerState: vm.power_state || vm.powerState || '',
|
||||
osFamily: 'windows',
|
||||
guestFullName: '',
|
||||
toolsAvailable: false,
|
||||
source: 'vcenter'
|
||||
};
|
||||
}
|
||||
|
||||
export function vmMatchesFilter(candidate, filter = {}) {
|
||||
const operator = filter.operator || 'contains';
|
||||
const needle = String(filter.value || '').trim().toLowerCase();
|
||||
@@ -172,6 +188,11 @@ export function vmMatchesFilter(candidate, filter = {}) {
|
||||
return value.includes(needle);
|
||||
}
|
||||
|
||||
export function filterSummaryVms(vms = [], filter = {}) {
|
||||
if ((filter?.field || 'hostname') !== 'name') return vms;
|
||||
return vms.filter((vm) => vmMatchesFilter(normalizeVCenterSummaryVm(vm), filter));
|
||||
}
|
||||
|
||||
export function buildHostPayloadFromVm(candidate, options = {}) {
|
||||
const tags = [...new Set(['vmware', 'vcenter', `vcenter:${candidate.id}`, ...(options.tags || [])].filter(Boolean))];
|
||||
return {
|
||||
@@ -199,12 +220,22 @@ export async function previewVCenterHosts(options = {}) {
|
||||
assertConfigured(settings);
|
||||
const sessionId = await createSession(settings);
|
||||
const vmPayload = await vcenterFetch(settings, '/api/vcenter/vm', { sessionId });
|
||||
const vms = unwrapList(vmPayload).slice(0, Math.max(options.limit || 100, 1) * 4);
|
||||
const allVms = unwrapList(vmPayload);
|
||||
const vms = filterSummaryVms(allVms, options.filter);
|
||||
const candidates = [];
|
||||
const diagnostics = {
|
||||
totalFromVCenter: allVms.length,
|
||||
scanned: 0,
|
||||
summaryMatched: vms.length,
|
||||
resultLimit: options.limit || 100,
|
||||
sampleNames: allVms.slice(0, 12).map((vm) => vm.name || vm.vm || vm.id).filter(Boolean),
|
||||
filter: options.filter || {}
|
||||
};
|
||||
|
||||
for (const vm of vms) {
|
||||
const vmId = vm.vm || vm.id;
|
||||
if (!vmId) continue;
|
||||
diagnostics.scanned += 1;
|
||||
const identity = await optionalVCenterFetch(settings, `/api/vcenter/vm/${encodeURIComponent(vmId)}/guest/identity`, sessionId);
|
||||
const interfaces = await optionalVCenterFetch(settings, `/api/vcenter/vm/${encodeURIComponent(vmId)}/guest/networking/interfaces`, sessionId);
|
||||
const candidate = normalizeVCenterVm(vm, identity?.unavailable ? null : identity, interfaces?.unavailable ? null : interfaces);
|
||||
@@ -216,6 +247,7 @@ export async function previewVCenterHosts(options = {}) {
|
||||
status: vcenterStatus(),
|
||||
filter: options.filter || {},
|
||||
count: candidates.length,
|
||||
diagnostics,
|
||||
candidates
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { load } from './setup.mjs';
|
||||
|
||||
const {
|
||||
buildHostPayloadFromVm,
|
||||
filterSummaryVms,
|
||||
normalizeBaseUrl,
|
||||
normalizeVCenterVm,
|
||||
vmMatchesFilter
|
||||
@@ -37,6 +38,17 @@ test('vmMatchesFilter supports hostname contains and IP equals matching', () =>
|
||||
assert.equal(vmMatchesFilter(candidate, { field: 'fqdn', operator: 'startsWith', value: 'db' }), false);
|
||||
});
|
||||
|
||||
test('filterSummaryVms scans the full VM summary list for VM-name filters', () => {
|
||||
const vms = [
|
||||
...Array.from({ length: 900 }, (_, index) => ({ vm: `vm-${index}`, name: `ZZZ-${index}` })),
|
||||
{ vm: 'vm-901', name: 'MCS-WIN-APP01' }
|
||||
];
|
||||
const matched = filterSummaryVms(vms, { field: 'name', operator: 'contains', value: 'MCS' });
|
||||
|
||||
assert.equal(matched.length, 1);
|
||||
assert.equal(matched[0].name, 'MCS-WIN-APP01');
|
||||
});
|
||||
|
||||
test('buildHostPayloadFromVm attaches credential, tags, transport, and import notes', () => {
|
||||
const payload = buildHostPayloadFromVm(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user