~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2014-07-29 07:06:21 UTC
  • mfrom: (35.2.12 trunk)
  • Revision ID: liam.young@canonical.com-20140729070621-h535klsdxdfrvmxv
[jamespage,r=gnuoy] Add support for multiple network configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from charmhelpers.core.hookenv import (
 
2
    config,
 
3
    unit_get,
 
4
)
 
5
 
 
6
from charmhelpers.contrib.network.ip import (
 
7
    get_address_in_network,
 
8
    is_address_in_network,
 
9
    is_ipv6,
 
10
)
 
11
 
 
12
from charmhelpers.contrib.hahelpers.cluster import is_clustered
 
13
 
 
14
PUBLIC = 'public'
 
15
INTERNAL = 'int'
 
16
ADMIN = 'admin'
 
17
 
 
18
_address_map = {
 
19
    PUBLIC: {
 
20
        'config': 'os-public-network',
 
21
        'fallback': 'public-address'
 
22
    },
 
23
    INTERNAL: {
 
24
        'config': 'os-internal-network',
 
25
        'fallback': 'private-address'
 
26
    },
 
27
    ADMIN: {
 
28
        'config': 'os-admin-network',
 
29
        'fallback': 'private-address'
 
30
    }
 
31
}
 
32
 
 
33
 
 
34
def canonical_url(configs, endpoint_type=PUBLIC):
 
35
    '''
 
36
    Returns the correct HTTP URL to this host given the state of HTTPS
 
37
    configuration, hacluster and charm configuration.
 
38
 
 
39
    :configs OSTemplateRenderer: A config tempating object to inspect for
 
40
        a complete https context.
 
41
    :endpoint_type str: The endpoint type to resolve.
 
42
 
 
43
    :returns str: Base URL for services on the current service unit.
 
44
    '''
 
45
    scheme = 'http'
 
46
    if 'https' in configs.complete_contexts():
 
47
        scheme = 'https'
 
48
    address = resolve_address(endpoint_type)
 
49
    if is_ipv6(address):
 
50
        address = "[{}]".format(address)
 
51
    return '%s://%s' % (scheme, address)
 
52
 
 
53
 
 
54
def resolve_address(endpoint_type=PUBLIC):
 
55
    resolved_address = None
 
56
    if is_clustered():
 
57
        if config(_address_map[endpoint_type]['config']) is None:
 
58
            # Assume vip is simple and pass back directly
 
59
            resolved_address = config('vip')
 
60
        else:
 
61
            for vip in config('vip').split():
 
62
                if is_address_in_network(
 
63
                        config(_address_map[endpoint_type]['config']),
 
64
                        vip):
 
65
                    resolved_address = vip
 
66
    else:
 
67
        resolved_address = get_address_in_network(
 
68
            config(_address_map[endpoint_type]['config']),
 
69
            unit_get(_address_map[endpoint_type]['fallback'])
 
70
        )
 
71
    if resolved_address is None:
 
72
        raise ValueError('Unable to resolve a suitable IP address'
 
73
                         ' based on charm state and configuration')
 
74
    else:
 
75
        return resolved_address