Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use .save() on tenant #304

Closed
ziggekatten opened this issue Dec 8, 2020 · 3 comments · Fixed by #308
Closed

Unable to use .save() on tenant #304

ziggekatten opened this issue Dec 8, 2020 · 3 comments · Fixed by #308

Comments

@ziggekatten
Copy link

ziggekatten commented Dec 8, 2020

Netbox 2.9.10
pynetbox 5.1.0

Creating a tenant works:

tenant = nb.tenancy.tenants.create({'name': 'Tenant', 'slug': 'tenant'})

Trying to set the group on tenant:

tenant.group = 1

Doing tenant.save() results in:

pynetbox.core.query.RequestError: The requested url: https://server1.int.net/ipam-stage/api/api/tenancy/257/ could not be found

doing pprint(dict(tenant)):

{'comments': '',
'created': '2020-12-08',
'custom_fields': {},
'description': '',
'group': 1,
'id': 258,
'last_updated': '2020-12-08T19:28:12.002493Z',
'name': 'Tenant',
'slug': 'tenant',
'tags': [],
'url': 'https://server1.int.net/ipam-stage/api/tenancy/tenants/258/'}

It sees that pynetbox looses the "tenants" in the url between tenancy and id of tenant

@markkuleinio
Copy link
Contributor

markkuleinio commented Dec 12, 2020

With NetBox 2.9.11 and pynetbox 5.1.0 here, I don't see the problem:

>>> t = netbox.tenancy.tenants.create(dict(name="Test", slug="test"))
>>> pprint(dict(t))
{'comments': '',
 'created': '2020-12-12',
 'custom_fields': {},
 'description': '',
 'group': None,
 'id': 4,
 'last_updated': '2020-12-12T14:28:05.332504+02:00',
 'name': 'Test',
 'slug': 'test',
 'tags': [],
 'url': 'http://netbox-test.example.net/api/tenancy/tenants/4/'}
>>> netbox.tenancy.tenant_groups.get(1)
Testgroup
>>> t.group = 1
>>> t.save()
True
>>> pprint(dict(t))
{'comments': '',
 'created': '2020-12-12',
 'custom_fields': {},
 'description': '',
 'group': 1,
 'id': 4,
 'last_updated': '2020-12-12T14:28:05.332504+02:00',
 'name': 'Test',
 'slug': 'test',
 'tags': [],
 'url': 'http://netbox-test.example.net/api/tenancy/tenants/4/'}
>>>

In your test you also have double /api in the path, maybe you can figure out what is messing up with your URLs?

Edit: I see the last_updated field is the same in my example before and after setting the tenant group. Maybe related to this: netbox-community/netbox#5419 (unrelated to your URL problem anyway)

@marcelb98
Copy link

I have a similar issue with Netbox 2.9.7 and pynetbox 5.1.0.

Creating a new device with

new_device = nb.dcim.devices.create(
            name="AP {}".format(serial),
            serial=serial,
            device_type=type,
            device_role=25,
            site=Config.netbox_stock_site_id,
            rack=Config.netbox_stock_rack_id
        )

is successful, but upgrading the interface of the new device (automatically created by Netbox since it is in the device type) with

interface = nb.dcim.interfaces.filter(device_id=new_device.id)[0]
interface.update({
            "mac_address": mac
        })

is not successful. It throws the following error:
pynetbox.core.query.RequestError: The requested url: https://my.tld/netbox/api/api/dcim/12037/ could not be found.

The Netbox is installed at https://my.tld/netbox. Since pynetbox is able to create the device (12037 is also the ID of the interface which was created), I provided the Netbox URI correctly to pynetbox.

So maybe .save() is forming the URL incorretly (as .upgrade() is also using .save() in the background).

@markkuleinio
Copy link
Contributor

Looks like the problem is in Record._endpoint_from_url() (affects NetBox URLs with a directory component). I will look at it later this week unless @zachmoody gets it first

zachmoody pushed a commit that referenced this issue Dec 15, 2020
Fixes #304: Change Record._endpoint_from_url() to count URL fields from the end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants