From 39376bf954183a2ee6aa4c1e788307459435ae12 Mon Sep 17 00:00:00 2001 From: Sabuhi Gurbani <51547928+sabuhigr@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:10:53 +0400 Subject: [PATCH] Added Getting vm by id to client There is a method for getting VmRefbyName, but by Id was not exist before.Thats solution for others, who need. --- proxmox/client.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/proxmox/client.go b/proxmox/client.go index 548c2c8f..68c0e86a 100644 --- a/proxmox/client.go +++ b/proxmox/client.go @@ -244,6 +244,35 @@ func (c *Client) GetVmRefsByName(vmName string) (vmrs []*VmRef, err error) { } } +func (c *Client) GetVmRefById(vmId int) (vmr *VmRef, err error) { + var exist bool = false + vms, err := c.GetResourceList(resourceListGuest) + if err != nil { + return + } + for vmii := range vms { + vm := vms[vmii].(map[string]interface{}) + if int(vm["vmid"].(float64)) != 0 && int(vm["vmid"].(float64)) == vmId { + vmr = NewVmRef(int(vm["vmid"].(float64))) + vmr.node = vm["node"].(string) + vmr.vmType = vm["type"].(string) + vmr.pool = "" + if vm["pool"] != nil { + vmr.pool = vm["pool"].(string) + } + if vm["hastate"] != nil { + vmr.haState = vm["hastate"].(string) + } + return + } + } + if !exist { + return nil, fmt.Errorf("vm 'id-%d' not found", vmId) + } else { + return + } +} + func (c *Client) GetVmState(vmr *VmRef) (vmState map[string]interface{}, err error) { err = c.CheckVmRef(vmr) if err != nil {