Endpoint¶
- class pynetbox.core.endpoint.Endpoint(api, app, name, model=None)¶
Represent actions available on endpoints in the Netbox API.
Takes
name
andapp
passed from App() and builds the correct url to make queries to and the proper Response object to return results in.- Parameters
Note
In order to call NetBox endpoints with dashes in their names you should convert the dash to an underscore. (E.g. querying the ip-addresses endpoint is done with
nb.ipam.ip_addresses.all()
.)- all(limit=0, offset=None)¶
Queries the ‘ListView’ of a given endpoint.
Returns all objects from an endpoint.
- Parameters
limit (int,optional) – Overrides the max page size on paginated returns.
offset (int,optional) – Overrides the offset on paginated returns.
- Returns
A
RecordSet
object.- Examples
>>> devices = nb.dcim.devices.all() >>> for device in devices: ... print(device.name) ... test1-leaf1 test1-leaf2 test1-leaf3 >>>
- choices()¶
Returns all choices from the endpoint.
The returned dict is also saved in the endpoint object (in
_choices
attribute) so that later calls will return the same data without recurring requests to NetBox. When using.choices()
in long-running applications, consider restarting them whenever NetBox is upgraded, to prevent using stale choices data.- Returns
Dict containing the available choices.
- Example (from NetBox 2.8.x)
>>> from pprint import pprint >>> pprint(nb.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'}]} >>>
- count(*args, **kwargs)¶
Returns the count of objects in a query.
Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it’s used as a freeform search argument if the endpoint supports it. If no arguments are passed the count for all objects on an endpoint are returned.
- Parameters
*args (str,optional) – Freeform search string that’s accepted on given endpoint.
**kwargs (str,optional) – Any search argument the endpoint accepts can be added as a keyword arg.
- Returns
Integer with count of objects returns by query.
- Examples
To return a count of objects matching a named argument filter.
>>> nb.dcim.devices.count(site='tst1') 5827 >>>
To return a count of objects on an entire endpoint.
>>> nb.dcim.devices.count() 87382 >>>
- create(*args, **kwargs)¶
Creates an object on an endpoint.
Allows for the creation of new objects on an endpoint. Named arguments are converted to json properties, and a single object is created. NetBox’s bulk creation capabilities can be used by passing a list of dictionaries as the first argument.
- Parameters
*args (list) – A list of dictionaries containing the properties of the objects to be created.
**kwargs (str) – key/value strings representing properties on a json object.
- Returns
A list or single
Record
object depending on whether a bulk creation was requested.- Examples
Creating an object on the devices endpoint:
>>> device = netbox.dcim.devices.create( ... name='test', ... device_role=1, ... ) >>>
Use bulk creation by passing a list of dictionaries:
>>> nb.dcim.devices.create([ ... { ... "name": "test1-core3", ... "device_role": 3, ... "site": 1, ... "device_type": 1, ... "status": 1 ... }, ... { ... "name": "test1-core4", ... "device_role": 3, ... "site": 1, ... "device_type": 1, ... "status": 1 ... } ... ])
- delete(objects)¶
Bulk deletes objects on an endpoint.
Allows for batch deletion of multiple objects from a single endpoint
- Parameters
objects (list) – A list of either ids or Records or a single RecordSet to delete.
- Returns
True if bulk DELETE operation was successful.
- Examples
Deleting all devices:
>>> netbox.dcim.devices.delete(netbox.dcim.devices.all(0)) >>>
Use bulk deletion by passing a list of ids:
>>> netbox.dcim.devices.delete([2, 243, 431, 700]) >>>
Use bulk deletion to delete objects eg. when filtering on a custom_field: >>> netbox.dcim.devices.delete([ >>> d for d in netbox.dcim.devices.all(0) >>> if d.custom_fields.get(‘field’, False) >>> ]) >>>
- filter(*args, **kwargs)¶
Queries the ‘ListView’ of a given endpoint.
Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it’s used as a freeform search argument if the endpoint supports it.
- Parameters
*args (str,optional) – Freeform search string that’s accepted on given endpoint.
**kwargs (str,optional) – Any search argument the endpoint accepts can be added as a keyword arg.
limit (int,optional) – Overrides the max page size on paginated returns.
offset (int,optional) – Overrides the offset on paginated returns.
- Returns
A
RecordSet
object.- Examples
To return a list of objects matching a named argument filter.
>>> devices = nb.dcim.devices.filter(role='leaf-switch') >>> for device in devices: ... print(device.name) ... test1-leaf1 test1-leaf2 test1-leaf3 >>>
Using a freeform query along with a named argument.
>>> devices = nb.dcim.devices.filter('a3', role='leaf-switch') >>> for device in devices: ... print(device.name) ... test1-a3-leaf1 test1-a3-leaf2 >>>
Chaining multiple named arguments.
>>> devices = nb.dcim.devices.filter(role='leaf-switch', status=True) >>> for device in devices: ... print(device.name) ... test1-leaf2 >>>
Passing a list as a named argument adds multiple filters of the same value.
>>> device = nb.dcim.devices.filter(role=['leaf-switch', 'spine-switch']) >>> for device in devices: ... print(device.name) ... test1-a3-spine1 test1-a3-spine2 test1-a3-leaf1 >>>
- get(*args, **kwargs)¶
Queries the DetailsView of a given endpoint.
- Parameters
key (int,optional) – id for the item to be retrieved.
**kwargs (str,optional) – Accepts the same keyword args as filter(). Any search argument the endpoint accepts can be added as a keyword arg.
- Returns
A single
Record
object or None- Raises
ValueError – if kwarg search return more than one value.
- Examples
Referencing with a kwarg that only returns one value.
>>> nb.dcim.devices.get(name='test1-a3-tor1b') test1-a3-tor1b >>>
Referencing with an id.
>>> nb.dcim.devices.get(1) test1-edge1 >>>
- update(objects)¶
Bulk updates existing objects on an endpoint.
Allows for bulk updating of existing objects on an endpoint. Objects is a list whic contain either json/dicts or Record derived objects, which contain the updates to apply. If json/dicts are used, then the id of the object must be included
- Parameters
objects (list) – A list of dicts or Record.
- Returns
True if the update succeeded
- Examples
Updating objects on the devices endpoint:
>>> device = netbox.dcim.devices.update([ ... {'id': 1, 'name': 'test'}, ... {'id': 2, 'name': 'test2'}, ... ]) >>> True
Use bulk update by passing a list of Records:
>>> devices = nb.dcim.devices.all() >>> for d in devices: >>> d.name = d.name+'-test' >>> nb.dcim.devices.update(devices) >>> True
- class pynetbox.core.endpoint.DetailEndpoint(parent_obj, name, custom_return=None)¶
Enables read/write operations on detail endpoints.
Endpoints like
available-ips
that are detail routes off traditional endpoints are handled with this class.- create(data=None)¶
The write operation for a detail endpoint.
Creates objects on a detail endpoint in NetBox.
- list(**kwargs)¶
The view operation for a detail endpoint
Returns the response from NetBox for a detail endpoint.