~hopem/charms/trusty/cinder/ensure-apache-restart

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-01-09 16:02:39 UTC
  • mfrom: (65 cinder.next)
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: liam.young@canonical.com-20150109160239-qldk423wxfno2ao3
Merged next in and resolved conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    config,
3
3
    unit_get,
4
4
)
5
 
 
6
5
from charmhelpers.contrib.network.ip import (
7
6
    get_address_in_network,
8
7
    is_address_in_network,
9
8
    is_ipv6,
10
9
    get_ipv6_addr,
11
10
)
12
 
 
13
11
from charmhelpers.contrib.hahelpers.cluster import is_clustered
14
12
 
15
13
PUBLIC = 'public'
16
14
INTERNAL = 'int'
17
15
ADMIN = 'admin'
18
16
 
19
 
_address_map = {
 
17
ADDRESS_MAP = {
20
18
    PUBLIC: {
21
19
        'config': 'os-public-network',
22
20
        'fallback': 'public-address'
33
31
 
34
32
 
35
33
def canonical_url(configs, endpoint_type=PUBLIC):
36
 
    '''
37
 
    Returns the correct HTTP URL to this host given the state of HTTPS
 
34
    """Returns the correct HTTP URL to this host given the state of HTTPS
38
35
    configuration, hacluster and charm configuration.
39
36
 
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
 
    '''
 
37
    :param configs: OSTemplateRenderer config templating object to inspect
 
38
                    for a complete https context.
 
39
    :param endpoint_type: str endpoint type to resolve.
 
40
    :param returns: str base URL for services on the current service unit.
 
41
    """
46
42
    scheme = 'http'
47
43
    if 'https' in configs.complete_contexts():
48
44
        scheme = 'https'
53
49
 
54
50
 
55
51
def resolve_address(endpoint_type=PUBLIC):
 
52
    """Return unit address depending on net config.
 
53
 
 
54
    If unit is clustered with vip(s) and has net splits defined, return vip on
 
55
    correct network. If clustered with no nets defined, return primary vip.
 
56
 
 
57
    If not clustered, return unit address ensuring address is on configured net
 
58
    split if one is configured.
 
59
 
 
60
    :param endpoint_type: Network endpoing type
 
61
    """
56
62
    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')
 
63
    vips = config('vip')
 
64
    if vips:
 
65
        vips = vips.split()
 
66
 
 
67
    net_type = ADDRESS_MAP[endpoint_type]['config']
 
68
    net_addr = config(net_type)
 
69
    net_fallback = ADDRESS_MAP[endpoint_type]['fallback']
 
70
    clustered = is_clustered()
 
71
    if clustered:
 
72
        if not net_addr:
 
73
            # If no net-splits defined, we expect a single vip
 
74
            resolved_address = vips[0]
61
75
        else:
62
 
            for vip in config('vip').split():
63
 
                if is_address_in_network(
64
 
                        config(_address_map[endpoint_type]['config']),
65
 
                        vip):
 
76
            for vip in vips:
 
77
                if is_address_in_network(net_addr, vip):
66
78
                    resolved_address = vip
 
79
                    break
67
80
    else:
68
81
        if config('prefer-ipv6'):
69
 
            fallback_addr = get_ipv6_addr(exc_list=[config('vip')])[0]
 
82
            fallback_addr = get_ipv6_addr(exc_list=vips)[0]
70
83
        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)
 
84
            fallback_addr = unit_get(net_fallback)
 
85
 
 
86
        resolved_address = get_address_in_network(net_addr, fallback_addr)
74
87
 
75
88
    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
 
89
        raise ValueError("Unable to resolve a suitable IP address based on "
 
90
                         "charm state and configuration. (net_type=%s, "
 
91
                         "clustered=%s)" % (net_type, clustered))
 
92
 
 
93
    return resolved_address