Use flavors.get when finding with id

flavors.get is more efficient than flavors.find when looking for
falvors with id.

Change-Id: I82a12e5287766d88a815c15a68515b3918a63988
This commit is contained in:
Rabi Mishra 2015-12-23 09:51:05 +05:30
parent 2f8a9993f6
commit 32ddfbfa48
7 changed files with 37 additions and 21 deletions

View File

@ -231,7 +231,7 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
:returns: the id of :flavor:
"""
try:
return self.client().flavors.find(id=flavor).id
return self.client().flavors.get(flavor).id
except exceptions.NotFound:
return self.client().flavors.find(name=flavor).id

View File

@ -99,7 +99,7 @@ class TroveClientPlugin(client_plugin.ClientPlugin):
:returns: the id of :flavor:
"""
try:
return self.client().flavors.find(id=flavor).id
return self.client().flavors.get(flavor).id
except exceptions.NotFound:
return self.client().flavors.find(name=flavor).id

View File

@ -80,12 +80,15 @@ class NovaClientPluginTests(NovaClientPluginTestCase):
my_flavor.name = flav_name
my_flavor.id = flav_id
self.nova_client.flavors.get.side_effect = [
my_flavor,
nova_exceptions.NotFound(''),
nova_exceptions.NotFound(''),
]
self.nova_client.flavors.find.side_effect = [
my_flavor,
nova_exceptions.NotFound(''),
my_flavor,
nova_exceptions.NotFound(''),
nova_exceptions.NotFound('')]
]
self.assertEqual(flav_id,
self.nova_plugin.find_flavor_by_name_or_id(flav_id))
self.assertEqual(flav_id,
@ -93,7 +96,8 @@ class NovaClientPluginTests(NovaClientPluginTestCase):
self.assertRaises(nova_exceptions.ClientException,
self.nova_plugin.find_flavor_by_name_or_id,
'noflavor')
self.assertEqual(5, self.nova_client.flavors.find.call_count)
self.assertEqual(3, self.nova_client.flavors.get.call_count)
self.assertEqual(2, self.nova_client.flavors.find.call_count)
def test_get_host(self):
"""Tests the get_host function."""
@ -475,19 +479,20 @@ class FlavorConstraintTest(common.HeatTestCase):
flavor = collections.namedtuple("Flavor", ["id", "name"])
flavor.id = "1234"
flavor.name = "foo"
client.flavors.find.side_effect = [flavor,
nova_exceptions.NotFound(''),
flavor,
nova_exceptions.NotFound(''),
nova_exceptions.NotFound('')]
client.flavors.get.side_effect = [flavor,
nova_exceptions.NotFound(''),
nova_exceptions.NotFound('')]
client.flavors.find.side_effect = [flavor,
nova_exceptions.NotFound('')]
constraint = nova.FlavorConstraint()
ctx = utils.dummy_context()
self.assertTrue(constraint.validate("1234", ctx))
self.assertTrue(constraint.validate("foo", ctx))
self.assertFalse(constraint.validate("bar", ctx))
self.assertEqual(1, nova.NovaClientPlugin._create.call_count)
self.assertEqual(5, client.flavors.find.call_count)
self.assertEqual(3, client.flavors.get.call_count)
self.assertEqual(2, client.flavors.find.call_count)
class NetworkConstraintTest(common.HeatTestCase):

View File

@ -64,7 +64,8 @@ class FakeHTTPClient(base_client.HTTPClient):
args = urlparse.parse_qsl(urlparse.urlparse(url)[4])
kwargs.update(args)
munged_url = url.rsplit('?', 1)[0]
munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_')
munged_url = munged_url.strip('/').replace('/', '_').replace(
'.', '_').replace(' ', '_')
munged_url = munged_url.replace('-', '_')
callback = "%s_%s" % (method.lower(), munged_url)
@ -310,9 +311,6 @@ class FakeHTTPClient(base_client.HTTPClient):
# Flavors
#
def get_flavors_detail(self, **kw):
return self.get_flavors()
def get_flavors(self, **kw):
return (200, {'flavors': [
{'id': 1, 'name': '256 MB Server', 'ram': 256, 'disk': 10,
@ -323,6 +321,15 @@ class FakeHTTPClient(base_client.HTTPClient):
'OS-FLV-EXT-DATA:ephemeral': 30}
]})
def get_flavors_256_MB_Server(self, **kw):
raise fake_exception()
def get_flavors_m1_small(self, **kw):
raise fake_exception()
def get_flavors_m1_large(self, **kw):
raise fake_exception()
def get_flavors_1(self, **kw):
return (200, {'flavor': {
'id': 1, 'name': '256 MB Server', 'ram': 256, 'disk': 10,

View File

@ -86,7 +86,7 @@ class TroveClusterTest(common.HeatTestCase):
mock_client = self.patcher_client.start()
self.client = mock_client.return_value
self.troveclient = mock.Mock()
self.troveclient.flavors.find.return_value = FakeFlavor(1, 'm1.heat')
self.troveclient.flavors.get.return_value = FakeFlavor(1, 'm1.heat')
self.troveclient.datastore_versions.list.return_value = [FakeVersion()]
self.patchobject(trove.TroveClientPlugin, 'client',
return_value=self.troveclient)
@ -158,10 +158,12 @@ class TroveClusterTest(common.HeatTestCase):
self.assertEqual(error_msg, six.text_type(ex))
def test_validate_invalid_flavor(self):
self.troveclient.flavors.find.side_effect = [troveexc.NotFound('')]
self.troveclient.flavors.get.side_effect = troveexc.NotFound()
self.troveclient.flavors.find.side_effect = troveexc.NotFound()
self.rsrc_defn['Properties']['instances'][0]['flavor'] = 'm1.small'
tc = cluster.TroveCluster('cluster', self.rsrc_defn, self.stack)
ex = self.assertRaises(exception.StackValidationFailed, tc.validate)
error_msg = ("Property error: "
"resources.cluster.properties.instances[0].flavor: ")
"resources.cluster.properties.instances[0].flavor: "
"Error validating value 'm1.small': Not Found (HTTP 404)")
self.assertEqual(error_msg, six.text_type(ex))

View File

@ -234,8 +234,10 @@ class OSDBInstanceTest(common.HeatTestCase):
trove.TroveClientPlugin._create().AndReturn(self.fc)
self.fc.flavors = self.m.CreateMockAnything()
self.m.StubOutWithMock(self.fc.flavors, "get")
self.fc.flavors.get(u'1GB').AndRaise(troveexc.NotFound())
self.m.StubOutWithMock(self.fc.flavors, "find")
self.fc.flavors.find(id=u'1GB').AndReturn(FakeFlavor(1, '1GB'))
self.fc.flavors.find(name=u'1GB').AndReturn(FakeFlavor(1, '1GB'))
self.fc.instances = self.m.CreateMockAnything()
self.m.StubOutWithMock(self.fc.instances, 'create')
users = [{"name": "testuser", "password": "pass", "host": "%",

View File

@ -1563,7 +1563,7 @@ class StackTest(common.HeatTestCase):
flavor = collections.namedtuple("Flavor", ["id", "name"])
flavor.id = "1234"
flavor.name = "dummy"
fc.flavors.find(id='1234').AndReturn(flavor)
fc.flavors.get('1234').AndReturn(flavor)
self.m.ReplayAll()