From 5caec3f5e5fd57a849b504071aab91aa56352c62 Mon Sep 17 00:00:00 2001 From: Wei Lei Date: Wed, 16 Nov 2016 11:40:41 -0500 Subject: [PATCH 1/4] update lease2fixed in hlinfoblox.py --- CHANGELOG.md | 4 ++ hlinfoblox.py | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 hlinfoblox.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ce0fff..8fc8477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ Infoblox Python API CHANGELOG ============================= +1.4.? +----- + +* [Wei Lei] - Fixed 'lease2fixed' in 'hlinfobolx.py' 1.4.0 ----- diff --git a/hlinfoblox.py b/hlinfoblox.py new file mode 100644 index 0000000..0464d83 --- /dev/null +++ b/hlinfoblox.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- +# +# Copyright stuff +# +# License stuff +# + +from .infoblox import Infoblox, InfobloxGeneralException, Util + +# import more stuff + + +class HighLevelInfobloxActions(object): + + """ Implements the following high level infoblox actions + convert_lease_to_fixed_address + """ + + def __init__(self, + iba_ipaddr, + iba_user, + iba_password, + iba_wapi_version, + iba_dns_view, + iba_network_view, + iba_verify_ssl=False): + """ Class initialization method + :param iba_ipaddr: IBA IP address of management interface + :param iba_user: IBA user name + :param iba_password: IBA user password + :param iba_wapi_version: IBA WAPI version (example: 1.0) + :param iba_dns_view: IBA default view + :param iba_network_view: IBA default network view + :param iba_verify_ssl: IBA SSL certificate validation (example: False) + """ + self.iba_host = iba_ipaddr + self.iba_user = iba_user + self.iba_password = iba_password + self.iba_wapi_version = iba_wapi_version + self.iba_dns_view = iba_dns_view + self.iba_network_view = iba_network_view + self.iba_verify_ssl = iba_verify_ssl + + self.api = Infoblox(iba_ipaddr, iba_user, iba_password, + iba_wapi_version, iba_dns_view, iba_network_view, + iba_verify_ssl) + + def convert_lease_to_fixed_address(self, address, fqdn=None, + confirm=False): + """Convert a DHCP-assigned leased address to a fixed address. + :param address: IP Address to be converted + :param fqdn: Fully qualified domain name for host if we cannot + determine it from the lease information. + :param confirm: Confirm that you really want to do this. + """ + + # Get our host information + ipv4address_record = self.api.get_ipv4address_by_ip(address, fields=[ + 'dhcp_client_identifier', + 'mac_address', + 'names', + 'objects']) + print("IPV4 Address Record [%s]" % (ipv4address_record)) + + # Try to find a record:host for the host we found. + names = ipv4address_record['names'] + if names is None and fqdn is None: + raise(InfobloxGeneralException( + "Cannot determine fqdn for [%s]" % (address))) + + mac = ipv4address_record['mac_address'] + print("mac [%s]" % (mac)) + if mac is None or mac is '': + raise(InfobloxGeneralException( + "Cannot determine mac for [%s]" % (address))) + + if fqdn is None: + fqdn = self._guess_fqdn(address, ipv4address_record) + print("have to find fqdn [%s]" % fqdn) + if not names: + self._create_host_record(address, fqdn, mac) + print("have to find names [%s]" % names) + + print("this-is-fqdn [%s]" % fqdn) + print("this-is-names [%s]" % names) + # If we found names associated with the address then + # we can use those names to find record:host objects + # and convert them to fixed. + for name in names: + print("Host Name [%s]" % (name)) + host_record = self.api.get_host(name, notFoundFail=False) + a_record = self.api.get_a_record_by_ip(address) + print("Host Record [%s]" % (host_record)) + print("A record [%s]" % (a_record)) + if host_record is None and a_record is None: + print(" Cannot find record:host or a:record for [%s], exit" % (name)) + # self._create_host_record(address, fqdn, mac) + else: + print(" Converting lease to fixedaddress") + payload = { + 'name': fqdn, + 'ipv4addrs' : [{'ipv4addr': address, + 'configure_for_dhcp': True, + 'mac': mac}] + } + uri = 'record:host' + fields='' + status_code = self.api.util.post(uri, payload, fields) + break + + def _create_host_record(self, address, fqdn, mac): + if fqdn is None: + raise(InfobloxGeneralException( + "Cannot determine fqdn for new HOST Record for [%s]" % + (address))) + payload = {'name': fqdn, + 'view': self.iba_dns_view, + 'ipv4addrs': [{'ipv4addr': address, + 'configure_for_dhcp': True, + 'match_client': 'MAC_ADDRESS', + 'mac': mac + }]} + print("host_data [%s]" % (payload)) + self.api.create_host_record(address, fqdn, + payload=payload) + + def _guess_fqdn(self, address, ipv4address_record): + """Try to figure out an address' fqdn. + :param address: IP v4 address or NET v4 address in CIDR format + :return: None on failure else fqdn. + :rtype string: + """ + + if 'dhcp_client_identifier' in ipv4address_record: + print("printing dhcp_client_identifier [%s]" % dhcp_client_identifier) + return ipv4address_record['dhcp_client_identifier'] + + for ref in ipv4address_record['objects']: + if ref.startswith('lease/'): + lease_ref = ref + print("Lease Reference [%s]" % (lease_ref)) + lease_record = self.api.util.get(lease_ref, + fields=['client_hostname']) + print("Lease Record [%s]" % (lease_record)) + + if 'client_hostname' in lease_record: + print("client_hostname [%s]" % lease_record) + return lease_record['client_hostname'] + + return None From 5bd56fb3c9741863a1f9fd404bbffc089034905d Mon Sep 17 00:00:00 2001 From: Wei Lei Date: Wed, 16 Nov 2016 11:44:11 -0500 Subject: [PATCH 2/4] update lease2fixed in hlinfoblox.py --- hlinfoblox.py | 150 ----------------------------------------- infoblox/hlinfoblox.py | 40 ++++++----- 2 files changed, 24 insertions(+), 166 deletions(-) delete mode 100644 hlinfoblox.py diff --git a/hlinfoblox.py b/hlinfoblox.py deleted file mode 100644 index 0464d83..0000000 --- a/hlinfoblox.py +++ /dev/null @@ -1,150 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright stuff -# -# License stuff -# - -from .infoblox import Infoblox, InfobloxGeneralException, Util - -# import more stuff - - -class HighLevelInfobloxActions(object): - - """ Implements the following high level infoblox actions - convert_lease_to_fixed_address - """ - - def __init__(self, - iba_ipaddr, - iba_user, - iba_password, - iba_wapi_version, - iba_dns_view, - iba_network_view, - iba_verify_ssl=False): - """ Class initialization method - :param iba_ipaddr: IBA IP address of management interface - :param iba_user: IBA user name - :param iba_password: IBA user password - :param iba_wapi_version: IBA WAPI version (example: 1.0) - :param iba_dns_view: IBA default view - :param iba_network_view: IBA default network view - :param iba_verify_ssl: IBA SSL certificate validation (example: False) - """ - self.iba_host = iba_ipaddr - self.iba_user = iba_user - self.iba_password = iba_password - self.iba_wapi_version = iba_wapi_version - self.iba_dns_view = iba_dns_view - self.iba_network_view = iba_network_view - self.iba_verify_ssl = iba_verify_ssl - - self.api = Infoblox(iba_ipaddr, iba_user, iba_password, - iba_wapi_version, iba_dns_view, iba_network_view, - iba_verify_ssl) - - def convert_lease_to_fixed_address(self, address, fqdn=None, - confirm=False): - """Convert a DHCP-assigned leased address to a fixed address. - :param address: IP Address to be converted - :param fqdn: Fully qualified domain name for host if we cannot - determine it from the lease information. - :param confirm: Confirm that you really want to do this. - """ - - # Get our host information - ipv4address_record = self.api.get_ipv4address_by_ip(address, fields=[ - 'dhcp_client_identifier', - 'mac_address', - 'names', - 'objects']) - print("IPV4 Address Record [%s]" % (ipv4address_record)) - - # Try to find a record:host for the host we found. - names = ipv4address_record['names'] - if names is None and fqdn is None: - raise(InfobloxGeneralException( - "Cannot determine fqdn for [%s]" % (address))) - - mac = ipv4address_record['mac_address'] - print("mac [%s]" % (mac)) - if mac is None or mac is '': - raise(InfobloxGeneralException( - "Cannot determine mac for [%s]" % (address))) - - if fqdn is None: - fqdn = self._guess_fqdn(address, ipv4address_record) - print("have to find fqdn [%s]" % fqdn) - if not names: - self._create_host_record(address, fqdn, mac) - print("have to find names [%s]" % names) - - print("this-is-fqdn [%s]" % fqdn) - print("this-is-names [%s]" % names) - # If we found names associated with the address then - # we can use those names to find record:host objects - # and convert them to fixed. - for name in names: - print("Host Name [%s]" % (name)) - host_record = self.api.get_host(name, notFoundFail=False) - a_record = self.api.get_a_record_by_ip(address) - print("Host Record [%s]" % (host_record)) - print("A record [%s]" % (a_record)) - if host_record is None and a_record is None: - print(" Cannot find record:host or a:record for [%s], exit" % (name)) - # self._create_host_record(address, fqdn, mac) - else: - print(" Converting lease to fixedaddress") - payload = { - 'name': fqdn, - 'ipv4addrs' : [{'ipv4addr': address, - 'configure_for_dhcp': True, - 'mac': mac}] - } - uri = 'record:host' - fields='' - status_code = self.api.util.post(uri, payload, fields) - break - - def _create_host_record(self, address, fqdn, mac): - if fqdn is None: - raise(InfobloxGeneralException( - "Cannot determine fqdn for new HOST Record for [%s]" % - (address))) - payload = {'name': fqdn, - 'view': self.iba_dns_view, - 'ipv4addrs': [{'ipv4addr': address, - 'configure_for_dhcp': True, - 'match_client': 'MAC_ADDRESS', - 'mac': mac - }]} - print("host_data [%s]" % (payload)) - self.api.create_host_record(address, fqdn, - payload=payload) - - def _guess_fqdn(self, address, ipv4address_record): - """Try to figure out an address' fqdn. - :param address: IP v4 address or NET v4 address in CIDR format - :return: None on failure else fqdn. - :rtype string: - """ - - if 'dhcp_client_identifier' in ipv4address_record: - print("printing dhcp_client_identifier [%s]" % dhcp_client_identifier) - return ipv4address_record['dhcp_client_identifier'] - - for ref in ipv4address_record['objects']: - if ref.startswith('lease/'): - lease_ref = ref - print("Lease Reference [%s]" % (lease_ref)) - lease_record = self.api.util.get(lease_ref, - fields=['client_hostname']) - print("Lease Record [%s]" % (lease_record)) - - if 'client_hostname' in lease_record: - print("client_hostname [%s]" % lease_record) - return lease_record['client_hostname'] - - return None diff --git a/infoblox/hlinfoblox.py b/infoblox/hlinfoblox.py index 3ee58ea..0464d83 100644 --- a/infoblox/hlinfoblox.py +++ b/infoblox/hlinfoblox.py @@ -5,7 +5,7 @@ # License stuff # -from .infoblox import Infoblox, InfobloxGeneralException +from .infoblox import Infoblox, InfobloxGeneralException, Util # import more stuff @@ -76,31 +76,37 @@ def convert_lease_to_fixed_address(self, address, fqdn=None, if fqdn is None: fqdn = self._guess_fqdn(address, ipv4address_record) - + print("have to find fqdn [%s]" % fqdn) if not names: self._create_host_record(address, fqdn, mac) - + print("have to find names [%s]" % names) + + print("this-is-fqdn [%s]" % fqdn) + print("this-is-names [%s]" % names) # If we found names associated with the address then # we can use those names to find record:host objects # and convert them to fixed. for name in names: print("Host Name [%s]" % (name)) host_record = self.api.get_host(name, notFoundFail=False) - if host_record is None: - print(" Cannot find record:host for [%s]" % (name)) - self._create_host_record(address, fqdn, mac) + a_record = self.api.get_a_record_by_ip(address) + print("Host Record [%s]" % (host_record)) + print("A record [%s]" % (a_record)) + if host_record is None and a_record is None: + print(" Cannot find record:host or a:record for [%s], exit" % (name)) + # self._create_host_record(address, fqdn, mac) else: - print(" HOST Record [%s]" % (host_record)) print(" Converting lease to fixedaddress") - for host_host_ipv4addr in host_record['ipv4addrs']: - fields = { - 'configure_for_dhcp': True, - 'match_client': 'MAC_ADDRESS', - 'mac': mac - } - self.api.update_record(host_record, - fields=fields, - confirm=confirm) + payload = { + 'name': fqdn, + 'ipv4addrs' : [{'ipv4addr': address, + 'configure_for_dhcp': True, + 'mac': mac}] + } + uri = 'record:host' + fields='' + status_code = self.api.util.post(uri, payload, fields) + break def _create_host_record(self, address, fqdn, mac): if fqdn is None: @@ -126,6 +132,7 @@ def _guess_fqdn(self, address, ipv4address_record): """ if 'dhcp_client_identifier' in ipv4address_record: + print("printing dhcp_client_identifier [%s]" % dhcp_client_identifier) return ipv4address_record['dhcp_client_identifier'] for ref in ipv4address_record['objects']: @@ -137,6 +144,7 @@ def _guess_fqdn(self, address, ipv4address_record): print("Lease Record [%s]" % (lease_record)) if 'client_hostname' in lease_record: + print("client_hostname [%s]" % lease_record) return lease_record['client_hostname'] return None From 9b489e5578d7fecc1efb89e43480b218b4575477 Mon Sep 17 00:00:00 2001 From: Wei Lei Date: Thu, 17 Nov 2016 10:36:35 -0500 Subject: [PATCH 3/4] more update for infoblox/hlifoblox.py --- infoblox/hlinfoblox.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/infoblox/hlinfoblox.py b/infoblox/hlinfoblox.py index 0464d83..19c6d31 100644 --- a/infoblox/hlinfoblox.py +++ b/infoblox/hlinfoblox.py @@ -79,7 +79,6 @@ def convert_lease_to_fixed_address(self, address, fqdn=None, print("have to find fqdn [%s]" % fqdn) if not names: self._create_host_record(address, fqdn, mac) - print("have to find names [%s]" % names) print("this-is-fqdn [%s]" % fqdn) print("this-is-names [%s]" % names) @@ -132,7 +131,6 @@ def _guess_fqdn(self, address, ipv4address_record): """ if 'dhcp_client_identifier' in ipv4address_record: - print("printing dhcp_client_identifier [%s]" % dhcp_client_identifier) return ipv4address_record['dhcp_client_identifier'] for ref in ipv4address_record['objects']: @@ -144,7 +142,6 @@ def _guess_fqdn(self, address, ipv4address_record): print("Lease Record [%s]" % (lease_record)) if 'client_hostname' in lease_record: - print("client_hostname [%s]" % lease_record) return lease_record['client_hostname'] return None From 2a267494150dfb5a9e455acb2c58ada847d848b2 Mon Sep 17 00:00:00 2001 From: Wei Lei Date: Thu, 17 Nov 2016 10:40:05 -0500 Subject: [PATCH 4/4] more update for infoblox/hlifoblox.py --- infoblox/hlinfoblox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infoblox/hlinfoblox.py b/infoblox/hlinfoblox.py index 19c6d31..114708a 100644 --- a/infoblox/hlinfoblox.py +++ b/infoblox/hlinfoblox.py @@ -76,7 +76,7 @@ def convert_lease_to_fixed_address(self, address, fqdn=None, if fqdn is None: fqdn = self._guess_fqdn(address, ipv4address_record) - print("have to find fqdn [%s]" % fqdn) + if not names: self._create_host_record(address, fqdn, mac)