Skip to content

Commit bc75987

Browse files
author
Zach Moody
authored
Merge pull request #71 from digitalocean/custom-field-serialization
Custom field serialization
2 parents 838fbe7 + a2cde39 commit bc75987

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

pynetbox/lib/response.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
from pynetbox.lib.query import Request
2020

2121

22-
def _get_return(lookup, return_fields=['id', 'value', 'nested_return']):
22+
def get_return(lookup, return_fields=None):
2323

24-
for i in return_fields:
24+
for i in return_fields or ['id', 'value', 'nested_return']:
2525
if isinstance(lookup, dict) and lookup.get(i):
2626
return lookup[i]
2727
else:
@@ -33,6 +33,13 @@ def _get_return(lookup, return_fields=['id', 'value', 'nested_return']):
3333
return lookup
3434

3535

36+
def flatten_custom(custom_dict):
37+
return {
38+
k: v if not isinstance(v, dict) else v['value']
39+
for k, v in custom_dict.items()
40+
}
41+
42+
3643
class Record(object):
3744
"""Create python objects from netbox API responses.
3845
@@ -114,7 +121,7 @@ def _add_cache(self, item):
114121
self._full_cache.append((key, dict(value)))
115122
else:
116123
self._full_cache.append((key, value))
117-
self._index_cache.append((key, _get_return(value)))
124+
self._index_cache.append((key, get_return(value)))
118125

119126
def _parse_values(self, values):
120127
""" Parses values init arg.
@@ -148,12 +155,11 @@ def _compare(self):
148155
init_vals = dict(self._index_cache)
149156
for i in dict(self):
150157
current_val = init_vals.get(i)
151-
if i != 'custom_fields':
152-
if isinstance(current_val, dict):
153-
init_dict.update({i: _get_return(current_val)})
154-
else:
155-
init_dict.update({i: _get_return(current_val)})
156-
init_dict.update({i: current_val})
158+
if i == 'custom_fields':
159+
init_dict[i] = flatten_custom(current_val)
160+
else:
161+
init_dict[i] = get_return(current_val)
162+
157163
if init_dict == self.serialize():
158164
return True
159165
return False
@@ -193,18 +199,24 @@ def serialize(self, nested=False):
193199
:returns: dict of values the NetBox API is expecting.
194200
"""
195201
if nested:
196-
return _get_return(self)
202+
return get_return(self)
197203

198204
ret = {}
199205
for i in dict(self):
200206
current_val = getattr(self, i)
201-
if isinstance(current_val, Record):
202-
current_val = getattr(current_val, 'serialize')(nested=True)
207+
if i == 'custom_fields':
208+
ret[i] = flatten_custom(current_val)
209+
else:
210+
if isinstance(current_val, Record):
211+
current_val = getattr(
212+
current_val,
213+
'serialize'
214+
)(nested=True)
203215

204-
if isinstance(current_val, netaddr.ip.IPNetwork):
205-
current_val = str(current_val)
216+
if isinstance(current_val, netaddr.ip.IPNetwork):
217+
current_val = str(current_val)
206218

207-
ret.update({i: current_val})
219+
ret[i] = current_val
208220
return ret
209221

210222
def save(self):

tests/fixtures/dcim/site.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"contact_email": "",
1414
"comments": "",
1515
"custom_fields": {
16-
"test_custom": "Hello"
16+
"test_custom": "Hello",
17+
"test_selection": {
18+
"value": 2,
19+
"label": "second"
20+
}
1721
},
1822
"count_prefixes": 2,
1923
"count_vlans": 1,

tests/test_dcim.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class SiteTestCase(unittest.TestCase, GenericTest):
282282
'pynetbox.lib.query.requests.get',
283283
return_value=Response(fixture='dcim/site.json')
284284
)
285-
def test_modify(self, mock):
285+
def test_modify_custom(self, mock):
286286
'''Test modifying a custom field.
287287
'''
288288
ret = getattr(nb, self.name).get(1)
@@ -293,6 +293,23 @@ def test_modify(self, mock):
293293
ret.custom_fields['test_custom'], 'Testing'
294294
)
295295

296+
@patch(
297+
'pynetbox.lib.query.requests.get',
298+
return_value=Response(fixture='dcim/site.json')
299+
)
300+
def test_custom_selection_serializer(self, _):
301+
'''Tests serializer with custom selection fields.
302+
'''
303+
ret = getattr(nb, self.name).get(1)
304+
ret.custom_fields['test_custom'] = "Testing"
305+
test = ret.serialize()
306+
from pprint import pprint
307+
pprint(test)
308+
self.assertEqual(
309+
test['custom_fields']['test_selection'],
310+
2
311+
)
312+
296313
@patch(
297314
'pynetbox.lib.query.requests.post',
298315
return_value=Response(fixture='dcim/site.json')

0 commit comments

Comments
 (0)