diff --git a/heat/engine/clients/os/keystone.py b/heat/engine/clients/os/keystone.py index a4549fe48..dcc8d7cc6 100644 --- a/heat/engine/clients/os/keystone.py +++ b/heat/engine/clients/os/keystone.py @@ -123,6 +123,14 @@ class KeystoneClientPlugin(client_plugin.ClientPlugin): raise exception.EntityNotFound(entity='KeystoneUser', name=user) + def get_region_id(self, region): + try: + region_obj = self.client().client.regions.get(region) + return region_obj.id + except exceptions.NotFound: + raise exception.EntityNotFound(entity='KeystoneRegion', + name=region) + class KeystoneRoleConstraint(constraints.BaseCustomConstraint): diff --git a/heat/tests/clients/test_keystone_client.py b/heat/tests/clients/test_keystone_client.py index 0f706c297..31ec16c1f 100644 --- a/heat/tests/clients/test_keystone_client.py +++ b/heat/tests/clients/test_keystone_client.py @@ -625,3 +625,49 @@ class KeystoneClientPluginUserTest(common.HeatTestCase): self.sample_name) self._client.client.users.list.assert_called_once_with( name=self.sample_name) + + +class KeystoneClientPluginRegionTest(common.HeatTestCase): + + sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152' + sample_name = 'sample_region' + + def _get_mock_region(self): + region = mock.MagicMock() + region.id = self.sample_uuid + region.name = self.sample_name + return region + + def setUp(self): + super(KeystoneClientPluginRegionTest, self).setUp() + self._client = mock.MagicMock() + + @mock.patch.object(client.KeystoneClientPlugin, 'client') + def test_get_region_id(self, client_keystone): + self._client.client.regions.get.return_value = self._get_mock_region() + + client_keystone.return_value = self._client + client_plugin = client.KeystoneClientPlugin( + context=mock.MagicMock() + ) + + self.assertEqual(self.sample_uuid, + client_plugin.get_region_id(self.sample_uuid)) + self._client.client.regions.get.assert_called_once_with( + self.sample_uuid) + + @mock.patch.object(client.KeystoneClientPlugin, 'client') + def test_get_region_id_not_found(self, client_keystone): + self._client.client.regions.get.side_effect = (keystone_exceptions + .NotFound) + client_keystone.return_value = self._client + client_plugin = client.KeystoneClientPlugin( + context=mock.MagicMock() + ) + + ex = self.assertRaises(exception.EntityNotFound, + client_plugin.get_region_id, + self.sample_name) + msg = ('The KeystoneRegion (%(name)s) could not be found.' % + {'name': self.sample_name}) + self.assertEqual(msg, six.text_type(ex))