Netbox: Deprecate the _choices API endpoints

Created on 9 Aug 2019  路  9Comments  路  Source: netbox-community/netbox

Environment

  • Python version: 3.5.2
  • NetBox version: 2.6.2

Proposed Change

The NetBox REST API provides a _choices endpoint under each app (e.g. /api/circuits/_choices/) which lists the available values and labels for each choice field within the app. As @lampwins recently pointed out, the Django REST Framework exposes these choices (as well as other useful field attributes) via an OPTIONS HTTP request.

More investigation is needed, however it is likely that we can ditch the statically defined _choices endpoints in favor of using the built-in OPTIONS behavior.

accepted deprecation

Most helpful comment

FYI, the new Endpoint.choices() has been merged in master branch of pynetbox:

https://pynetbox.readthedocs.io/en/latest/endpoint.html#pynetbox.core.endpoint.Endpoint.choices

>>> from pprint import pprint
>>> pprint(netbox.ipam.ip_addresses.choices())
{'role': [{'display_name': 'Secondary', 'value': 20},
          {'display_name': 'VIP', 'value': 40},
          {'display_name': 'VRRP', 'value': 41},
          {'display_name': 'Loopback', 'value': 10},
          {'display_name': 'GLBP', 'value': 43},
          {'display_name': 'CARP', 'value': 44},
          {'display_name': 'HSRP', 'value': 42},
          {'display_name': 'Anycast', 'value': 30}],
 'status': [{'display_name': 'Active', 'value': 1},
            {'display_name': 'Reserved', 'value': 2},
            {'display_name': 'Deprecated', 'value': 3},
            {'display_name': 'DHCP', 'value': 5}]}
>>>

All 9 comments

Also of note, the OPTIONS method uses the keys display_name and value, so we should look at deprecating the use of label in the ChoiceField, used for Swagger introspection. This is also more in line with Django naming convention anyway.

pynetbox needs to be updated to use the OPTIONS retrieval in step with the deprecation. Perhaps that should happen sooner so that by the time the _choices API is removed, pynetbox users will be none-the-wiser.

Is there a minimum NetBox version where this OPTIONS interface is available?

I sent a PR in pynetbox to implement the OPTIONS request, but if there is some version dependency then it needs to be mentioned in pynetbox documentation.

@markkuleinio no, this is a feature of DRF and has always been available in netbox.

FYI, the new Endpoint.choices() has been merged in master branch of pynetbox:

https://pynetbox.readthedocs.io/en/latest/endpoint.html#pynetbox.core.endpoint.Endpoint.choices

>>> from pprint import pprint
>>> pprint(netbox.ipam.ip_addresses.choices())
{'role': [{'display_name': 'Secondary', 'value': 20},
          {'display_name': 'VIP', 'value': 40},
          {'display_name': 'VRRP', 'value': 41},
          {'display_name': 'Loopback', 'value': 10},
          {'display_name': 'GLBP', 'value': 43},
          {'display_name': 'CARP', 'value': 44},
          {'display_name': 'HSRP', 'value': 42},
          {'display_name': 'Anycast', 'value': 30}],
 'status': [{'display_name': 'Active', 'value': 1},
            {'display_name': 'Reserved', 'value': 2},
            {'display_name': 'Deprecated', 'value': 3},
            {'display_name': 'DHCP', 'value': 5}]}
>>>

I see pynetbox 4.1.0 has now been released with the .choices() method in the endpoint objects (among other enhancements).

Just to show an example of Endpoint.choices() with the string values in 2.7-beta1, works fine:

>>> pynetbox.__version__
'4.2.2'
>>> netbox.version
'2.7'
>>> pprint(netbox.ipam.ip_addresses.choices())
{'role': [{'display_name': 'Loopback', 'value': 'loopback'},
          {'display_name': 'Secondary', 'value': 'secondary'},
          {'display_name': 'Anycast', 'value': 'anycast'},
          {'display_name': 'VIP', 'value': 'vip'},
          {'display_name': 'VRRP', 'value': 'vrrp'},
          {'display_name': 'HSRP', 'value': 'hsrp'},
          {'display_name': 'GLBP', 'value': 'glbp'},
          {'display_name': 'CARP', 'value': 'carp'}],
 'status': [{'display_name': 'Active', 'value': 'active'},
            {'display_name': 'Reserved', 'value': 'reserved'},
            {'display_name': 'Deprecated', 'value': 'deprecated'},
            {'display_name': 'DHCP', 'value': 'dhcp'}]}
>>>

For swagger, we need to change label to display_name in CustomChoiceFieldInspector in addition to the ChoiceField.

For swagger, we need to change label to display_name in CustomChoiceFieldInspector in addition to the ChoiceField.

IIRC at some point we had discussed moving the other way: changing value to display_name in the ChoiceField serializer. I've opened #4358 to track this separately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VictorJ76 picture VictorJ76  路  3Comments

robbagithub picture robbagithub  路  3Comments

markve-sa picture markve-sa  路  4Comments

mrfroggg picture mrfroggg  路  3Comments

benjy44 picture benjy44  路  3Comments