Endpoint Module

class pynautobot.core.endpoint.DetailEndpoint(parent_obj, name, custom_return=None)

Bases: object

Enables read/write Operations on detail endpoints.

Endpoints like available-ips that are detail routes off traditional endpoints are handled with this class.

__init__(parent_obj, name, custom_return=None)
create(data=None, api_version=None)

The write operation for a detail endpoint.

Creates objects on a detail endpoint in Nautobot.

Parameters:

data (dict/list,optional) – A dictionary containing the key/value pair of the items you’re creating on the parent object. Defaults to empty dict which will create a single item with default values.

Args str,optional api_version:

Override default or globally set Nautobot REST API version for this single request.

Returns:

A dictionary or list of dictionaries its created in Nautobot.

list(api_version=None, **kwargs)

The view operation for a detail endpoint

Returns the response from Nautobot for a detail endpoint.

:param : arg str,optional api_version: Override default or globally set Nautobot REST API version for this single request. :param **kwargs: key/value pairs that get converted into url parameters when passed to the endpoint.

E.g. .list(method='get_facts') would be converted to .../?method=get_facts.

Returns:

A dictionary or list of dictionaries retrieved from Nautobot.

class pynautobot.core.endpoint.Endpoint(api, app, name, model=None)

Bases: object

Represent actions available on endpoints in the Nautobot API.

Takes name and app passed from App() and builds the correct url to make queries to and the proper Response object to return results in.

Parameters:
  • api (obj) – Takes Api created at instantiation.

  • app (obj) – Takes App.

  • name (str) – Name of endpoint passed to App().

  • model (obj,optional) – Custom model for given app.

Note

In order to call Nautobot 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().)

__init__(api, app, name, model=None)
all(api_version=None)

Queries the ‘ListView’ of a given endpoint.

Returns all objects from an endpoint.

Parameters:

api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

Returns:

List of Record objects.

Examples:

>>> nb.dcim.devices.all()
[test1-a3-oobsw2, test1-a3-oobsw3, test1-a3-oobsw4]
>>>
choices(api_version=None)

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 Nautobot. When using .choices() in long-running applications, consider restarting them whenever Nautobot is upgraded, to prevent using stale choices data.

Parameters:

api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

Returns:

Dict containing the available choices.

Example (from Nautobot 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, api_version=None, **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.

  • api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

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, api_version=None, **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. Nautobot’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.

  • api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

Returns:

A list or single Record object depending on whether a bulk creation was requested.

Examples:

Creating an object on the devices endpoint you can lookup a device_role’s name with:

>>> nautobot.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
...     }
... ])
filter(*args, api_version=None, **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.

  • api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

Returns:

A list of Record objects.

Examples:

To return a list of objects matching a named argument filter.

>>> nb.dcim.devices.filter(role='leaf-switch')
[test1-a3-tor1b, test1-a3-tor1c, test1-a3-tor1d, test1-a3-tor2a]
>>>

Using a freeform query along with a named argument.

>>> nb.dcim.devices.filter('a3', role='leaf-switch')
[test1-a3-tor1b, test1-a3-tor1c, test1-a3-tor1d, test1-a3-tor2a]
>>>

Chaining multiple named arguments.

>>> nb.dcim.devices.filter(role='leaf-switch', status=True)
[test1-leaf2]
>>>

Passing a list as a named argument adds multiple filters of the same value.

>>> nb.dcim.devices.filter(role=['leaf-switch', 'spine-switch'])
[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.

  • api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

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(id: str, data: Dict[str, any])

Update a resource with a dictionary.

Accepts the id of the object that needs to be updated as well as a dictionary of k/v pairs used to update an object. The object is directly updated on the server using a PATCH request without fetching object information.

For fields requiring an object reference (such as a device location), the API user is responsible for providing the object ID or the object URL. This API will not accept the pynautobot object directly.

Parameters:
  • id (str) – Identifier of the object being updated

  • data (dict) – Dictionary containing the k/v to update the record object with.

Returns:

True if PATCH request was successful.

Example:

>>> nb.dcim.devices.update(id="0238a4e3-66f2-455a-831f-5f177215de0f", data={
...     "name": "test-switch2",
...     "serial": "ABC321",
...     "location": "9b1f53c7-89fa-4fb2-a89a-b97364fef50c",
... })
True
class pynautobot.core.endpoint.JobsEndpoint(api, app, name, model=None)

Bases: Endpoint

Extend Endpoint class to support run method only for jobs.

run(*args, api_version=None, **kwargs)

Runs a job based on the class_path provided to the job.

Takes a kwarg of class_path or job_id to specify the job that should be run.

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.

  • api_version (str,optional) – Override default or globally-set Nautobot REST API version for this single request.

Returns:

Job details: job_result object uuid found at obj.result.id.

Examples:

To return a count of objects matching a named argument filter.

>>> nb.extras.jobs.run(
        class_path="local/data_quality/VerifyHostnames",
        data={"hostname_regex": ".*"},
        commit=True,
    )
>>>
class pynautobot.core.endpoint.RODetailEndpoint(parent_obj, name, custom_return=None)

Bases: DetailEndpoint

create(data)

The write operation for a detail endpoint.

Creates objects on a detail endpoint in Nautobot.

Parameters:

data (dict/list,optional) – A dictionary containing the key/value pair of the items you’re creating on the parent object. Defaults to empty dict which will create a single item with default values.

Args str,optional api_version:

Override default or globally set Nautobot REST API version for this single request.

Returns:

A dictionary or list of dictionaries its created in Nautobot.

pynautobot.core.endpoint.response_loader(req, return_obj, endpoint)