~openstack-charmers-archive/charms/trusty/keystone/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/network/ip.py

  • Committer: james.page at ubuntu
  • Date: 2014-07-02 08:19:37 UTC
  • mto: This revision was merged to the branch mainline in revision 72.
  • Revision ID: james.page@ubuntu.com-20140702081937-3uloept2tbj8o5me
Resync helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
        not_found_error_out()
68
68
 
69
69
    return None
 
70
 
 
71
 
 
72
def is_address_in_network(network, address):
 
73
    """
 
74
    Determine whether the provided address is within a network range.
 
75
 
 
76
    :param network (str): CIDR presentation format. For example,
 
77
        '192.168.1.0/24'.
 
78
    :param address: An individual IPv4 or IPv6 address without a net
 
79
        mask or subnet prefix. For example, '192.168.1.1'.
 
80
    :returns boolean: Flag indicating whether address is in network.
 
81
    """
 
82
    try:
 
83
        network = netaddr.IPNetwork(network)
 
84
    except (netaddr.core.AddrFormatError, ValueError):
 
85
        raise ValueError("Network (%s) is not in CIDR presentation format" %
 
86
                         network)
 
87
    try:
 
88
        address = netaddr.IPAddress(address)
 
89
    except (netaddr.core.AddrFormatError, ValueError):
 
90
        raise ValueError("Address (%s) is not in correct presentation format" %
 
91
                         address)
 
92
    if address in network:
 
93
        return True
 
94
    else:
 
95
        return False