Skip to content

Commit 0d190e8

Browse files
Merge pull request #7 from danasmera/master
support to update or repoint a cname record
2 parents c39f643 + 97a738c commit 0d190e8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

infoblox.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Infoblox(object):
4343
delete_host_alias
4444
create_cname_record
4545
delete_cname_record
46+
update_cname_record
4647
create_dhcp_range
4748
delete_dhcp_range
4849
get_next_available_ip
@@ -381,6 +382,39 @@ def delete_cname_record(self, fqdn):
381382
except Exception:
382383
raise
383384

385+
def update_cname_record(self, canonical, name):
386+
""" Implements IBA REST API call to update or repoint IBA cname record
387+
:param canonical: canonical name in FQDN format
388+
:param name: the name for the new CNAME record in FQDN format
389+
"""
390+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:cname'
391+
payload = json.dumps({'name': name})
392+
try:
393+
r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
394+
r_json = r.json()
395+
# RFC1912 - A CNAME can not coexist with any other data, we should expect utmost one entry
396+
if r.status_code == 200 and len(r_json) == 1:
397+
ibx_cname = r.json()[0]
398+
cname_ref = ibx_cname['_ref']
399+
payload = '{"canonical": ' + json.JSONEncoder().encode(canonical) + '}'
400+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/' + cname_ref
401+
r = requests.put(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
402+
if r.status_code == 200 or r.status_code == 201:
403+
return
404+
else:
405+
r.raise_for_status()
406+
elif len(r_json) == 0:
407+
raise InfobloxNotFoundException("CNAME: " + name + " not found.")
408+
else:
409+
if 'text' in r_json:
410+
raise InfobloxGeneralException(r_json['text'])
411+
else:
412+
r.raise_for_status()
413+
except ValueError:
414+
raise Exception(r)
415+
except Exception:
416+
raise
417+
384418
def create_dhcp_range(self, start_ip_v4, end_ip_v4):
385419
""" Implements IBA REST API call to add DHCP range for given start and end addresses
386420
:param start_ip_v4: IP v4 address

0 commit comments

Comments
 (0)