Merge "Adds get_region_id() in keystone client plugin"

This commit is contained in:
Jenkins 2015-11-17 15:44:08 +00:00 committed by Gerrit Code Review
commit 67cf007f9c
2 changed files with 54 additions and 0 deletions

View File

@ -123,6 +123,14 @@ class KeystoneClientPlugin(client_plugin.ClientPlugin):
raise exception.EntityNotFound(entity='KeystoneUser', name=user) 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): class KeystoneRoleConstraint(constraints.BaseCustomConstraint):

View File

@ -625,3 +625,49 @@ class KeystoneClientPluginUserTest(common.HeatTestCase):
self.sample_name) self.sample_name)
self._client.client.users.list.assert_called_once_with( self._client.client.users.list.assert_called_once_with(
name=self.sample_name) 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))