GetGuestAgentInterfaces retrieves network interface information from the QEMU guest agent.
(vm *VM)
| 35 | |
| 36 | // GetGuestAgentInterfaces retrieves network interface information from the QEMU guest agent. |
| 37 | func (c *Client) GetGuestAgentInterfaces(vm *VM) ([]NetworkInterface, error) { |
| 38 | if vm.Type != VMTypeQemu || vm.Status != VMStatusRunning { |
| 39 | return nil, fmt.Errorf("guest agent not applicable for this VM type or status") |
| 40 | } |
| 41 | |
| 42 | if !vm.AgentEnabled { |
| 43 | return nil, fmt.Errorf("guest agent is not enabled for this VM") |
| 44 | } |
| 45 | |
| 46 | var res map[string]interface{} |
| 47 | |
| 48 | endpoint := fmt.Sprintf("/nodes/%s/qemu/%d/agent/network-get-interfaces", vm.Node, vm.ID) |
| 49 | |
| 50 | // Use GetNoRetry to avoid repeated failed requests if agent is not running |
| 51 | err := c.GetNoRetry(endpoint, &res) |
| 52 | if err != nil { |
| 53 | // Check if the error is due to guest agent not running |
| 54 | if strings.Contains(err.Error(), "QEMU guest agent is not running") { |
| 55 | return nil, fmt.Errorf("QEMU guest agent is not running") |
| 56 | } |
| 57 | |
| 58 | return nil, fmt.Errorf("failed to get guest agent interfaces: %w", err) |
| 59 | } |
| 60 | |
| 61 | data, ok := res["data"].(map[string]interface{}) |
| 62 | if !ok { |
| 63 | return nil, fmt.Errorf("unexpected response format from guest agent") |
| 64 | } |
| 65 | |
| 66 | resultArray, ok := data["result"].([]interface{}) |
| 67 | if !ok { |
| 68 | return nil, fmt.Errorf("unexpected result format from guest agent") |
| 69 | } |
| 70 | |
| 71 | var interfaces []NetworkInterface |
| 72 | |
| 73 | for _, iface := range resultArray { |
| 74 | ifaceMap, ok := iface.(map[string]interface{}) |
| 75 | if !ok { |
| 76 | continue |
| 77 | } |
| 78 | |
| 79 | netInterface := NetworkInterface{} |
| 80 | |
| 81 | // Get interface name and MAC address |
| 82 | if name, ok := ifaceMap["name"].(string); ok { |
| 83 | netInterface.Name = name |
| 84 | netInterface.IsLoopback = name == "lo" || strings.HasPrefix(name, "lo:") |
| 85 | } |
| 86 | |
| 87 | if mac, ok := ifaceMap["hardware-address"].(string); ok { |
| 88 | netInterface.MACAddress = mac |
| 89 | } |
| 90 | |
| 91 | // Parse IP addresses |
| 92 | if ipAddresses, ok := ifaceMap["ip-addresses"].([]interface{}); ok { |
| 93 | for _, ipData := range ipAddresses { |
| 94 | ipMap, ok := ipData.(map[string]interface{}) |
no test coverage detected