Skip to content

Commit

Permalink
Merge pull request #75 from digitalocean/fix-prefix-idempotence
Browse files Browse the repository at this point in the history
Fix prefix idempotence
  • Loading branch information
Zach Moody authored Jul 18, 2018
2 parents e539950 + dd19f8d commit ceb8e48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pynetbox/lib/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@


def 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. If lookup is an IPNetwork object immediately
return the string representation. 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.
:arg list,optional return_fields: A list of fields to reference when
calling values on lookup.
'''

if isinstance(lookup, netaddr.IPNetwork):
return str(lookup)

for i in return_fields or ['id', 'value', 'nested_return']:
if isinstance(lookup, dict) and lookup.get(i):
return lookup[i]
else:
if hasattr(lookup, i):
return getattr(lookup, i)

if isinstance(lookup, Record):
return str(lookup)
else:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ def test_modify(self, mock):
self.assertEqual(ret_serialized['prefix'], '10.1.2.0/24')
self.assertTrue(netaddr.IPNetwork(ret_serialized['prefix']))

@patch(
'pynetbox.lib.query.requests.put',
return_value=Response(fixture='ipam/prefix.json')
)
@patch(
'pynetbox.lib.query.requests.get',
return_value=Response(fixture='ipam/prefix.json')
)
def test_idempotence(self, *_):
ret = nb.prefixes.get(1)
test = ret.save()
self.assertFalse(test)

@patch(
'pynetbox.lib.query.requests.get',
side_effect=[
Expand Down

0 comments on commit ceb8e48

Please sign in to comment.