fix: resolve custom threshold dropdown validation error

- Client now uses 'auto-detect' placeholder when node can't be determined
- Server handles node auto-detection by looking up VM/LXC in current state
- Fixes 'Please select a VM/LXC from the dropdown first' error in v3.17.3
- Addresses issue #110
This commit is contained in:
courtmanr@gmail.com
2025-06-01 20:51:53 +01:00
parent 7abe322944
commit f7303b573a
3 changed files with 68 additions and 5 deletions
+16 -1
View File
@@ -1 +1,16 @@
{}
{
"primary:103": {
"endpointId": "primary",
"nodeId": "auto-detect",
"vmid": "103",
"thresholds": {
"disk": {
"warning": 70,
"critical": 80
}
},
"enabled": true,
"createdAt": "2025-06-01T19:50:34.784Z",
"updatedAt": "2025-06-01T19:50:34.784Z"
}
}
+40 -2
View File
@@ -56,7 +56,7 @@ function setupThresholdRoutes(app) {
// Set custom thresholds for VM/LXC
app.post('/api/thresholds/:endpointId/:nodeId/:vmid', async (req, res) => {
try {
const { endpointId, nodeId, vmid } = req.params;
let { endpointId, nodeId, vmid } = req.params;
const { thresholds } = req.body;
if (!thresholds) {
@@ -66,6 +66,25 @@ function setupThresholdRoutes(app) {
});
}
// Handle auto-detect node
if (nodeId === 'auto-detect') {
const state = require('./state');
// Find the VM/LXC in the current state
const allGuests = [...(state.vms || []), ...(state.containers || [])];
const guest = allGuests.find(g =>
g.endpointId === endpointId && g.id === vmid
);
if (guest && guest.node) {
nodeId = guest.node;
console.log(`[ThresholdRoutes] Auto-detected node '${nodeId}' for ${endpointId}:${vmid}`);
} else {
// If we can't find the node, use a wildcard that will match any node
nodeId = '*';
console.log(`[ThresholdRoutes] Could not auto-detect node for ${endpointId}:${vmid}, using wildcard`);
}
}
if (!customThresholdManager.initialized) {
await customThresholdManager.init();
}
@@ -84,7 +103,7 @@ function setupThresholdRoutes(app) {
// Update existing custom thresholds
app.put('/api/thresholds/:endpointId/:nodeId/:vmid', async (req, res) => {
try {
const { endpointId, nodeId, vmid } = req.params;
let { endpointId, nodeId, vmid } = req.params;
const { thresholds } = req.body;
if (!thresholds) {
@@ -94,6 +113,25 @@ function setupThresholdRoutes(app) {
});
}
// Handle auto-detect node
if (nodeId === 'auto-detect') {
const state = require('./state');
// Find the VM/LXC in the current state
const allGuests = [...(state.vms || []), ...(state.containers || [])];
const guest = allGuests.find(g =>
g.endpointId === endpointId && g.id === vmid
);
if (guest && guest.node) {
nodeId = guest.node;
console.log(`[ThresholdRoutes] Auto-detected node '${nodeId}' for ${endpointId}:${vmid}`);
} else {
// If we can't find the node, use a wildcard that will match any node
nodeId = '*';
console.log(`[ThresholdRoutes] Could not auto-detect node for ${endpointId}:${vmid}, using wildcard`);
}
}
if (!customThresholdManager.initialized) {
await customThresholdManager.init();
}
+12 -2
View File
@@ -1712,14 +1712,24 @@ PulseApp.ui.settings = (() => {
// Find the current node for this VM (for display purposes)
const selectedGuest = allGuests.find(g => g.endpointId === selectedEndpoint && g.id === selectedVmid);
nodeField.value = selectedGuest ? selectedGuest.node : '';
// If we can't find the guest or it doesn't have a node, use a placeholder
// The server will handle finding the actual node
if (selectedGuest && selectedGuest.node) {
nodeField.value = selectedGuest.node;
} else {
// Use a placeholder value that will pass validation
// The server can determine the actual node from endpointId and vmid
nodeField.value = 'auto-detect';
}
// Debug logging
console.log('[Settings] Guest selector changed:', {
selectedEndpoint,
selectedVmid,
selectedGuest,
nodeValue: selectedGuest ? selectedGuest.node : 'MISSING'
nodeValue: nodeField.value,
allGuestsCount: allGuests.length
});
} else {
endpointField.value = '';