swayf / proxmoxer

python wrapper for Proxmox API v2 (https://pve.proxmox.com/pve-docs/api-viewer/index.html)
MIT License
156 stars 58 forks source link

Handling config object of vm #41

Closed callahanb73 closed 6 years ago

callahanb73 commented 6 years ago

First, I am pretty new to Python and Proxmox, so I am not sure if this is an issue or just my experience. I apologize in advance for any offense.

I am using the base sample code to get to the /node/nodename/qemu/vmname/config so that I can pull the net0 key in the dictionary and parse the value.

Here is my code.

for node in proxmox.nodes.get(): for vm in proxmox.nodes(node['node']).qemu.get(): print('nodes/{0}/qemu/{1}/config'.format(node['node'], vm['vmid'])) f = proxmox.get('nodes/{0}/qemu/{1}/config'.format(node['node'], vm['vmid'])) print (f)

Here is my output:

nodes/pve5/qemu/116/config <proxmoxer.core.ProxmoxResource object at 0x0000000003FB04A8>

If I run the same in pvesh, I get the dictionary object back, no problem, but I don't want to install pvesh onto the vms.

root@pve3:~# pvesh get /nodes/pve5/qemu/116/config 200 OK { "bootdisk" : "scsi0", "cores" : 4, "digest" : "73af400d29537a6d223403db60b68b7e52c781a9", "ide2" : "none,media=cdrom", "memory" : 4096, "name" : "dat03.int.wscnet.com", "net0" : "virtio=16:E3:62:B7:2F:48,bridge=vmbr0", "net1" : "virtio=36:EE:1F:7A:9E:86,bridge=vmbr1", "numa" : 0, "ostype" : "l26", "scsi0" : "pme-vms1:116/vm-116-disk-1.qcow2,size=32G", "scsi1" : "local-lvm:vm-116-disk-1,size=100G", "scsihw" : "virtio-scsi-pci", "smbios1" : "uuid=0b9dab19-a871-48d4-8105-2b698fa9fac8", "sockets" : 1 }

Is there something that I need to add to my GET request: proxmox.get('nodes/{0}/qemu/{1}/config'.format(node['node'], vm['vmid']))

In order to get the config dictionary? I have also tried several different ways of getting to the dictionary. For example, print(f.dict), just gives me

{'_store': {'session': <proxmoxer.backends.https.ProxmoxHttpSession object at 0x00000000035390B8>, 'serializer': <proxmoxer.backends.https.JsonSerializer object at 0x00000000030764A8>, 'base_url': 'https://pve1.int.wscnet.com:8006/api2/json/get/nodes/pve5/qemu/117/config'}} nodes/pve5/qemu/116/config

I have searched all over and have not found a single example of people attempting this, which I could leverage.

Thanks in advance,

Bill Callahan

jarrodjackson commented 6 years ago

Hello Bill,

I had a very similar problem to the one that you did and was pretty frustrated. This issue helped me understand the design of this wrapper better, so I am responding with my solution:

Use the interactive python interpreter to inspect the proxmox object. I did this by putting a breakpoint in my script import pdb; pdb.set_trace() after I had instantiated it. As you observed, one of the attributes of proxmox object is base_url. The README touches on various different ways to construct a query, but doesn't really explain what is going on under the hood or how to invoke the verbs.

proxmox.get('nodes/{0}/qemu/{1}/config'.format(node['node'], vm['vmid'])) could also be constructed as: proxmox.nodes(node['node']).qemu(vm['vmid']).config.get() (without the variable interpolation).

Your construction (the former) results in: 'base_url': 'https://pve1.int.wscnet.com:8006/api2/json/get/nodes/pve5/qemu/117/config'. Note that 'get' has been inserted into the url after 'json'.

The latter construction results in: 'base_url': 'https://pve1.int.wscnet.com:8006/api2/json/nodes/pve5/qemu/117/config' and then subsequently invokes the GET verb on the url.

Manipulating the proxmox object in the interactive interpreter was key to understanding how to construct the url that I wanted. By thinking of the proxmox object as the base url and the subsequent attributes as elements for constructing the url, I was able to observe how the urls were constructed and how to successfully invoke the get() method.

Thanks for the wrapper @swayf!

-Jarrod

swayf commented 6 years ago

Thank you Jarrod for the help with explanation =).. So I close the issue for now

hocineKHE commented 5 years ago

how we can use base url with CREATE ?