Merge "Add transport aliases"

This commit is contained in:
Jenkins 2013-12-07 10:14:14 +00:00 committed by Gerrit Code Review
commit 1e8ef86626
3 changed files with 90 additions and 27 deletions

View File

@ -123,7 +123,7 @@ class DriverLoadFailure(exceptions.MessagingException):
self.ex = ex self.ex = ex
def get_transport(conf, url=None, allowed_remote_exmods=[]): def get_transport(conf, url=None, allowed_remote_exmods=[], aliases=None):
"""A factory method for Transport objects. """A factory method for Transport objects.
This method will construct a Transport object from transport configuration This method will construct a Transport object from transport configuration
@ -150,12 +150,14 @@ def get_transport(conf, url=None, allowed_remote_exmods=[]):
transport will deserialize remote exceptions transport will deserialize remote exceptions
from from
:type allowed_remote_exmods: list :type allowed_remote_exmods: list
:param aliases: A map of transport alias to transport name
:type aliases: dict
""" """
conf.register_opts(_transport_opts) conf.register_opts(_transport_opts)
if not isinstance(url, TransportURL): if not isinstance(url, TransportURL):
url = url or conf.transport_url url = url or conf.transport_url
parsed = TransportURL.parse(conf, url) parsed = TransportURL.parse(conf, url, aliases)
if not parsed.transport: if not parsed.transport:
raise InvalidTransportURL(url, 'No scheme specified in "%s"' % url) raise InvalidTransportURL(url, 'No scheme specified in "%s"' % url)
url = parsed url = parsed
@ -220,9 +222,12 @@ class TransportURL(object):
:type virtual_host: str :type virtual_host: str
:param hosts: a list of TransportHost objects :param hosts: a list of TransportHost objects
:type hosts: list :type hosts: list
:param aliases: A map of transport alias to transport name
:type aliases: dict
""" """
def __init__(self, conf, transport=None, virtual_host=None, hosts=None): def __init__(self, conf, transport=None, virtual_host=None, hosts=None,
aliases=None):
self.conf = conf self.conf = conf
self.conf.register_opts(_transport_opts) self.conf.register_opts(_transport_opts)
self._transport = transport self._transport = transport
@ -231,13 +236,18 @@ class TransportURL(object):
self.hosts = [] self.hosts = []
else: else:
self.hosts = hosts self.hosts = hosts
if aliases is None:
self.aliases = {}
else:
self.aliases = aliases
@property @property
def transport(self): def transport(self):
if self._transport is None: if self._transport is None:
return self.conf.rpc_backend transport = self.conf.rpc_backend
else: else:
return self._transport transport = self._transport
return self.aliases.get(transport, transport)
@transport.setter @transport.setter
def transport(self, value): def transport(self, value):
@ -300,7 +310,7 @@ class TransportURL(object):
return url return url
@classmethod @classmethod
def parse(cls, conf, url): def parse(cls, conf, url, aliases=None):
"""Parse an url. """Parse an url.
Assuming a URL takes the form of: Assuming a URL takes the form of:
@ -337,10 +347,12 @@ class TransportURL(object):
:type conf: oslo.config.cfg.ConfigOpts :type conf: oslo.config.cfg.ConfigOpts
:param url: The URL to parse :param url: The URL to parse
:type url: str :type url: str
:param aliases: A map of transport alias to transport name
:type aliases: dict
:returns: A TransportURL :returns: A TransportURL
""" """
if not url: if not url:
return cls(conf) return cls(conf, aliases=aliases)
# FIXME(flaper87): Not PY3K compliant # FIXME(flaper87): Not PY3K compliant
if not isinstance(url, basestring): if not isinstance(url, basestring):
@ -402,4 +414,4 @@ class TransportURL(object):
username=username, username=username,
password=password)) password=password))
return cls(conf, url.scheme, virtual_host, hosts) return cls(conf, url.scheme, virtual_host, hosts, aliases)

View File

