Skip to content

Commit 4fa86a8

Browse files
author
Zach Moody
authored
Merge pull request #201 from markkuleinio/custom-choices
Add method to return custom choices
2 parents bc70a8a + 4e4dd35 commit 4fa86a8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pynetbox/api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ def choices(self):
7878

7979
return self._choices
8080

81+
def custom_choices(self):
82+
""" Returns _custom_field_choices response from app
83+
84+
:Returns: Raw response from NetBox's _custom_field_choices endpoint.
85+
:Raises: :py:class:`.RequestError` if called for an invalid endpoint.
86+
:Example:
87+
88+
>>> nb.extras.custom_choices()
89+
{'Testfield1': {'Testvalue2': 2, 'Testvalue1': 1},
90+
'Testfield2': {'Othervalue2': 4, 'Othervalue1': 3}}
91+
"""
92+
custom_field_choices = Request(
93+
base="{}/{}/_custom_field_choices/".format(
94+
self.api.base_url,
95+
self.name,
96+
),
97+
token=self.api.token,
98+
private_key=self.api.private_key,
99+
ssl_verify=self.api.ssl_verify,
100+
http_session=self.api.http_session,
101+
).get()
102+
return custom_field_choices
103+
81104

82105
class Api(object):
83106
""" The API object is the point of entry to pynetbox.

tests/test_app.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
import six
4+
5+
import pynetbox
6+
7+
if six.PY3:
8+
from unittest.mock import patch
9+
else:
10+
from mock import patch
11+
12+
host = "http://localhost:8000"
13+
14+
def_kwargs = {
15+
'token': 'abc123',
16+
}
17+
18+
19+
class AppCustomChoicesTestCase(unittest.TestCase):
20+
21+
@patch(
22+
'pynetbox.core.query.Request.get',
23+
return_value={
24+
"Testfield1": {"TF1_1": 1, "TF1_2": 2},
25+
"Testfield2": {"TF2_1": 3, "TF2_2": 4},
26+
}
27+
)
28+
def test_custom_choices(self, *_):
29+
api = pynetbox.api(
30+
host,
31+
**def_kwargs
32+
)
33+
choices = api.extras.custom_choices()
34+
self.assertEqual(len(choices), 2)
35+
self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])

0 commit comments

Comments
 (0)