Skip to content

Commit

Permalink
Merge pull request #314 from markkuleinio/add-status
Browse files Browse the repository at this point in the history
Add Api.status() to be used with NetBox 2.10.0+
  • Loading branch information
Zach Moody committed Dec 29, 2020
2 parents e7d0518 + 7e5f473 commit 50cb312
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pynetbox/core/api.py
Expand Up @@ -150,3 +150,36 @@ def openapi(self):
return Request(
base=self.base_url, http_session=self.http_session,
).get_openapi()

def status(self):
"""Gets the status information from NetBox.
Available in NetBox 2.10.0 or newer.
:Returns: Dictionary as returned by NetBox.
:Raises: :py:class:`.RequestError` if the request is not successful.
:Example:
>>> pprint.pprint(nb.status())
{'django-version': '3.1.3',
'installed-apps': {'cacheops': '5.0.1',
'debug_toolbar': '3.1.1',
'django_filters': '2.4.0',
'django_prometheus': '2.1.0',
'django_rq': '2.4.0',
'django_tables2': '2.3.3',
'drf_yasg': '1.20.0',
'mptt': '0.11.0',
'rest_framework': '3.12.2',
'taggit': '1.3.0',
'timezone_field': '4.0'},
'netbox-version': '2.10.2',
'plugins': {},
'python-version': '3.7.3',
'rq-workers-running': 1}
>>>
"""
status = Request(
base=self.base_url, token=self.token, http_session=self.http_session,
).get_status()
return status
17 changes: 17 additions & 0 deletions pynetbox/core/query.py
Expand Up @@ -217,6 +217,23 @@ def get_session_key(self):
else:
raise RequestError(req)

def get_status(self):
""" Gets the status from /api/status/ endpoint in NetBox.
:Returns: Dictionary as returned by NetBox.
:Raises: RequestError if request is not successful.
"""
headers = {"Content-Type": "application/json;"}
if self.token:
headers["authorization"] = "Token {}".format(self.token)
req = self.http_session.get(
"{}status/".format(self.normalize_url(self.base)), headers=headers,
)
if req.ok:
return req.json()
else:
raise RequestError(req)

def normalize_url(self, url):
""" Builds a url for POST actions.
"""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_api.py
Expand Up @@ -70,3 +70,20 @@ class ResponseHeadersWithoutVersion:
def test_api_version_not_found(self, *_):
api = pynetbox.api(host,)
self.assertEqual(api.version, "")


class ApiStatusTestCase(unittest.TestCase):
class ResponseWithStatus:
ok = True
def json(self):
return {
"netbox-version": "0.9.9",
}

@patch(
"pynetbox.core.query.requests.sessions.Session.get",
return_value=ResponseWithStatus(),
)
def test_api_status(self, *_):
api = pynetbox.api(host,)
self.assertEqual(api.status()["netbox-version"], "0.9.9")

0 comments on commit 50cb312

Please sign in to comment.