@ -55,39 +55,63 @@ class GetTransportTestCase(test_utils.BaseTestCase):
scenarios = [ scenarios = [
('rpc_backend', ('rpc_backend',
dict(url=None, transport_url=None, rpc_backend='testbackend', dict(url=None, transport_url=None, rpc_backend='testbackend',
control_exchange=None, allowed=None, control_exchange=None, allowed=None, aliases=None,
expect=dict(backend='testbackend', expect=dict(backend='testbackend',
exchange=None, exchange=None,
url='testbackend:', url='testbackend:',
allowed=[]))), allowed=[]))),
('transport_url', ('transport_url',
dict(url=None, transport_url='testtransport:', rpc_backend=None, dict(url=None, transport_url='testtransport:', rpc_backend=None,
control_exchange=None, allowed=None, control_exchange=None, allowed=None, aliases=None,
expect=dict(backend='testtransport', expect=dict(backend='testtransport',
exchange=None, exchange=None,
url='testtransport:', url='testtransport:',
allowed=[]))), allowed=[]))),
('url_param', ('url_param',
dict(url='testtransport:', transport_url=None, rpc_backend=None, dict(url='testtransport:', transport_url=None, rpc_backend=None,
control_exchange=None, allowed=None, control_exchange=None, allowed=None, aliases=None,
expect=dict(backend='testtransport', expect=dict(backend='testtransport',
exchange=None, exchange=None,
url='testtransport:', url='testtransport:',
allowed=[]))), allowed=[]))),
('control_exchange', ('control_exchange',
dict(url=None, transport_url=None, rpc_backend='testbackend', dict(url=None, transport_url=None, rpc_backend='testbackend',
control_exchange='testexchange', allowed=None, control_exchange='testexchange', allowed=None, aliases=None,
expect=dict(backend='testbackend', expect=dict(backend='testbackend',
exchange='testexchange', exchange='testexchange',
url='testbackend:', url='testbackend:',
allowed=[]))), allowed=[]))),
('allowed_remote_exmods', ('allowed_remote_exmods',
dict(url=None, transport_url=None, rpc_backend='testbackend', dict(url=None, transport_url=None, rpc_backend='testbackend',
control_exchange=None, allowed=['foo', 'bar'], control_exchange=None, allowed=['foo', 'bar'], aliases=None,
expect=dict(backend='testbackend', expect=dict(backend='testbackend',
exchange=None, exchange=None,
url='testbackend:', url='testbackend:',
allowed=['foo', 'bar']))), allowed=['foo', 'bar']))),
('rpc_backend_aliased',
dict(url=None, transport_url=None, rpc_backend='testfoo',
control_exchange=None, allowed=None,
aliases=dict(testfoo='testbackend'),
expect=dict(backend='testbackend',
exchange=None,
url='testbackend:',
allowed=[]))),
('transport_url_aliased',
dict(url=None, transport_url='testfoo:', rpc_backend=None,
control_exchange=None, allowed=None,
aliases=dict(testfoo='testtransport'),
expect=dict(backend='testtransport',
exchange=None,
url='testtransport:',
allowed=[]))),
('url_param_aliased',
dict(url='testfoo:', transport_url=None, rpc_backend=None,
control_exchange=None, allowed=None,
aliases=dict(testfoo='testtransport'),
expect=dict(backend='testtransport',
exchange=None,
url='testtransport:',
allowed=[]))),
] ]
def setUp(self): def setUp(self):
@ -120,6 +144,8 @@ class GetTransportTestCase(test_utils.BaseTestCase):
kwargs = dict(url=self.url) kwargs = dict(url=self.url)
if self.allowed is not None: if self.allowed is not None:
kwargs['allowed_remote_exmods'] = self.allowed kwargs['allowed_remote_exmods'] = self.allowed
if self.aliases is not None:
kwargs['aliases'] = self.aliases
transport = messaging.get_transport(self.conf, **kwargs) transport = messaging.get_transport(self.conf, **kwargs)
self.assertIsNotNone(transport) self.assertIsNotNone(transport)

View File

