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 create BGP Session object using pynetbox, when peer_group is supplied in the args #46

Closed
pspdbx opened this issue Jul 1, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@pspdbx
Copy link

pspdbx commented Jul 1, 2021

NetBox version
2.11.2

Describe the bug
Unable to create BGP Session object using pynetbox, when peer_group is supplied in the args.

To Reproduce
nb_staging in the output below is my instance of pynetbox.api

In [943]: object_dict
Out[943]:
{'import_policies': [51, 66, 18],
 'export_policies': [60, 18],
 'description': 'LEVEL3',
 'peer_group': 8,
 'local_address': {'address': '4.71.253.6/30'},
 'remote_address': {'address': '4.71.253.5/30'},
 'local_as': {'number': 12345},
 'remote_as': {'number': 3356},
 'name': '3356_TRANSIT_LEVEL3_mydevice',
 'device': {'name': 'mydevice'}}

In [944]: nb_staging.plugins.bgp.session.create(object_dict)

RequestError                              Traceback (most recent call last)
<ipython-input-944-5e86e2d8ef9b> in <module>()
----> 1 nb_staging.plugins.bgp.session.create(object_dict)

/netbox_dev/nsot-ipython.runfiles/__main__/pip/pynetbox/pynetbox-cpython-38/lib/pynetbox/core/endpoint.py in create(self, *args, **kwargs)
    302         """
    303
--> 304         req = Request(
    305             base=self.url,
    306             token=self.token,

/netbox_dev/nsot-ipython.runfiles/__main__/pip/pynetbox/pynetbox-cpython-38/lib/pynetbox/core/query.py in post(self, data)
    385         :Returns: Dict containing the response from NetBox's API.
    386         """
--> 387         return self._make_call(verb="post", data=data)
    388
    389     def delete(self):

/netbox_dev/nsot-ipython.runfiles/__main__/pip/pynetbox/pynetbox-cpython-38/lib/pynetbox/core/query.py in _make_call(self, verb, url_override, add_par
ams, data)
    280                 raise ContentError(req)
    281         else:
--> 282             raise RequestError(req)
    283
    284     def concurrent_get(self, ret, page_size, page_offsets):

RequestError: The request failed with code 500 Internal Server Error: {'error': "argument of type 'BGPPeerGroup' is not iterable", 'exception': 'TypeError', 'netbox_
version': '2.11.2', 'python_version': '3.8.8+'}

Expected behavior
The BGP Session object gets created

Screenshots
n/a - the traceback above should be sufficient.

Additional context
I have also tried changing
object_dict['peer_group'] = {'name': 'TRANSIT'}
but I still get the same error. Neither referencing related object by a dictionary of attributes nor by its ID seem to work.

If I remove the 'peer_group' key from object_dict altogether, the BGP session object gets created successfully. However, thats not what I would like. I'd like to relate the session to a peer group.

@pspdbx pspdbx added the bug Something isn't working label Jul 1, 2021
@pspdbx
Copy link
Author

pspdbx commented Jul 1, 2021

I would add that the problem is not just during creation. If I create the BGP Session object without relating it any Peer Group, and later try to update the Session object, I get the same error.

# nb is my pynetbox.api client

In [42]: foo = nb.plugins.bgp.session.get(1)

In [43]: foo.peer_group

In [44]: foo.peer_group = {'id': 1,
    ...:   'name': 'mypeergroup',
    ...:   }

In [45]: foo.save()
---------------------------------------------------------------------------
RequestError                              Traceback (most recent call last)
<ipython-input-45-0dd54f04032b> in <module>
----> 1 foo.save()

~/ctf2020/lib/python3.7/site-packages/pynetbox/core/response.py in save(self)
    403                     http_session=self.api.http_session,
    404                 )
--> 405                 if req.patch({i: serialized[i] for i in diff}):
    406                     return True
    407

~/ctf2020/lib/python3.7/site-packages/pynetbox/core/query.py in patch(self, data)
    413         :returns: Dict containing the response from NetBox's API.
    414         """
--> 415         return self._make_call(verb="patch", data=data)
    416
    417     def options(self):

~/ctf2020/lib/python3.7/site-packages/pynetbox/core/query.py in _make_call(self, verb, url_override, add_params, data)
    278                 raise ContentError(req)
    279         else:
--> 280             raise RequestError(req)
    281
    282     def concurrent_get(self, ret, page_size, page_offsets):

RequestError: The request failed with code 500 Internal Server Error: {'error': "argument of type 'BGPPeerGroup' is not iterable", 'exception': 'TypeError', 'netbox_version': '2.11.2', 'python_version': '3.8.8+'}

@k01ek k01ek self-assigned this Jul 7, 2021
@pspdbx
Copy link
Author

pspdbx commented Jul 8, 2021

Both creating and updating a BGP Session with Peer Group, works fine over the GUI. The problem only seems to be when pynetbox is used to either create/update BGP session with a related peer_group. Also, removing a related peer_group from a BGP session works fine over the pynetbox client/API.

k01ek added a commit that referenced this issue Jul 9, 2021
@k01ek
Copy link
Collaborator

k01ek commented Jul 9, 2021

I have dug into this bug and found the reason. There is some problem in rest_framework with nested serializer and unique together validation. I found the workaround to fix the problem: remove validation for the nested serializer, but I afraid it can have some side effects. You can use this fix https://github.com/k01ek/netbox-bgp/tree/quick-fix-46
I will look for a more elegant solution.

@pspdbx
Copy link
Author

pspdbx commented Jul 12, 2021

Thank you @k01ek

@pspdbx
Copy link
Author

pspdbx commented Jul 22, 2021

What is the impact of rolling this fix into the master branch @k01ek ? After all, the change is restricted only to BGP Peer Group Serializer

@petercrocker
Copy link

Just ran in to this as well. I've just avoided peer groups for the short term until this can be further tested and rolled in to master.

@pspdbx
Copy link
Author

pspdbx commented Aug 5, 2021

yeah, I'm doing the same for the time being. Can't wait for it to get rolled into the master branch

@k01ek
Copy link
Collaborator

k01ek commented Aug 24, 2021

Sorry guys, I was on vacation. Will update it soon.

k01ek added a commit that referenced this issue Aug 25, 2021
@k01ek k01ek mentioned this issue Aug 25, 2021
@k01ek k01ek closed this as completed Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants