@@ -47,6 +47,7 @@ class Infoblox(object):
47
47
get_host
48
48
get_host_by_ip
49
49
get_ip_by_host
50
+ get_host_by_regexp
50
51
get_host_by_extattrs
51
52
get_host_extattrs
52
53
get_network
@@ -405,6 +406,33 @@ def get_host(self, fqdn, fields=None):
405
406
except Exception :
406
407
raise
407
408
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
+
408
436
def get_host_by_ip (self , ip_v4 ):
409
437
""" Implements IBA REST API call to find hostname by IP address
410
438
Returns array of host names in FQDN associated with given IP address
@@ -468,21 +496,22 @@ def get_host_extattrs(self, fqdn, attributes=None):
468
496
:param fqdn: hostname in FQDN
469
497
:param attributes: array of extensible attribute names (optional)
470
498
"""
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 '
472
500
try :
473
501
r = requests .get (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl )
474
502
r_json = r .json ()
475
503
if r .status_code == 200 :
476
504
if len (r_json ) > 0 :
505
+ extattrs = {}
477
506
if attributes :
478
- extattrs = {}
479
507
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' ]
482
510
else :
483
511
raise InfobloxNotFoundException ("No requested attribute found: " + attribute )
484
512
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' ]
486
515
return extattrs
487
516
else :
488
517
raise InfobloxNotFoundException ("No requested host found: " + fqdn )
@@ -625,21 +654,22 @@ def get_network_extattrs(self, network, attributes=None):
625
654
:param network: network in CIDR format
626
655
:param attributes: array of extensible attribute names (optional)
627
656
"""
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 '
629
658
try :
630
659
r = requests .get (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl )
631
660
r_json = r .json ()
632
661
if r .status_code == 200 :
633
662
if len (r_json ) > 0 :
663
+ extattrs = {}
634
664
if attributes :
635
- extattrs = {}
636
665
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' ]
639
668
else :
640
669
raise InfobloxNotFoundException ("No requested attribute found: " + attribute )
641
670
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' ]
643
673
return extattrs
644
674
else :
645
675
raise InfobloxNotFoundException ("No requested network found: " + network )
@@ -658,7 +688,7 @@ def update_network_extattrs(self, network, attributes):
658
688
:param network: network in CIDR format
659
689
:param attributes: hash table of extensible attributes with attribute name as a hash key
660
690
"""
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 '
662
692
extattrs = {}
663
693
try :
664
694
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):
667
697
if len (r_json ) > 0 :
668
698
network_ref = r_json [0 ]['_ref' ]
669
699
if network_ref :
670
- extattrs = r_json [0 ]['extensible_attributes ' ]
700
+ extattrs = r_json [0 ]['extattrs ' ]
671
701
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 ) + '}'
674
704
rest_url = 'https://' + self .iba_host + '/wapi/v' + self .iba_wapi_version + '/' + network_ref
675
705
r = requests .put (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl , data = payload )
676
706
if r .status_code == 200 :
@@ -699,19 +729,19 @@ def delete_network_extattrs(self, network, attributes):
699
729
:param network: network in CIDR format
700
730
:param attributes: array of extensible attribute names
701
731
"""
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 '
703
733
try :
704
734
r = requests .get (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl )
705
735
r_json = r .json ()
706
736
if r .status_code == 200 :
707
737
if len (r_json ) > 0 :
708
738
network_ref = r_json [0 ]['_ref' ]
709
739
if network_ref :
710
- extattrs = r_json [0 ]['extensible_attributes ' ]
740
+ extattrs = r_json [0 ]['extattrs ' ]
711
741
for attribute in attributes :
712
742
if attribute in extattrs :
713
743
del extattrs [attribute ]
714
- payload = '{"extensible_attributes ": ' + json .JSONEncoder ().encode (extattrs ) + '}'
744
+ payload = '{"extattrs ": ' + json .JSONEncoder ().encode (extattrs ) + '}'
715
745
rest_url = 'https://' + self .iba_host + '/wapi/v' + self .iba_wapi_version + '/' + network_ref
716
746
r = requests .put (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl , data = payload )
717
747
if r .status_code == 200 :
0 commit comments