netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
563 stars 168 forks source link

Is PUT don't work and PATCH not available in pynetbox? #31

Closed 1NoOne1 closed 6 years ago

1NoOne1 commented 6 years ago

It looks like using HTTL requestes, we can do PUT as well as POST on an object (looking at the swagger api docs). However, I am trying to do the following:

  1. I am trying to create a virtual machine.
  2. Add an interface to the created VM.
  3. Add an IP Address to the interface that we just created.

I am using the netbox version 2.2.8 and pynetbox 3.0.1

Any help is appreciated. Any way that would work other than this is also good.

the first and seconds steps, I got through somehow. [The documentation is pretty sparse and I don't know how to use or modify]. I am trying to use the PUT for the third step...but it is failing.

[root@lab-linux ~]# python netbox_python.py
<pynetbox.api.Api object at 0x2a12d50>
<pynetbox.api.App object at 0x2a12e90>
obj_name from Endpoint class Vrfs
ret value is: <class 'pynetbox.lib.response.IPRecord'>
vrf_obj is : 3
obj_name from Endpoint class IpAddresses
ret value is: <class 'pynetbox.ipam.IpAddresses'>
Traceback (most recent call last):
  File "netbox_python.py", line 11, in <module>
    nb.ipam.ip_addresses.put(address='11.11.11.11/24',status=1,vrf_id=vrf_obj.id,virtual_machine_id=6,interface_id=25)
AttributeError: 'Endpoint' object has no attribute 'put'

I have My code like the below.

import pynetbox

vm_name='vm1'

nb = pynetbox.api('http://xx.xx.xx.xx:8000', token='xxxxxxxxxxxxxxxxx')

print nb

vrf_obj=nb.ipam.vrfs.get(name='test')
print "vrf_obj is : %s" %vrf_obj.id

nb.virtualization.virtual_machines.create(name=vm_name,status=1,cluster='1')

vm_obj = nb.virtualization.virtual_machines.get(name=var_name)

if vm_obj != None:
    nb.virtualization.interfaces.create(virtual_machine=vm_obj.id,name='eth1')
else:
    print "vm with the given name doesn't exists in the netbox inventory."

nb.ipam.ip_addresses.create(address='11.11.11.11/24',status=1,vrf_id=vrf_obj.id)

#Assign the IP ADDR to the interface on the vm

nb.ipam.ip_addresses.put(address='11.11.11.11/24',status=1,vrf_id=vrf_obj.id,virtual_machine_id=6,interface_id=25)
#print ip_obj

quit()
zachmoody commented 6 years ago

Hi, sorry, didn't see this come through. PATCH support is coming, probably via an .update() method, but PUT should work. Have a look at the response docs for how to manipulate an object. Basically, you'll want to .get() change the attributes via assignment (e.g. device.name = 'test') then call .save() to send the PUT.

SA0TAY commented 6 years ago

@zachmoody Something like this?

>>> test3 = nb.virtualization.virtual_machines.get(201)
>>> test3.name = "newname"
>>> test3.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/konaya/.local/lib/python3.6/site-packages/pynetbox/lib/response.py", line 233, in save
    if req.put(self.serialize()):
  File "/home/konaya/.local/lib/python3.6/site-packages/pynetbox/lib/query.py", line 242, in put
    raise RequestError(req)
pynetbox.lib.query.RequestError: The request failed with code 500 Internal Server Error
>>> 
zachmoody commented 6 years ago

Yeah, that should work. I don't have an easy way of seeing what that endpoint expects at the moment, but you can catch the RequestError with something like:

from pynetbox import RequestError
try:
  test3.save()
except RequestError as err:
  raise Exception(err.error)

to get the response from NetBox.

SA0TAY commented 6 years ago

Huh.

<pre><strong>&lt;class &#39;TypeError&#39;&gt;</strong><br />
int() argument must be a string, a bytes-like object or a number, not &#39;dict&#39;</pre>
zachmoody commented 6 years ago

The original request object is also returned in the exception as the .req attribute. I'd have to go digging around in the requests docs to find it, but you should be able to see the body of the request and maybe pick out where the errant dict is from that.