Fix get_watch_server_url for ipv6 addresses

https://review.openstack.org/#/c/508021 fixed it
to work in uwsgi mode but did broke it to work with
ipv6 as it contains ':' as part of the host.

This patch fixes how the URL is parsed in order to
work properly with ipv6.

Change-Id: I3965ee83e1f743e25cbdcbbec516be0ed6a30afc
Closes-Bug: #1721045
This commit is contained in:
Alfredo Moralejo 2017-10-03 18:59:19 +02:00
parent dd22ab2fc6
commit 54ac107d7f
3 changed files with 29 additions and 10 deletions

View File

@ -93,8 +93,11 @@ class HeatClientPlugin(client_plugin.ClientPlugin):
def get_watch_server_url(self):
cfn_url = self.get_heat_cfn_url()
parsed_url = urllib.parse.urlparse(cfn_url)
separator = ':'
(host, separator, port) = parsed_url.netloc.partition(separator)
host = parsed_url.hostname
port = parsed_url.port
# For ipv6 we need to include the host in brackets
if parsed_url.netloc.startswith('['):
host = "[%s]" % host
# The old url model, like http://localhost:port/v1
if port:
watch_api_port = (

View File

@ -75,7 +75,7 @@ class ClientsTest(common.HeatTestCase):
obj._get_client_option.return_value = result
self.assertEqual(result, obj.get_heat_url())
def _client_cfn_url(self, use_uwsgi=False):
def _client_cfn_url(self, use_uwsgi=False, use_ipv6=False):
con = mock.Mock()
c = clients.Clients(con)
con.clients = c
@ -83,10 +83,16 @@ class ClientsTest(common.HeatTestCase):
obj._get_client_option = mock.Mock()
obj._get_client_option.return_value = None
obj.url_for = mock.Mock(name="url_for")
if use_uwsgi:
obj.url_for.return_value = "http://0.0.0.0/heat-api-cfn/v1/"
if use_ipv6:
if use_uwsgi:
obj.url_for.return_value = "http://[::1]/heat-api-cfn/v1/"
else:
obj.url_for.return_value = "http://[::1]:8000/v1/"
else:
obj.url_for.return_value = "http://0.0.0.0:8000/v1/"
if use_uwsgi:
obj.url_for.return_value = "http://0.0.0.0/heat-api-cfn/v1/"
else:
obj.url_for.return_value = "http://0.0.0.0:8000/v1/"
return obj
def test_clients_get_heat_cfn_url(self):
@ -98,6 +104,16 @@ class ClientsTest(common.HeatTestCase):
self.assertEqual("http://0.0.0.0:8003/v1/",
obj.get_watch_server_url())
def test_clients_get_watch_server_url_ipv6(self):
obj = self._client_cfn_url(use_ipv6=True)
self.assertEqual("http://[::1]:8003/v1/",
obj.get_watch_server_url())
def test_clients_get_watch_server_url_use_uwsgi_ipv6(self):
obj = self._client_cfn_url(use_uwsgi=True, use_ipv6=True)
self.assertEqual("http://[::1]/heat-api-cloudwatch/v1/",
obj.get_watch_server_url())
def test_clients_get_watch_server_url_use_uwsgi(self):
obj = self._client_cfn_url(use_uwsgi=True)
self.assertEqual("http://0.0.0.0/heat-api-cloudwatch/v1/",

View File

@ -786,14 +786,14 @@ class ServersTest(common.HeatTestCase):
@mock.patch.object(heat_plugin.HeatClientPlugin, 'url_for')
def test_server_create_software_config(self, fake_url):
fake_url.return_value = 'http://ip:port/v1'
fake_url.return_value = 'http://ip:8000/v1'
server = self._server_create_software_config()
self.assertEqual({
'os-collect-config': {
'cfn': {
'access_key_id': '4567',
'metadata_url': 'http://ip:port/v1/',
'metadata_url': 'http://ip:8000/v1/',
'path': 'WebServer.Metadata',
'secret_access_key': '8901',
'stack_name': 'software_config_s'
@ -1650,7 +1650,7 @@ class ServersTest(common.HeatTestCase):
@mock.patch.object(heat_plugin.HeatClientPlugin, 'url_for')
def test_server_update_metadata_software_config(self, fake_url):
fake_url.return_value = 'http://ip:port/v1'
fake_url.return_value = 'http://ip:8000/v1'
server, ud_tmpl = self._server_create_software_config(
stack_name='update_meta_sc', ret_tmpl=True)
@ -1658,7 +1658,7 @@ class ServersTest(common.HeatTestCase):
'os-collect-config': {
'cfn': {
'access_key_id': '4567',
'metadata_url': 'http://ip:port/v1/',
'metadata_url': 'http://ip:8000/v1/',
'path': 'WebServer.Metadata',
'secret_access_key': '8901',
'stack_name': 'update_meta_sc'