Skip to content

Commit c39f643

Browse files
Merge pull request #5 from nigel4321/master
support to create/delete/search TXT records
2 parents 482606a + 877644a commit c39f643

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Class **Infoblox** implements the following methods:
1111
- create_networkcontainer
1212
- delete_networkcontainer
1313
- create_host_record
14+
- create_txt_record
1415
- delete_host_record
16+
- delete_txt_record
1517
- add_host_alias
1618
- delete_host_alias
1719
- create_cname_record
@@ -24,6 +26,7 @@ Class **Infoblox** implements the following methods:
2426
- get_ip_by_host
2527
- get_host_by_extattrs
2628
- get_host_by_regexp
29+
- get_txt_by_regexp
2730
- get_host_extattrs
2831
- get_network
2932
- get_network_by_ip

infoblox.py

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Infoblox(object):
3636
create_networkcontainer
3737
delete_networkcontainer
3838
create_host_record
39+
create_txt_record
40+
delete_txt_record
3941
delete_host_record
4042
add_host_alias
4143
delete_host_alias
@@ -48,6 +50,7 @@ class Infoblox(object):
4850
get_host_by_ip
4951
get_ip_by_host
5052
get_host_by_regexp
53+
get_txt_by_regexp
5154
get_host_by_extattrs
5255
get_host_extattrs
5356
get_network
@@ -58,7 +61,7 @@ class Infoblox(object):
5861
delete_network_extattrs
5962
"""
6063

61-
def __init__(self, iba_ipaddr, iba_user, iba_password, iba_wapi_version, iba_dns_view, iba_network_view, iba_verify_ssl=True):
64+
def __init__(self, iba_ipaddr, iba_user, iba_password, iba_wapi_version, iba_dns_view, iba_network_view, iba_verify_ssl=False):
6265
""" Class initialization method
6366
:param iba_ipaddr: IBA IP address of management interface
6467
:param iba_user: IBA user name
@@ -75,7 +78,7 @@ def __init__(self, iba_ipaddr, iba_user, iba_password, iba_wapi_version, iba_dns
7578
self.iba_dns_view = iba_dns_view
7679
self.iba_network_view = iba_network_view
7780
self.iba_verify_ssl = iba_verify_ssl
78-
81+
7982
def get_next_available_ip(self, network):
8083
""" Implements IBA next_available_ip REST API call
8184
Returns IP v4 address
@@ -144,6 +147,29 @@ def create_host_record(self, address, fqdn):
144147
except Exception:
145148
raise
146149

150+
def create_txt_record(self, text, fqdn):
151+
""" Implements IBA REST API call to create IBA txt record
152+
Returns IP v4 address assigned to the host
153+
:param text: free text to be added to the record
154+
:param fqdn: hostname in FQDN
155+
"""
156+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:txt'
157+
payload = '{"text": "' + text + '","name": "' + fqdn + '","view": "' + self.iba_dns_view + '"}'
158+
try:
159+
r = requests.post(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
160+
r_json = r.json()
161+
if r.status_code == 200 or r.status_code == 201:
162+
return
163+
else:
164+
if 'text' in r_json:
165+
raise InfobloxGeneralException(r_json['text'])
166+
else:
167+
r.raise_for_status()
168+
except ValueError:
169+
raise Exception(r)
170+
except Exception:
171+
raise
172+
147173
def delete_host_record(self, fqdn):
148174
""" Implements IBA REST API call to delete IBA host record
149175
:param fqdn: hostname in FQDN
@@ -179,6 +205,41 @@ def delete_host_record(self, fqdn):
179205
except Exception:
180206
raise
181207

208+
def delete_txt_record(self, fqdn):
209+
""" Implements IBA REST API call to delete IBA TXT record
210+
:param fqdn: hostname in FQDN
211+
"""
212+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:txt?name=' + fqdn + '&view=' + self.iba_dns_view
213+
try:
214+
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
215+
r_json = r.json()
216+
if r.status_code == 200:
217+
if len(r_json) > 0:
218+
host_ref = r_json[0]['_ref']
219+
if host_ref and re.match("record:txt\/[^:]+:([^\/]+)\/", host_ref).group(1) == fqdn:
220+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/' + host_ref
221+
r = requests.delete(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
222+
if r.status_code == 200:
223+
return
224+
else:
225+
if 'text' in r_json:
226+
raise InfobloxGeneralException(r_json['text'])
227+
else:
228+
r.raise_for_status()
229+
else:
230+
raise InfobloxGeneralException("Received unexpected host reference: " + host_ref)
231+
else:
232+
raise InfobloxNotFoundException("No requested host found: " + fqdn)
233+
else:
234+
if 'text' in r_json:
235+
raise InfobloxGeneralException(r_json['text'])
236+
else:
237+
r.raise_for_status()
238+
except ValueError:
239+
raise Exception(r)
240+
except Exception:
241+
raise
242+
182243
def add_host_alias(self, host_fqdn, alias_fqdn):
183244
""" Implements IBA REST API call to add an alias to IBA host record
184245
:param host_fqdn: host record name in FQDN
@@ -433,6 +494,33 @@ def get_host_by_regexp(self, fqdn):
433494
except Exception:
434495
raise
435496

497+
def get_txt_by_regexp(self, fqdn):
498+
""" Implements IBA REST API call to retrieve TXT records by fqdn regexp filter
499+
Returns dictonary of host names in FQDN matched to given regexp filter with the TXT value
500+
:param fqdn: hostname in FQDN or FQDN regexp filter
501+
"""
502+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:txt?name~=' + fqdn + '&view=' + self.iba_dns_view
503+
hosts = {}
504+
try:
505+
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
506+
r_json = r.json()
507+
if r.status_code == 200:
508+
if len(r_json) > 0:
509+
for host in r_json:
510+
hosts[host['name']] = host['text']
511+
return hosts
512+
else:
513+
raise InfobloxNotFoundException("No txt records found for regexp filter: " + fqdn)
514+
else:
515+
if 'text' in r_json:
516+
raise InfobloxGeneralException(r_json['text'])
517+
else:
518+
r.raise_for_status()
519+
except ValueError:
520+
raise Exception(r)
521+
except Exception:
522+
raise
523+
436524
def get_host_by_ip(self, ip_v4):
437525
""" Implements IBA REST API call to find hostname by IP address
438526
Returns array of host names in FQDN associated with given IP address

0 commit comments

Comments
 (0)