~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

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

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from charmhelpers.core.hookenv import (
 
18
    config,
 
19
    unit_get,
 
20
)
 
21
from charmhelpers.contrib.network.ip import (
 
22
    get_address_in_network,
 
23
    is_address_in_network,
 
24
    is_ipv6,
 
25
    get_ipv6_addr,
 
26
)
 
27
from charmhelpers.contrib.hahelpers.cluster import is_clustered
 
28
 
 
29
PUBLIC = 'public'
 
30
INTERNAL = 'int'
 
31
ADMIN = 'admin'
 
32
 
 
33
ADDRESS_MAP = {
 
34
    PUBLIC: {
 
35
        'config': 'os-public-network',
 
36
        'fallback': 'public-address'
 
37
    },
 
38
    INTERNAL: {
 
39
        'config': 'os-internal-network',
 
40
        'fallback': 'private-address'
 
41
    },
 
42
    ADMIN: {
 
43
        'config': 'os-admin-network',
 
44
        'fallback': 'private-address'
 
45
    }
 
46
}
 
47
 
 
48
 
 
49
def canonical_url(configs, endpoint_type=PUBLIC):
 
50
    """Returns the correct HTTP URL to this host given the state of HTTPS
 
51
    configuration, hacluster and charm configuration.
 
52
 
 
53
    :param configs: OSTemplateRenderer config templating object to inspect
 
54
                    for a complete https context.
 
55
    :param endpoint_type: str endpoint type to resolve.
 
56
    :param returns: str base URL for services on the current service unit.
 
57
    """
 
58
    scheme = 'http'
 
59
    if 'https' in configs.complete_contexts():
 
60
        scheme = 'https'
 
61
    address = resolve_address(endpoint_type)
 
62
    if is_ipv6(address):
 
63
        address = "[{}]".format(address)
 
64
    return '%s://%s' % (scheme, address)
 
65
 
 
66
 
 
67
def resolve_address(endpoint_type=PUBLIC):
 
68
    """Return unit address depending on net config.
 
69
 
 
70
    If unit is clustered with vip(s) and has net splits defined, return vip on
 
71
    correct network. If clustered with no nets defined, return primary vip.
 
72
 
 
73
    If not clustered, return unit address ensuring address is on configured net
 
74
    split if one is configured.
 
75
 
 
76
    :param endpoint_type: Network endpoing type
 
77
    """
 
78
    resolved_address = None
 
79
    vips = config('vip')
 
80
    if vips:
 
81
        vips = vips.split()
 
82
 
 
83
    net_type = ADDRESS_MAP[endpoint_type]['config']
 
84
    net_addr = config(net_type)
 
85
    net_fallback = ADDRESS_MAP[endpoint_type]['fallback']
 
86
    clustered = is_clustered()
 
87
    if clustered:
 
88
        if not net_addr:
 
89
            # If no net-splits defined, we expect a single vip
 
90
            resolved_address = vips[0]
 
91
        else:
 
92
            for vip in vips:
 
93
                if is_address_in_network(net_addr, vip):
 
94
                    resolved_address = vip
 
95
                    break
 
96
    else:
 
97
        if config('prefer-ipv6'):
 
98
            fallback_addr = get_ipv6_addr(exc_list=vips)[0]
 
99
        else:
 
100
            fallback_addr = unit_get(net_fallback)
 
101
 
 
102
        resolved_address = get_address_in_network(net_addr, fallback_addr)
 
103
 
 
104
    if resolved_address is None:
 
105
        raise ValueError("Unable to resolve a suitable IP address based on "
 
106
                         "charm state and configuration. (net_type=%s, "
 
107
                         "clustered=%s)" % (net_type, clustered))
 
108
 
 
109
    return resolved_address