netbox-community / pynetbox

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

Can't create virtual machines #498

Closed syncopsta closed 1 year ago

syncopsta commented 2 years ago

Code:

import pynetbox
nb = pynetbox.api('http://localhost', token=<token>)
nb.http_session.verify = False

vm = nb.virtualization.virtual_machines.create(dict(
        name="testing",
        cluster={"name":"prx"}
))
print(repr(vm))
vm.save()

Error:

/usr/lib/python3/dist-packages/urllib3/connectionpool.py:1015: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(

Traceback (most recent call last):
  File "/root/test.py", line 10, in <module>
    vm.save()
  File "/usr/local/lib/python3.9/dist-packages/pynetbox/core/response.py", line 529, in save
    updates = self.updates()
  File "/usr/local/lib/python3.9/dist-packages/pynetbox/core/response.py", line 505, in updates
    if self.id:
  File "/usr/local/lib/python3.9/dist-packages/pynetbox/core/response.py", line 298, in __getattr__
    raise AttributeError('object has no attribute "{}"'.format(k))
AttributeError: object has no attribute "id"

root@netbox:~# pip3 show pynetbox Name: pynetbox Version: 6.6.2

Netbox version is v3.3.2

Am I doing something wrong?

markkuleinio commented 2 years ago

Works here:

>>> netbox.status()["netbox-version"]
'3.3.4'
>>> pynetbox.__version__
'6.6.2'
>>> vm = netbox.virtualization.virtual_machines.create(dict(name="TestVM123", cluster={"name":"Cluster1"}))
>>> vm
TestVM123
>>> print(repr(vm))
TestVM123
>>> vm.save()
False
>>>

Am I doing something wrong?

Probably, my guess is that you have incorrect URL:

http://localhost

InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'

so apparently there is a redirect that messes up the results.

arthanson commented 1 year ago

Closing this for cleanup see markkuleinio note above.