From 9684ee4546762f425029988573a11c0418ed92ee Mon Sep 17 00:00:00 2001 From: JUN JIE NAN Date: Tue, 31 Dec 2013 12:25:55 +0800 Subject: [PATCH] Added heat url in config for heat standalone mode And use heat url for heat standalone mode in client heat. Closes-Bug: #1255713 Change-Id: Ibbb0c53bd88b77b4444b652cebbd06034f69cebe --- etc/heat/heat.conf.sample | 4 ++++ heat/common/config.py | 5 +++++ heat/engine/clients.py | 12 +++++++++++- heat/tests/test_clients.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/etc/heat/heat.conf.sample b/etc/heat/heat.conf.sample index 0ef55c60f..0f750d963 100644 --- a/etc/heat/heat.conf.sample +++ b/etc/heat/heat.conf.sample @@ -584,6 +584,10 @@ # (boolean value) #insecure=false +# Optional heat url in format like +# http://0.0.0.0:8004/v1/%(tenant_id)s. (string value) +#url= + [clients_keystone] diff --git a/heat/common/config.py b/heat/common/config.py index f9265517d..3d2a930a0 100644 --- a/heat/common/config.py +++ b/heat/common/config.py @@ -151,6 +151,11 @@ def register_clients_opts(): # register opts copy and put it to globals in order to # generate_sample.sh to work opts_copy = copy.deepcopy(clients_opts) + if client == 'heat': + opts_copy.append( + cfg.StrOpt('url', + help=_('Optional heat url in format like' + ' http://0.0.0.0:8004/v1/%(tenant_id)s.'))) globals()[client_specific_group + '_opts'] = opts_copy cfg.CONF.register_opts(opts_copy, group=client_specific_group) diff --git a/heat/engine/clients.py b/heat/engine/clients.py index 1250cf6d7..b3b6a49a6 100644 --- a/heat/engine/clients.py +++ b/heat/engine/clients.py @@ -270,6 +270,13 @@ class OpenStackClients(object): cfg.CONF.import_opt(option, 'heat.common.config', group='clients') return getattr(cfg.CONF.clients, option) + def _get_heat_url(self): + heat_url = self._get_client_option('heat', 'url') + if heat_url: + tenant_id = self.context.tenant_id + heat_url = heat_url % {'tenant_id': tenant_id} + return heat_url + def heat(self): if self._heat: return self._heat @@ -290,7 +297,10 @@ class OpenStackClients(object): 'insecure': self._get_client_option('heat', 'insecure') } - endpoint = self.url_for(service_type='orchestration') + endpoint = self._get_heat_url() + if not endpoint: + endpoint = self.url_for(service_type='orchestration') + self._heat = heatclient.Client('1', endpoint, **args) return self._heat diff --git a/heat/tests/test_clients.py b/heat/tests/test_clients.py index 6aea47586..ae997e285 100644 --- a/heat/tests/test_clients.py +++ b/heat/tests/test_clients.py @@ -12,8 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from heat.engine import clients from heat.tests.common import HeatTestCase +from heatclient import client as heatclient class ClientsTest(HeatTestCase): @@ -21,3 +24,36 @@ class ClientsTest(HeatTestCase): def test_clients_chosen_at_module_initilization(self): self.assertFalse(hasattr(clients.Clients, 'nova')) self.assertTrue(hasattr(clients.Clients('fakecontext'), 'nova')) + + def test_clients_get_heat_url(self): + con = mock.Mock() + con.tenant_id = "b363706f891f48019483f8bd6503c54b" + obj = clients.Clients(con) + obj._get_client_option = mock.Mock() + obj._get_client_option.return_value = None + self.assertEqual(None, obj._get_heat_url()) + heat_url = "http://0.0.0.0:8004/v1/%(tenant_id)s" + obj._get_client_option.return_value = heat_url + tenant_id = "b363706f891f48019483f8bd6503c54b" + result = heat_url % {"tenant_id": tenant_id} + self.assertEqual(result, obj._get_heat_url()) + obj._get_client_option.return_value = result + self.assertEqual(result, obj._get_heat_url()) + + @mock.patch.object(heatclient, 'Client') + def test_clients_heat(self, mock_call): + con = mock.Mock() + con.auth_url = "http://auth.example.com:5000/v2.0" + con.tenant_id = "b363706f891f48019483f8bd6503c54b" + con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155" + obj = clients.Clients(con) + obj._get_heat_url = mock.Mock(name="_get_heat_url") + obj._get_heat_url.return_value = None + obj.url_for = mock.Mock(name="url_for") + obj.url_for.return_value = "url_from_keystone" + obj.heat() + self.assertEqual('url_from_keystone', mock_call.call_args[0][1]) + obj._get_heat_url.return_value = "url_from_config" + obj._heat = None + obj.heat() + self.assertEqual('url_from_config', mock_call.call_args[0][1])