@ -27,51 +27,54 @@ class TestParseURL(test_utils.BaseTestCase):
scenarios = [ scenarios = [
('transport', ('transport',
dict(url='foo:', dict(url='foo:', aliases=None,
expect=dict(transport='foo'))),
('transport_aliased',
dict(url='bar:', aliases=dict(bar='foo'),
expect=dict(transport='foo'))), expect=dict(transport='foo'))),
('virtual_host_slash', ('virtual_host_slash',
dict(url='foo:////', dict(url='foo:////', aliases=None,
expect=dict(transport='foo', virtual_host='/'))), expect=dict(transport='foo', virtual_host='/'))),
('virtual_host', ('virtual_host',
dict(url='foo:///bar', dict(url='foo:///bar', aliases=None,
expect=dict(transport='foo', virtual_host='bar'))), expect=dict(transport='foo', virtual_host='bar'))),
('host', ('host',
dict(url='foo://host/bar', dict(url='foo://host/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(host='host'), dict(host='host'),
]))), ]))),
('ipv6_host', ('ipv6_host',
dict(url='foo://[ffff::1]/bar', dict(url='foo://[ffff::1]/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(host='ffff::1'), dict(host='ffff::1'),
]))), ]))),
('port', ('port',
dict(url='foo://host:1234/bar', dict(url='foo://host:1234/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(host='host', port=1234), dict(host='host', port=1234),
]))), ]))),
('ipv6_port', ('ipv6_port',
dict(url='foo://[ffff::1]:1234/bar', dict(url='foo://[ffff::1]:1234/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(host='ffff::1', port=1234), dict(host='ffff::1', port=1234),
]))), ]))),
('username', ('username',
dict(url='foo://u@host:1234/bar', dict(url='foo://u@host:1234/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(host='host', port=1234, username='u'), dict(host='host', port=1234, username='u'),
]))), ]))),
('password', ('password',
dict(url='foo://u:p@host:1234/bar', dict(url='foo://u:p@host:1234/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
@ -79,14 +82,14 @@ class TestParseURL(test_utils.BaseTestCase):
username='u', password='p'), username='u', password='p'),
]))), ]))),
('creds_no_host', ('creds_no_host',
dict(url='foo://u:p@/bar', dict(url='foo://u:p@/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
dict(username='u', password='p'), dict(username='u', password='p'),
]))), ]))),
('multi_host', ('multi_host',
dict(url='foo://u:p@host1:1234,host2:4321/bar', dict(url='foo://u:p@host1:1234,host2:4321/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
@ -95,7 +98,7 @@ class TestParseURL(test_utils.BaseTestCase):
dict(host='host2', port=4321), dict(host='host2', port=4321),
]))), ]))),
('multi_creds', ('multi_creds',
dict(url='foo://u1:p1@host1:1234,u2:p2@host2:4321/bar', dict(url='foo://u1:p1@host1:1234,u2:p2@host2:4321/bar', aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
@ -106,6 +109,7 @@ class TestParseURL(test_utils.BaseTestCase):
]))), ]))),
('multi_creds_ipv6', ('multi_creds_ipv6',
dict(url='foo://u1:p1@[ffff::1]:1234,u2:p2@[ffff::2]:4321/bar', dict(url='foo://u1:p1@[ffff::1]:1234,u2:p2@[ffff::2]:4321/bar',
aliases=None,
expect=dict(transport='foo', expect=dict(transport='foo',
virtual_host='bar', virtual_host='bar',
hosts=[ hosts=[
@ -123,7 +127,7 @@ class TestParseURL(test_utils.BaseTestCase):
def test_parse_url(self): def test_parse_url(self):
self.config(rpc_backend=None) self.config(rpc_backend=None)
url = messaging.TransportURL.parse(self.conf, self.url) url = messaging.TransportURL.parse(self.conf, self.url, self.aliases)
hosts = [] hosts = []
for host in self.expect.get('hosts', []): for host in self.expect.get('hosts', []):
@ -147,18 +151,35 @@ class TestFormatURL(test_utils.BaseTestCase):
transport=None, transport=None,
virtual_host=None, virtual_host=None,
hosts=[], hosts=[],
aliases=None,
expected='testbackend:///')),
('rpc_backend_aliased',
dict(rpc_backend='testfoo',
transport=None,
virtual_host=None,
hosts=[],
aliases=dict(testfoo='testbackend'),
expected='testbackend:///')), expected='testbackend:///')),
('transport', ('transport',
dict(rpc_backend=None, dict(rpc_backend=None,
transport='testtransport', transport='testtransport',
virtual_host=None, virtual_host=None,
hosts=[], hosts=[],
aliases=None,
expected='testtransport:///')),
('transport_aliased',
dict(rpc_backend=None,
transport='testfoo',
virtual_host=None,
hosts=[],
aliases=dict(testfoo='testtransport'),
expected='testtransport:///')), expected='testtransport:///')),
('virtual_host', ('virtual_host',
dict(rpc_backend=None, dict(rpc_backend=None,
transport='testtransport', transport='testtransport',
virtual_host='/vhost', virtual_host='/vhost',
hosts=[], hosts=[],
aliases=None,
expected='testtransport:////vhost')), expected='testtransport:////vhost')),
('host', ('host',
dict(rpc_backend=None, dict(rpc_backend=None,
@ -170,6 +191,7 @@ class TestFormatURL(test_utils.BaseTestCase):
username='bob', username='bob',
password='secret'), password='secret'),
], ],
aliases=None,
expected='testtransport://bob:secret@host:10//')), expected='testtransport://bob:secret@host:10//')),
('multi_host', ('multi_host',
dict(rpc_backend=None, dict(rpc_backend=None,
@ -185,6 +207,7 @@ class TestFormatURL(test_utils.BaseTestCase):
username='b2', username='b2',
password='s2'), password='s2'),
], ],
aliases=None,
expected='testtransport://b1:s1@h1:1000,b2:s2@h2:2000/')), expected='testtransport://b1:s1@h1:1000,b2:s2@h2:2000/')),
('quoting', ('quoting',
dict(rpc_backend=None, dict(rpc_backend=None,
@ -196,6 +219,7 @@ class TestFormatURL(test_utils.BaseTestCase):
username='b$', username='b$',
password='s&'), password='s&'),
], ],
aliases=None,
expected='testtransport://b%24:s%26@host:10//%24')), expected='testtransport://b%24:s%26@host:10//%24')),
] ]
@ -216,6 +240,7 @@ class TestFormatURL(test_utils.BaseTestCase):
url = messaging.TransportURL(self.conf, url = messaging.TransportURL(self.conf,
self.transport, self.transport,
self.virtual_host, self.virtual_host,
hosts) hosts,
self.aliases)
self.assertEqual(str(url), self.expected) self.assertEqual(str(url), self.expected)