vme-mcp-server
Version:
An intelligent Model Context Protocol (MCP) server that transforms HPE VM Essentials (VME) infrastructure management into natural language conversations. Provision VMs, manage resources, and control your infrastructure through simple English commands with
86 lines (85 loc) • 3.5 kB
JavaScript
// Helper function to parse VM name patterns
export function parseVMNames(nameInput, count) {
// Handle letter range patterns like "ws-a->ws-x" or "ws-a -> ws-x"
const letterRangeMatch = nameInput.match(/^(.+?)([a-z])\s*->\s*(.+?)([a-z])$/i);
if (letterRangeMatch) {
const [, prefix1, startLetter, prefix2, endLetter] = letterRangeMatch;
if (prefix1.trim() === prefix2.trim()) {
const basePrefix = prefix1.trim();
const startCode = startLetter.toLowerCase().charCodeAt(0);
const endCode = endLetter.toLowerCase().charCodeAt(0);
const names = [];
for (let i = startCode; i <= endCode; i++) {
const letter = String.fromCharCode(i);
names.push(`${basePrefix}${letter}`);
}
return names;
}
}
// Handle numeric range patterns like "web01->web03" or "web01 -> web03"
const numericRangeMatch = nameInput.match(/^(.+?)(\d+)\s*->\s*(.+?)(\d+)$/);
if (numericRangeMatch) {
const [, prefix1, start, prefix2, end] = numericRangeMatch;
if (prefix1.trim() === prefix2.trim()) {
const basePrefix = prefix1.trim();
const startNum = parseInt(start);
const endNum = parseInt(end);
const names = [];
for (let i = startNum; i <= endNum; i++) {
const paddedNum = i.toString().padStart(start.length, '0');
names.push(`${basePrefix}${paddedNum}`);
}
return names;
}
}
// Handle count-based generation
if (count && count > 1) {
const names = [];
// Extract base name and number pattern
const match = nameInput.match(/^(.+?)(\d+)$/);
if (match) {
const [, prefix, startStr] = match;
const startNum = parseInt(startStr);
for (let i = 0; i < count; i++) {
const num = startNum + i;
const paddedNum = num.toString().padStart(startStr.length, '0');
names.push(`${prefix}${paddedNum}`);
}
return names;
}
else {
// If no number pattern, append numbers
for (let i = 1; i <= count; i++) {
const paddedNum = i.toString().padStart(2, '0');
names.push(`${nameInput}${paddedNum}`);
}
return names;
}
}
// Single VM
return [nameInput];
}
// Determine node assignment strategy for VMs
export function calculateNodeAssignments(vmNames, nodes, distribution) {
let nodeAssignments = [];
if (distribution === 'spread' || (vmNames.length > 1 && !distribution)) {
// Distribute VMs across all available nodes
vmNames.forEach((vmName, index) => {
const nodeId = nodes[index % nodes.length];
nodeAssignments.push({ name: vmName, kvmHostId: nodeId });
});
}
else if (distribution && !['auto', 'spread'].includes(distribution)) {
// Distribution is a node/hypervisor name - signal that it needs resolution
// Return assignments with the node name for the calling function to resolve
nodeAssignments = vmNames.map(vmName => ({
name: vmName,
nodeNameToResolve: distribution
}));
}
else {
// Auto-placement (no kvmHostId specified)
nodeAssignments = vmNames.map(vmName => ({ name: vmName }));
}
return nodeAssignments;
}