Skip to content

Commit

Permalink
Added Getting vm by id to client
Browse files Browse the repository at this point in the history
There is a method for getting VmRefbyName, but by Id was not exist before.Thats solution for others, who need.
  • Loading branch information
sabuhigr committed Nov 20, 2023
1 parent 03f4e42 commit 39376bf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 39376bf

Please sign in to comment.