Response Module

class pynautobot.core.response.JsonField

Bases: object

Explicit field type for values that are not to be converted to a Record object

class pynautobot.core.response.Record(values, api, endpoint)

Bases: object

Create python objects from nautobot API responses.

Creates an object from a Nautobot response passed as values. Nested dicts that represent other endpoints are also turned into Record objects. All fields are then assigned to the object’s attributes. If a missing attr is requested (e.g. requesting a field that’s only present on a full response on a Record made from a nested response) the pynautobot will make a request for the full object and return the requsted value.

Examples:

Default representation of the object usually contains object’s class, object’s name and it’s location in memory

>>> x = nb.dcim.devices.get(1)
>>> x
<pynautobot.models.dcim.Devices ('test1-switch1') at 0x1953d821250>
>>>

Querying a string field.

>>> x = nb.dcim.devices.get(1)
>>> x.serial
'ABC123'
>>>

Querying a field on a nested object.

>>> x = nb.dcim.devices.get(1)
>>> x.device_type.model
'QFX5100-24Q'
>>>

Casting the object as a dictionary.

>>> from pprint import pprint
>>> pprint(dict(x))
{'asset_tag': None,
 'cluster': None,
 'comments': '',
 'config_context': {},
 'created': '2018-04-01',
 'custom_fields': {},
 'device_role': {'id': 1,
                 'name': 'Test Switch',
                 'slug': 'test-switch',
                 'url': 'http://localhost:8000/api/dcim/device-roles/1/'},
 'device_type': {...},
 'display_name': 'test1-switch1',
 'face': {'label': 'Rear', 'value': 1},
 'id': 1,
 'name': 'test1-switch1',
 'parent_device': None,
 'platform': {...},
 'position': 1,
 'primary_ip': {'address': '192.0.2.1/24',
                'family': 4,
                'id': 1,
                'url': 'http://localhost:8000/api/ipam/ip-addresses/1/'},
 'primary_ip4': {...},
 'primary_ip6': None,
 'rack': {'display_name': 'Test Rack',
          'id': 1,
          'name': 'Test Rack',
          'url': 'http://localhost:8000/api/dcim/racks/1/'},
 'serial': 'ABC123',
 'site': {'id': 1,
          'name': 'TEST',
          'slug': 'TEST',
          'url': 'http://localhost:8000/api/dcim/sites/1/'},
 'status': {'label': 'Active', 'value': 1},
 'tags': [],
 'tenant': None,
 'vc_position': None,
 'vc_priority': None,
 'virtual_chassis': None}
 >>>

Iterating over a Record object.

>>> for i in x:
...  print(i)
...
('id', 1)
('name', 'test1-switch1')
('display_name', 'test1-switch1')
>>>
__init__(values, api, endpoint)
delete()

Deletes an existing object.

Returns:

True if DELETE operation was successful.

Example:

>>> x = nb.dcim.devices.get(name='test1-a3-tor1b')
>>> x.delete()
True
>>>
full_details()

Queries the hyperlinked endpoint if ‘url’ is defined.

This method will populate the attributes from the detail endpoint when it’s called. Sets the class-level has_details attribute when it’s called to prevent being called more than once.

Returns:

True

save()

Saves changes to an existing object.

Takes a diff between the objects current state and its state at init and sends them as a dictionary to Request.patch().

Returns:

True if PATCH request was successful.

Example:

>>> x = nb.dcim.devices.get(name='test1-a3-tor1b')
>>> x.serial
u''
>>> x.serial = '1234'
>>> x.save()
True
>>>
serialize(nested=False, init=False)

Serializes an object

Pulls all the attributes in an object and creates a dict that can be turned into the json that nautobot is expecting.

If an attribute’s value is a Record type it’s replaced with the id field of that object.

Note

Using this to get a dictionary representation of the record is discouraged. It’s probably better to cast to dict() instead. See Record docstring for example.

Returns:

dict.

update(data)

Update an object with a dictionary.

Accepts a dict and uses it to update the record and call save(). For nested and choice fields you’d pass an int the same as if you were modifying the attribute and calling save().

Parameters:

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

Returns:

True if PATCH request was successful.

Example:

>>> x = nb.dcim.devices.get(1)
>>> x.update({
...   "name": "test-switch2",
...   "serial": "ABC321",
... })
True
url = None
pynautobot.core.response.get_return(lookup, return_fields=None)

Returns simple representations for items passed to lookup.

Used to return a “simple” representation of objects and collections sent to it via lookup. Otherwise, we look to see if lookup is a “choices” field (dict with only ‘id’ and ‘value’) or a nested_return. Finally, we check if it’s a Record, if so simply return a string. Order is important due to nested_return being self-referential.

Parameters:

return_fields (list,optional) – A list of fields to reference when calling values on lookup.