Skip to content

Commit

Permalink
Merge pull request #94 from coxley/master
Browse files Browse the repository at this point in the history
Adds Network.closest_parent() -> Network instance
  • Loading branch information
jathanism committed Apr 1, 2016
2 parents 85f0b4c + e852bb9 commit 996ff1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pynsot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@ def init_payload(self):
'attributes': self.attributes,
}

def closest_parent(self):
'''Returns resource object of the closest parent network
Empty dictionary if no parent network
Returns:
dict
'''
self.ensure_client()
site = getattr(self.client.sites(self['site_id']), self.resource_name)
cidr = '%s/%s' % (self['network_address'], self['prefix_length'])
try:
lookup = get_result(site(cidr).closest_parent.get())
return Network(raw=lookup)
except Exception as e:
self.log_error(e)
return {}

def __len__(self):
return self['prefix_length']

Expand Down
2 changes: 1 addition & 1 deletion pynsot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.20.1'
__version__ = '0.20.2'
16 changes: 16 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def test_existing(client, site):
assert n.existing_resource() == n._existing_resource


def test_net_closest_parent(client, site):
'''Test that Network.closest_parent returns instance of Network or dict'''
site_id = site['id']
c = client

parent = Network(client=c, site_id=site_id, cidr='8.8.8.0/24')
assert parent.ensure()

child = Network(client=c, site_id=site_id, cidr='8.8.8.8/32')
assert child.closest_parent() == parent

orphan = Network(client=c, site_id=site_id, cidr='1.1.1.1/32')
assert not orphan.closest_parent()
assert orphan.closest_parent() == {}


def test_dict():
'''Test methods/behavior that should work like a dictionary'''
n = Network(site_id=1, cidr='8.8.8.0/24')
Expand Down

0 comments on commit 996ff1e

Please sign in to comment.