~robert-ayres/charms/trusty/contrail-configuration/trunk

« back to all changes in this revision

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

  • Committer: Robert Ayres
  • Date: 2014-09-10 14:03:02 UTC
  • Revision ID: robert.ayres@canonical.com-20140910140302-bqu0wb61an4nhgfa
Initial charm

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