~james-page/charms/trusty/swift-storage/lp1531102-trunk

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2015-08-10 16:39:22 UTC
  • Revision ID: james.page@ubuntu.com-20150810163922-45l3t6prprlctpd2
Tags: 15.07
[gnuoy] 15.07 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from charmhelpers.core.hookenv import (
18
18
    config,
19
19
    unit_get,
 
20
    service_name,
20
21
)
21
22
from charmhelpers.contrib.network.ip import (
22
23
    get_address_in_network,
26
27
)
27
28
from charmhelpers.contrib.hahelpers.cluster import is_clustered
28
29
 
29
 
from functools import partial
30
 
 
31
30
PUBLIC = 'public'
32
31
INTERNAL = 'int'
33
32
ADMIN = 'admin'
35
34
ADDRESS_MAP = {
36
35
    PUBLIC: {
37
36
        'config': 'os-public-network',
38
 
        'fallback': 'public-address'
 
37
        'fallback': 'public-address',
 
38
        'override': 'os-public-hostname',
39
39
    },
40
40
    INTERNAL: {
41
41
        'config': 'os-internal-network',
42
 
        'fallback': 'private-address'
 
42
        'fallback': 'private-address',
 
43
        'override': 'os-internal-hostname',
43
44
    },
44
45
    ADMIN: {
45
46
        'config': 'os-admin-network',
46
 
        'fallback': 'private-address'
 
47
        'fallback': 'private-address',
 
48
        'override': 'os-admin-hostname',
47
49
    }
48
50
}
49
51
 
57
59
    :param endpoint_type: str endpoint type to resolve.
58
60
    :param returns: str base URL for services on the current service unit.
59
61
    """
60
 
    scheme = 'http'
61
 
    if 'https' in configs.complete_contexts():
62
 
        scheme = 'https'
 
62
    scheme = _get_scheme(configs)
 
63
 
63
64
    address = resolve_address(endpoint_type)
64
65
    if is_ipv6(address):
65
66
        address = "[{}]".format(address)
 
67
 
66
68
    return '%s://%s' % (scheme, address)
67
69
 
68
70
 
 
71
def _get_scheme(configs):
 
72
    """Returns the scheme to use for the url (either http or https)
 
73
    depending upon whether https is in the configs value.
 
74
 
 
75
    :param configs: OSTemplateRenderer config templating object to inspect
 
76
                    for a complete https context.
 
77
    :returns: either 'http' or 'https' depending on whether https is
 
78
              configured within the configs context.
 
79
    """
 
80
    scheme = 'http'
 
81
    if configs and 'https' in configs.complete_contexts():
 
82
        scheme = 'https'
 
83
    return scheme
 
84
 
 
85
 
 
86
def _get_address_override(endpoint_type=PUBLIC):
 
87
    """Returns any address overrides that the user has defined based on the
 
88
    endpoint type.
 
89
 
 
90
    Note: this function allows for the service name to be inserted into the
 
91
    address if the user specifies {service_name}.somehost.org.
 
92
 
 
93
    :param endpoint_type: the type of endpoint to retrieve the override
 
94
                          value for.
 
95
    :returns: any endpoint address or hostname that the user has overridden
 
96
              or None if an override is not present.
 
97
    """
 
98
    override_key = ADDRESS_MAP[endpoint_type]['override']
 
99
    addr_override = config(override_key)
 
100
    if not addr_override:
 
101
        return None
 
102
    else:
 
103
        return addr_override.format(service_name=service_name())
 
104
 
 
105
 
69
106
def resolve_address(endpoint_type=PUBLIC):
70
107
    """Return unit address depending on net config.
71
108
 
77
114
 
78
115
    :param endpoint_type: Network endpoing type
79
116
    """
80
 
    resolved_address = None
 
117
    resolved_address = _get_address_override(endpoint_type)
 
118
    if resolved_address:
 
119
        return resolved_address
 
120
 
81
121
    vips = config('vip')
82
122
    if vips:
83
123
        vips = vips.split()
109
149
                         "clustered=%s)" % (net_type, clustered))
110
150
 
111
151
    return resolved_address
112
 
 
113
 
 
114
 
def endpoint_url(configs, url_template, port, endpoint_type=PUBLIC,
115
 
                 override=None):
116
 
    """Returns the correct endpoint URL to advertise to Keystone.
117
 
 
118
 
    This method provides the correct endpoint URL which should be advertised to
119
 
    the keystone charm for endpoint creation. This method allows for the url to
120
 
    be overridden to force a keystone endpoint to have specific URL for any of
121
 
    the defined scopes (admin, internal, public).
122
 
 
123
 
    :param configs: OSTemplateRenderer config templating object to inspect
124
 
                    for a complete https context.
125
 
    :param url_template: str format string for creating the url template. Only
126
 
                         two values will be passed - the scheme+hostname
127
 
                        returned by the canonical_url and the port.
128
 
    :param endpoint_type: str endpoint type to resolve.
129
 
    :param override: str the name of the config option which overrides the
130
 
                     endpoint URL defined by the charm itself. None will
131
 
                     disable any overrides (default).
132
 
    """
133
 
    if override:
134
 
        # Return any user-defined overrides for the keystone endpoint URL.
135
 
        user_value = config(override)
136
 
        if user_value:
137
 
            return user_value.strip()
138
 
 
139
 
    return url_template % (canonical_url(configs, endpoint_type), port)
140
 
 
141
 
 
142
 
public_endpoint = partial(endpoint_url, endpoint_type=PUBLIC)
143
 
 
144
 
internal_endpoint = partial(endpoint_url, endpoint_type=INTERNAL)
145
 
 
146
 
admin_endpoint = partial(endpoint_url, endpoint_type=ADMIN)