Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_count_no_filters(self):
test_obj = Request(
http_session=Mock(),
base="http://localhost:8001/api/dcim/devices",
)
test_obj.http_session.get.return_value.json.return_value = {
"count": 42,
"next": "http://localhost:8001/api/dcim/devices?limit=1&offset=1",
"previous": False,
"results": [],
}
test_obj.http_session.get.ok = True
test = test_obj.get_count()
self.assertEqual(test, 42)
test_obj.http_session.get.assert_called_with(
"http://localhost:8001/api/dcim/devices/",
params={"limit": 1},
headers={"accept": "application/json;"},
def all(self):
"""Queries the 'ListView' of a given endpoint.
Returns all objects from an endpoint.
:Returns: List of :py:class:`.Record` objects.
:Examples:
>>> nb.dcim.devices.all()
[test1-a3-oobsw2, test1-a3-oobsw3, test1-a3-oobsw4]
>>>
"""
req = Request(
base="{}/".format(self.url),
token=self.token,
session_key=self.session_key,
ssl_verify=self.ssl_verify,
http_session=self.api.http_session,
)
return [self._response_loader(i) for i in req.get()]
>>> nb.dcim.devices.count()
87382
>>>
"""
if args:
kwargs.update({"q": args[0]})
if any(i in RESERVED_KWARGS for i in kwargs):
raise ValueError(
"A reserved {} kwarg was passed. Please remove it "
"try again.".format(RESERVED_KWARGS)
)
ret = Request(
filters=kwargs,
base=self.url,
token=self.token,
session_key=self.session_key,
ssl_verify=self.ssl_verify,
http_session=self.api.http_session,
)
return ret.get_count()
{'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}]}
>>>
"""
if self._choices:
return self._choices
req = Request(
base=self.url,
token=self.api.token,
private_key=self.api.private_key,
ssl_verify=self.api.ssl_verify,
http_session=self.api.http_session,
).options()
try:
post_data = req['actions']['POST']
except KeyError:
raise ValueError(
"Unexpected format in the OPTIONS response at {}".format(
self.url
)
)
self._choices = {}
for prop in post_data:
"""
if args:
kwargs.update({"q": args[0]})
if not kwargs:
raise ValueError(
"filter must be passed kwargs. Perhaps use all() instead."
)
if any(i in RESERVED_KWARGS for i in kwargs):
raise ValueError(
"A reserved {} kwarg was passed. Please remove it "
"try again.".format(RESERVED_KWARGS)
)
req = Request(
filters=kwargs,
base=self.url,
token=self.token,
session_key=self.session_key,
ssl_verify=self.ssl_verify,
http_session=self.api.http_session,
)
ret = [self._response_loader(i) for i in req.get()]
return ret
def delete(self):
"""Deletes an existing object.
:returns: True if DELETE operation was successful.
:example:
>>> x = nb.dcim.devices.get(name='test1-a3-tor1b')
>>> x.delete()
True
>>>
"""
req = Request(
key=self.id,
base=self.endpoint.url,
token=self.api.token,
session_key=self.api.session_key,
ssl_verify=self.api.ssl_verify,
http_session=self.api.http_session,
)
return True if req.delete() else False
version-dependent features or syntaxes in the API.
:Returns: Version number as a string.
:Example:
>>> import pynetbox
>>> nb = pynetbox.api(
... 'http://localhost:8000',
... private_key_file='/path/to/private-key.pem',
... token='d6f4e314a5b5fefd164995169f28ae32d987704f'
... )
>>> nb.version
'2.6'
>>>
"""
version = Request(
base=self.base_url,
ssl_verify=self.ssl_verify,
http_session=self.http_session,
).get_version()
return version
)
base_url = "{}/api".format(url if url[-1] != "/" else url[:-1])
self.token = token
self.private_key = private_key
self.private_key_file = private_key_file
self.base_url = base_url
self.ssl_verify = ssl_verify
self.session_key = None
self.http_session = requests.Session()
if self.private_key_file:
with open(self.private_key_file, "r") as kf:
private_key = kf.read()
self.private_key = private_key
req = Request(
base=base_url,
token=token,
private_key=private_key,
ssl_verify=ssl_verify,
http_session=self.http_session
)
if self.token and self.private_key:
self.session_key = req.get_session_key()
self.dcim = App(self, "dcim")
self.ipam = App(self, "ipam")
self.circuits = App(self, "circuits")
self.secrets = App(self, "secrets")
self.tenancy = App(self, "tenancy")
self.extras = App(self, "extras")
self.virtualization = App(self, "virtualization")
def full_details(self):
"""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
"""
if self.url:
req = Request(
base=self.url,
token=self.api.token,
session_key=self.api.session_key,
ssl_verify=self.api.ssl_verify,
http_session=self.api.http_session,
)
self._parse_values(req.get())
self.has_details = True
return True
return False
: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
>>>
"""
if self.id:
diff = self._diff()
if diff:
serialized = self.serialize()
req = Request(
key=self.id,
base=self.endpoint.url,
token=self.api.token,
session_key=self.api.session_key,
ssl_verify=self.api.ssl_verify,
http_session=self.api.http_session,
)
if req.patch({i: serialized[i] for i in diff}):
return True
return False