Skip to content

Commit 482606a

Browse files
author
igor-feoktistov
committed
Fixes and enhancements
1. Fixes to align with Extensible Attributes object changes in WAPI 1.6 2. New method get_host_by_regexp
1 parent 67cd0c0 commit 482606a

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Class **Infoblox** implements the following methods:
2323
- get_host_by_ip
2424
- get_ip_by_host
2525
- get_host_by_extattrs
26+
- get_host_by_regexp
2627
- get_host_extattrs
2728
- get_network
2829
- get_network_by_ip

infoblox.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Infoblox(object):
4747
get_host
4848
get_host_by_ip
4949
get_ip_by_host
50+
get_host_by_regexp
5051
get_host_by_extattrs
5152
get_host_extattrs
5253
get_network
@@ -405,6 +406,33 @@ def get_host(self, fqdn, fields=None):
405406
except Exception:
406407
raise
407408

409+
def get_host_by_regexp(self, fqdn):
410+
""" Implements IBA REST API call to retrieve host records by fqdn regexp filter
411+
Returns array of host names in FQDN matched to given regexp filter
412+
:param fqdn: hostname in FQDN or FQDN regexp filter
413+
"""
414+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:host?name~=' + fqdn + '&view=' + self.iba_dns_view
415+
hosts = []
416+
try:
417+
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
418+
r_json = r.json()
419+
if r.status_code == 200:
420+
if len(r_json) > 0:
421+
for host in r_json:
422+
hosts.append(host['name'])
423+
return hosts
424+
else:
425+
raise InfobloxNotFoundException("No hosts found for regexp filter: " + fqdn)
426+
else:
427+
if 'text' in r_json:
428+
raise InfobloxGeneralException(r_json['text'])
429+
else:
430+
r.raise_for_status()
431+
except ValueError:
432+
raise Exception(r)
433+
except Exception:
434+
raise
435+
408436
def get_host_by_ip(self, ip_v4):
409437
""" Implements IBA REST API call to find hostname by IP address
410438
Returns array of host names in FQDN associated with given IP address
@@ -468,21 +496,22 @@ def get_host_extattrs(self, fqdn, attributes=None):
468496
:param fqdn: hostname in FQDN
469497
:param attributes: array of extensible attribute names (optional)
470498
"""
471-
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:host?name=' + fqdn + '&view=' + self.iba_dns_view + '&_return_fields=name,extensible_attributes'
499+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:host?name=' + fqdn + '&view=' + self.iba_dns_view + '&_return_fields=name,extattrs'
472500
try:
473501
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
474502
r_json = r.json()
475503
if r.status_code == 200:
476504
if len(r_json) > 0:
505+
extattrs = {}
477506
if attributes:
478-
extattrs = {}
479507
for attribute in attributes:
480-
if attribute in r_json[0]['extensible_attributes']:
481-
extattrs[attribute] = r_json[0]['extensible_attributes'][attribute]
508+
if attribute in r_json[0]['extattrs']:
509+
extattrs[attribute] = r_json[0]['extattrs'][attribute]['value']
482510
else:
483511
raise InfobloxNotFoundException("No requested attribute found: " + attribute)
484512
else:
485-
extattrs = r_json[0]['extensible_attributes']
513+
for attribute in r_json[0]['extattrs'].keys():
514+
extattrs[attribute] = r_json[0]['extattrs'][attribute]['value']
486515
return extattrs
487516
else:
488517
raise InfobloxNotFoundException("No requested host found: " + fqdn)
@@ -625,21 +654,22 @@ def get_network_extattrs(self, network, attributes=None):
625654
:param network: network in CIDR format
626655
:param attributes: array of extensible attribute names (optional)
627656
"""
628-
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extensible_attributes'
657+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extattrs'
629658
try:
630659
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
631660
r_json = r.json()
632661
if r.status_code == 200:
633662
if len(r_json) > 0:
663+
extattrs = {}
634664
if attributes:
635-
extattrs = {}
636665
for attribute in attributes:
637-
if attribute in r_json[0]['extensible_attributes']:
638-
extattrs[attribute] = r_json[0]['extensible_attributes'][attribute]
666+
if attribute in r_json[0]['extattrs']:
667+
extattrs[attribute] = r_json[0]['extattrs'][attribute]['value']
639668
else:
640669
raise InfobloxNotFoundException("No requested attribute found: " + attribute)
641670
else:
642-
extattrs = r_json[0]['extensible_attributes']
671+
for attribute in r_json[0]['extattrs'].keys():
672+
extattrs[attribute] = r_json[0]['extattrs'][attribute]['value']
643673
return extattrs
644674
else:
645675
raise InfobloxNotFoundException("No requested network found: " + network)
@@ -658,7 +688,7 @@ def update_network_extattrs(self, network, attributes):
658688
:param network: network in CIDR format
659689
:param attributes: hash table of extensible attributes with attribute name as a hash key
660690
"""
661-
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extensible_attributes'
691+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extattrs'
662692
extattrs = {}
663693
try:
664694
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
@@ -667,10 +697,10 @@ def update_network_extattrs(self, network, attributes):
667697
if len(r_json) > 0:
668698
network_ref = r_json[0]['_ref']
669699
if network_ref:
670-
extattrs = r_json[0]['extensible_attributes']
700+
extattrs = r_json[0]['extattrs']
671701
for attr_name, attr_value in attributes.iteritems():
672-
extattrs[attr_name] = attr_value
673-
payload = '{"extensible_attributes": ' + json.JSONEncoder().encode(extattrs) + '}'
702+
extattrs[attr_name]['value'] = attr_value
703+
payload = '{"extattrs": ' + json.JSONEncoder().encode(extattrs) + '}'
674704
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/' + network_ref
675705
r = requests.put(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
676706
if r.status_code == 200:
@@ -699,19 +729,19 @@ def delete_network_extattrs(self, network, attributes):
699729
:param network: network in CIDR format
700730
:param attributes: array of extensible attribute names
701731
"""
702-
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extensible_attributes'
732+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view + '&_return_fields=network,extattrs'
703733
try:
704734
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
705735
r_json = r.json()
706736
if r.status_code == 200:
707737
if len(r_json) > 0:
708738
network_ref = r_json[0]['_ref']
709739
if network_ref:
710-
extattrs = r_json[0]['extensible_attributes']
740+
extattrs = r_json[0]['extattrs']
711741
for attribute in attributes:
712742
if attribute in extattrs:
713743
del extattrs[attribute]
714-
payload = '{"extensible_attributes": ' + json.JSONEncoder().encode(extattrs) + '}'
744+
payload = '{"extattrs": ' + json.JSONEncoder().encode(extattrs) + '}'
715745
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/' + network_ref
716746
r = requests.put(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
717747
if r.status_code == 200:

0 commit comments

Comments
 (0)