RefreshVMData refreshes data for a specific VM by clearing its cache entries and fetching fresh data The onEnrichmentComplete callback is called after VM data has been enriched with guest agent information.
(vm *VM, onEnrichmentComplete func(*VM))
| 580 | // RefreshVMData refreshes data for a specific VM by clearing its cache entries and fetching fresh data |
| 581 | // The onEnrichmentComplete callback is called after VM data has been enriched with guest agent information. |
| 582 | func (c *Client) RefreshVMData(vm *VM, onEnrichmentComplete func(*VM)) (*VM, error) { |
| 583 | // Clear cache entries for this specific VM |
| 584 | statusPath := fmt.Sprintf("/nodes/%s/%s/%d/status/current", vm.Node, vm.Type, vm.ID) |
| 585 | configPath := fmt.Sprintf("/nodes/%s/%s/%d/config", vm.Node, vm.Type, vm.ID) |
| 586 | |
| 587 | // Generate cache keys and delete them |
| 588 | statusCacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, statusPath) |
| 589 | statusCacheKey = strings.ReplaceAll(statusCacheKey, "/", "_") |
| 590 | |
| 591 | configCacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, configPath) |
| 592 | configCacheKey = strings.ReplaceAll(configCacheKey, "/", "_") |
| 593 | |
| 594 | // Delete cache entries (ignore errors as they might not exist) |
| 595 | _ = c.cache.Delete(statusCacheKey) |
| 596 | _ = c.cache.Delete(configCacheKey) |
| 597 | |
| 598 | // Also clear guest agent related cache entries if it's a QEMU VM |
| 599 | if vm.Type == VMTypeQemu { |
| 600 | agentNetPath := fmt.Sprintf("/nodes/%s/qemu/%d/agent/network-get-interfaces", vm.Node, vm.ID) |
| 601 | agentFsPath := fmt.Sprintf("/nodes/%s/qemu/%d/agent/get-fsinfo", vm.Node, vm.ID) |
| 602 | |
| 603 | agentNetCacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, agentNetPath) |
| 604 | agentNetCacheKey = strings.ReplaceAll(agentNetCacheKey, "/", "_") |
| 605 | |
| 606 | agentFsCacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, agentFsPath) |
| 607 | agentFsCacheKey = strings.ReplaceAll(agentFsCacheKey, "/", "_") |
| 608 | |
| 609 | _ = c.cache.Delete(agentNetCacheKey) |
| 610 | _ = c.cache.Delete(agentFsCacheKey) |
| 611 | } else if vm.Type == VMTypeLXC { |
| 612 | // Clear LXC interfaces cache |
| 613 | lxcInterfacesPath := fmt.Sprintf("/nodes/%s/lxc/%d/interfaces", vm.Node, vm.ID) |
| 614 | lxcInterfacesCacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, lxcInterfacesPath) |
| 615 | lxcInterfacesCacheKey = strings.ReplaceAll(lxcInterfacesCacheKey, "/", "_") |
| 616 | |
| 617 | _ = c.cache.Delete(lxcInterfacesCacheKey) |
| 618 | } |
| 619 | |
| 620 | c.logger.Debug("Cleared cache for VM %s (%d) on node %s", vm.Name, vm.ID, vm.Node) |
| 621 | |
| 622 | // Store the original IP address to preserve it if needed |
| 623 | originalIP := vm.IP |
| 624 | |
| 625 | // Fetch fresh VM data using GetDetailedVmInfo for basic information |
| 626 | freshVM, err := c.GetDetailedVmInfo(vm.Node, vm.Type, vm.ID) |
| 627 | if err != nil { |
| 628 | return nil, fmt.Errorf("failed to get VM details: %w", err) |
| 629 | } |
| 630 | |
| 631 | // Now enrich the VM with guest agent data just like the full refresh does |
| 632 | // This is what was missing - we need to call GetVmStatus to get the enriched data |
| 633 | if freshVM.Status == VMStatusRunning { |
| 634 | // Store the current disk values from GetDetailedVmInfo to preserve them |
| 635 | diskUsage := freshVM.Disk |
| 636 | maxDiskUsage := freshVM.MaxDisk |
| 637 | |
| 638 | // Enrich with guest agent data (network interfaces, filesystems, etc.) |
| 639 | if err := c.GetVmStatus(freshVM); err != nil { |