Skip to content

Commit

Permalink
Merge pull request #19 from dylanfmarquis/feature/subnet_from_ip
Browse files Browse the repository at this point in the history
Feature/subnet from ip
  • Loading branch information
DrewMonroe committed Feb 8, 2018
2 parents 28a22cd + 723c6e4 commit d2d5271
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Subnet
```python
#Query next available IP for a given subnet.
ip = iblox.subnet('10.1.1.0/24').next_available_ip()

# Get the subnet for a particular IP address
subnet = iblox.subnet_from_ip('10.1.1.0/24')
```
Lease
----
Expand Down
21 changes: 19 additions & 2 deletions infoblox/_internal/subnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@ class constructor - Automatically called on class instantiation
self.subnet = self.prompt()[0]
else:
self.subnet = subnet
self._ref_ = self._ref()

data = self.get()
if type(data) is int:
self.infoblox_.__caller__(
"Infoblox error code {0}".format(str(data)))
raise Exception("Infoblox error code {0}".format(str(data)))
self._ref_ = data['_ref']
self.comment = data['comment']

def __str__(self):
return "{0} - {1}".format(self.subnet, self.comment)

def __repr__(self):
return self.__str__()

def _ref(self):
"""
_ref - Get _ref for a specified subnet
input void (void)
output subnet_ref (string) _ref ID for a subnet
"""
d = self.get()
return d if type(d) is int else d['_ref']

def get(self):
resp = self.infoblox_.get('network?network={0}'.format(self.subnet))
if resp.status_code != 200:
try:
Expand All @@ -39,7 +56,7 @@ def _ref(self):
.format(self.subnet, resp.status_code), resp.status_code)
except Exception:
return resp.status_code
return json.loads(resp.text)[0]['_ref']
return resp.json()[0]

def next_available_ip(self, offset=2):
"""
Expand Down
25 changes: 25 additions & 0 deletions infoblox/infoblox.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
__author__ = "Dylan F. Marquis"
__author__ = "Drew Monroe"


# Infoblox Network Management
class infoblox(object):

Expand Down Expand Up @@ -281,3 +282,27 @@ def rpz_cname(self, name):
output handle (handle) Reference to record:rpz:cname object
"""
return _internal._rpz_cname(self, name)

def subnet_from_ip(self, ip):
"""
Takes an IP address as a string and returns the subnet the IP belongs
to
input ip (string) IP address to get the subnet of
output subnet (Subnet) Returns a subnet object
"""
resp = self.get("network?contains_address={0}".format(ip))

if resp.status_code != 200:
try:
return self.__caller__(
'Could not retrieve subnet _ref for {0} - Status {1}'
.format(ip, resp.status_code), resp.status_code)
except Exception:
return resp.status_code
try:
s = resp.json()[0]['network']
except (ValueError, IndexError):
return None

return self.subnet(subnet=s)
2 changes: 2 additions & 0 deletions infoblox/test/test_infoblox.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def test_subnet_query(self):
.next_available_ip(offset=1))
next_ip = (self.iblox.subnet(config.TEST_SUBNET)
.next_available_ip(offset=1) is None)
self.assertTrue(self.iblox.subnet_from_ip(config.TEST_IP).subnet ==
config.TEST_SUBNET)
self.assertTrue(matches or next_ip)

def test_lease_query(self):
Expand Down

0 comments on commit d2d5271

Please sign in to comment.