19
19
# Common python helper functions used for OpenStack charms.
20
20
from collections import OrderedDict
21
from functools import wraps
21
from functools import wraps, partial
31
from charmhelpers.contrib.network import ip
32
33
from charmhelpers.core.hookenv import (
422
423
zap_disk(block_device)
427
Returns True if address is a valid IP address.
430
# Test to see if already an IPv4 address
431
socket.inet_aton(address)
437
def ns_query(address):
441
apt_install('python-dnspython')
444
if isinstance(address, dns.name.Name):
446
elif isinstance(address, six.string_types):
451
answers = dns.resolver.query(address, rtype)
453
return str(answers[0])
457
def get_host_ip(hostname):
459
Resolves the IP for a given hostname, or returns
460
the input if it is already an IP.
465
return ns_query(hostname)
468
def get_hostname(address, fqdn=True):
470
Resolves hostname for given IP, or returns the input
471
if it is already a hostname.
475
import dns.reversename
477
apt_install('python-dnspython')
478
import dns.reversename
480
rev = dns.reversename.from_address(address)
481
result = ns_query(rev)
489
if result.endswith('.'):
494
return result.split('.')[0]
425
is_ip = partial(ip.is_ip)
426
ns_query = partial(ip.ns_query)
427
get_host_ip = partial(ip.get_host_ip)
428
get_hostname = partial(ip.get_hostname)
497
431
def get_matchmaker_map(mm_file='/etc/oslo/matchmaker_ring.json'):