[wolsen,r=]

Clean up context code, support multiple regions split by spaces.
This commit is contained in:
Billy Olsen
2014-12-01 16:45:41 -07:00
parent a237aeea00
commit b56189c6df
4 changed files with 60 additions and 17 deletions

View File

@@ -69,32 +69,41 @@ class IdentityServiceContext(OSContextGenerator):
def __call__(self):
log('Generating template context for identity-service')
ctxt = {}
regions = []
regions = set()
# TODO (wolsen) each region is normally split by keystone
# and enpoints created for each word in the region string.
# handle this scenario.
for rid in relation_ids('identity-service'):
for unit in related_units(rid):
rdata = relation_get(rid=rid, unit=unit)
serv_host = rdata.get('service_host')
serv_host = format_ipv6_addr(serv_host) or serv_host
region = rdata.get('region')
local_ctxt = {
'service_port': rdata.get('service_port'),
'service_host': serv_host,
'service_protocol':
rdata.get('service_protocol') or 'http',
'region': rdata.get('region')
rdata.get('service_protocol') or 'http'
}
if context_complete(local_ctxt):
regions.append(local_ctxt)
if len(regions) > 0:
# TODO (wolsen) added all the information from region[0]
# to not change template much. Need to refactor this.
ctxt['regions'] = regions
ctxt.update(regions[0])
if not context_complete(local_ctxt):
continue
# Update the service endpoint and title for each available
# region in order to support multi-region deployments
if region is not None:
endpoint = ("%(service_protocol)s://"
"%(service_host)s:%(service_port)s/v2.0") % \
local_ctxt
for reg in region.split():
regions.add((endpoint, reg))
if len(ctxt) == 0:
ctxt = local_ctxt
if len(regions) > 1:
avail_regions = map(lambda r: {'endpoint': r[0], 'title': r[1]},
regions)
ctxt['regions'] = sorted(avail_regions)
return ctxt

View File

@@ -120,11 +120,13 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ('http://cluster1.example.com:5000/v2.0', 'cluster1'),
# ('http://cluster2.example.com:5000/v2.0', 'cluster2'),
# ]
{% if regions|length > 1 -%}
AVAILABLE_REGIONS = [
{% for r in regions -%}
('{{r.service_protocol}}://{{r.service_host}}:{{r.service_port}}/v2.0', '{{r.region}}'),
{% for region in regions -%}
('{{ region.endpoint }}', '{{ region.title }}'),
{% endfor -%}
]
{% endif -%}
OPENSTACK_HOST = "{{ service_host }}"
OPENSTACK_KEYSTONE_URL = "{{ service_protocol }}://%s:{{ service_port }}/v2.0" % OPENSTACK_HOST

View File

@@ -132,11 +132,13 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ('http://cluster1.example.com:5000/v2.0', 'cluster1'),
# ('http://cluster2.example.com:5000/v2.0', 'cluster2'),
# ]
{% if regions|length > 1 -%}
AVAILABLE_REGIONS = [
{% for r in regions -%}
('{{r.service_protocol}}://{{r.service_host}}:{{r.service_port}}/v2.0', '{{r.region}}'),
{% for region in regions -%}
('{{ region.endpoint }}', '{{ region.title }}'),
{% endfor -%}
]
{% endif -%}
OPENSTACK_HOST = "{{ service_host }}"
OPENSTACK_KEYSTONE_URL = "{{ service_protocol }}://%s:{{ service_port }}/v2.0" % OPENSTACK_HOST

View File

@@ -180,6 +180,36 @@ class TestHorizonContexts(CharmTestCase):
{'service_host': 'foo', 'service_port': 5000,
'service_protocol': 'http'})
@patch("horizon_contexts.format_ipv6_addr")
def test_IdentityServiceContext_single_region(self, mock_format_ipv6_addr):
mock_format_ipv6_addr.return_value = "foo"
self.relation_ids.return_value = ['foo']
self.related_units.return_value = ['bar', 'baz']
self.relation_get.side_effect = self.test_relation.get
self.test_relation.set({'service_host': 'foo', 'service_port': 5000,
'region': 'regionOne'})
self.context_complete.return_value = True
self.assertEquals(horizon_contexts.IdentityServiceContext()(),
{'service_host': 'foo', 'service_port': 5000,
'service_protocol': 'http'})
@patch("horizon_contexts.format_ipv6_addr")
def test_IdentityServiceContext_multi_region(self, mock_format_ipv6_addr):
mock_format_ipv6_addr.return_value = "foo"
self.relation_ids.return_value = ['foo']
self.related_units.return_value = ['bar', 'baz']
self.relation_get.side_effect = self.test_relation.get
self.test_relation.set({'service_host': 'foo', 'service_port': 5000,
'region': 'regionOne regionTwo'})
self.context_complete.return_value = True
self.assertEqual(horizon_contexts.IdentityServiceContext()(),
{'service_host': 'foo', 'service_port': 5000,
'service_protocol': 'http',
'regions': [{'endpoint': 'http://foo:5000/v2.0',
'title': 'regionOne'},
{'endpoint': 'http://foo:5000/v2.0',
'title': 'regionTwo'}]})
def test_HorizonHAProxyContext_no_cluster(self):
self.relation_ids.return_value = []
self.local_unit.return_value = 'openstack-dashboard/0'