1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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/>.
17
from charmhelpers.core.hookenv import (
21
from charmhelpers.contrib.network.ip import (
22
get_address_in_network,
23
is_address_in_network,
27
from charmhelpers.contrib.hahelpers.cluster import is_clustered
35
'config': 'os-public-network',
36
'fallback': 'public-address'
39
'config': 'os-internal-network',
40
'fallback': 'private-address'
43
'config': 'os-admin-network',
44
'fallback': 'private-address'
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.
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.
59
if 'https' in configs.complete_contexts():
61
address = resolve_address(endpoint_type)
63
address = "[{}]".format(address)
64
return '%s://%s' % (scheme, address)
67
def resolve_address(endpoint_type=PUBLIC):
68
"""Return unit address depending on net config.
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.
73
If not clustered, return unit address ensuring address is on configured net
74
split if one is configured.
76
:param endpoint_type: Network endpoing type
78
resolved_address = None
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()
89
# If no net-splits defined, we expect a single vip
90
resolved_address = vips[0]
93
if is_address_in_network(net_addr, vip):
94
resolved_address = vip
97
if config('prefer-ipv6'):
98
fallback_addr = get_ipv6_addr(exc_list=vips)[0]
100
fallback_addr = unit_get(net_fallback)
102
resolved_address = get_address_in_network(net_addr, fallback_addr)
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))
109
return resolved_address