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
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user