Use flavors.find interface to find flavors

This patch uses 'flavors.find' to look for flavors rather than
finding them from 'flavors.list' output.

Change-Id: I48b058a6b01d941d2e0f362900d0c85bc7c71fbc
This commit is contained in:
Rabi Mishra 2015-12-01 11:23:24 +05:30
parent 4b53e073a2
commit c3a8cc4197
15 changed files with 84 additions and 78 deletions

View File

@ -306,7 +306,7 @@ class Group(resource.Resource):
bool(user_data is not None and len(user_data.strip())))
image_id = self.client_plugin('glance').get_image_id(
server_args[self.LAUNCH_CONFIG_ARGS_SERVER_IMAGE_REF])
flavor_id = self.client_plugin('nova').get_flavor_id(
flavor_id = self.client_plugin('nova').find_flavor_by_name_or_id(
server_args[self.LAUNCH_CONFIG_ARGS_SERVER_FLAVOR_REF])
return dict(

View File

@ -212,7 +212,8 @@ class ScalingGroupTest(common.HeatTestCase):
# mock nova and glance client methods to satisfy contraints
mock_im = self.patchobject(glance.GlanceClientPlugin, 'get_image_id')
mock_im.return_value = 'image-ref'
mock_fl = self.patchobject(nova.NovaClientPlugin, 'get_flavor_id')
mock_fl = self.patchobject(nova.NovaClientPlugin,
'find_flavor_by_name_or_id')
mock_fl.return_value = 'flavor-ref'
def _setup_test_stack(self):

View File

@ -224,27 +224,16 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
resource_status=server.status,
result=_('%s is not active') % res_name)
def get_flavor_id(self, flavor):
"""Get the id for the specified flavor name.
If the specified value is flavor id, just return it.
def find_flavor_by_name_or_id(self, flavor):
"""Find the specified flavor by name or id.
:param flavor: the name of the flavor to find
:returns: the id of :flavor:
:raises: exception.EntityNotFound
"""
flavor_id = None
flavor_list = self.client().flavors.list()
for o in flavor_list:
if o.name == flavor:
flavor_id = o.id
break
if o.id == flavor:
flavor_id = o.id
break
if flavor_id is None:
raise exception.EntityNotFound(entity='Flavor', name=flavor)
return flavor_id
try:
return self.client().flavors.find(id=flavor).id
except exceptions.NotFound:
return self.client().flavors.find(name=flavor).id
def get_host(self, host_name):
"""Get the host id specified by name.
@ -705,7 +694,9 @@ class KeypairConstraint(NovaBaseConstraint):
class FlavorConstraint(NovaBaseConstraint):
resource_getter_name = 'get_flavor_id'
expected_exceptions = (exceptions.NotFound,)
resource_getter_name = 'find_flavor_by_name_or_id'
class NetworkConstraint(NovaBaseConstraint):

View File

@ -92,32 +92,21 @@ class TroveClientPlugin(client_plugin.ClientPlugin):
def is_conflict(self, ex):
return isinstance(ex, exceptions.Conflict)
def get_flavor_id(self, flavor):
"""Get the ID for the specified flavor name.
If the specified value is flavor id, just return it.
def find_flavor_by_name_or_id(self, flavor):
"""Find the specified flavor by name or id.
:param flavor: the name of the flavor to find
:returns: the id of :flavor:
:raises: exception.EntityNotFound
"""
flavor_id = None
flavor_list = self.client().flavors.list()
for o in flavor_list:
if o.name == flavor:
flavor_id = o.id
break
if o.id == flavor:
flavor_id = o.id
break
if flavor_id is None:
raise exception.EntityNotFound(entity='Flavor', name=flavor)
return flavor_id
try:
return self.client().flavors.find(id=flavor).id
except exceptions.NotFound:
return self.client().flavors.find(name=flavor).id
class FlavorConstraint(constraints.BaseCustomConstraint):
expected_exceptions = (exception.EntityNotFound,)
expected_exceptions = (exceptions.NotFound,)
resource_client_name = CLIENT_NAME
resource_getter_name = 'get_flavor_id'
resource_getter_name = 'find_flavor_by_name_or_id'

View File

@ -512,7 +512,7 @@ class Instance(resource.Resource, sh.SchedulerHintsMixin):
image_id = self.client_plugin('glance').get_image_id(image_name)
flavor_id = self.client_plugin().get_flavor_id(flavor)
flavor_id = self.client_plugin().find_flavor_by_name_or_id(flavor)
scheduler_hints = {}
if self.properties[self.NOVA_SCHEDULER_HINTS]:
@ -627,7 +627,7 @@ class Instance(resource.Resource, sh.SchedulerHintsMixin):
def _update_instance_type(self, prop_diff):
flavor = prop_diff[self.INSTANCE_TYPE]
flavor_id = self.client_plugin().get_flavor_id(flavor)
flavor_id = self.client_plugin().find_flavor_by_name_or_id(flavor)
handler_args = {'args': (flavor_id,)}
checker_args = {'args': (flavor_id, flavor)}

View File

@ -732,7 +732,7 @@ class Server(stack_user.StackUser, sh.SchedulerHintsMixin,
if image:
image = self.client_plugin('glance').get_image_id(image)
flavor_id = self.client_plugin().get_flavor_id(flavor)
flavor_id = self.client_plugin().find_flavor_by_name_or_id(flavor)
instance_meta = self.properties[self.METADATA]
if instance_meta is not None:
@ -969,7 +969,7 @@ class Server(stack_user.StackUser, sh.SchedulerHintsMixin,
def _update_flavor(self, prop_diff):
flavor = prop_diff[self.FLAVOR]
flavor_id = self.client_plugin().get_flavor_id(flavor)
flavor_id = self.client_plugin().find_flavor_by_name_or_id(flavor)
handler_args = {'args': (flavor_id,)}
checker_args = {'args': (flavor_id, flavor)}

View File

@ -253,7 +253,7 @@ class SaharaNodeGroupTemplate(resource.Resource):
'name': self._ngt_name(),
'plugin_name': self.properties[self.PLUGIN_NAME],
'hadoop_version': self.properties[self.HADOOP_VERSION],
'flavor_id': self.client_plugin("nova").get_flavor_id(
'flavor_id': self.client_plugin("nova").find_flavor_by_name_or_id(
self.properties[self.FLAVOR]),
'description': self.properties[self.DESCRIPTION],
'volumes_per_node': self.properties[self.VOLUMES_PER_NODE],

View File

@ -145,7 +145,7 @@ class TroveCluster(resource.Resource):
instances = []
for instance in self.properties[self.INSTANCES]:
instances.append({
'flavorRef': self.client_plugin().get_flavor_id(
'flavorRef': self.client_plugin().find_flavor_by_name_or_id(
instance[self.FLAVOR]),
'volume': {'size': instance[self.VOLUME_SIZE]}
})

View File

@ -298,7 +298,7 @@ class OSDBInstance(resource.Resource):
def handle_create(self):
"""Create cloud database instance."""
self.flavor = self.client_plugin().get_flavor_id(
self.flavor = self.client_plugin().find_flavor_by_name_or_id(
self.properties[self.FLAVOR])
self.volume = {'size': self.properties[self.SIZE]}
self.databases = self.properties[self.DATABASES]

View File

@ -72,22 +72,28 @@ class NovaClientPluginTests(NovaClientPluginTestCase):
observed = self.nova_plugin.get_ip(my_image, 'public', 6)
self.assertEqual(expected, observed)
def test_get_flavor_id(self):
def test_find_flavor_by_name_or_id(self):
"""Tests the get_flavor_id function."""
flav_id = str(uuid.uuid4())
flav_name = 'X-Large'
my_flavor = mock.MagicMock()
my_flavor.name = flav_name
my_flavor.id = flav_id
self.nova_client.flavors.list.return_value = [my_flavor]
self.assertEqual(flav_id, self.nova_plugin.get_flavor_id(flav_name))
self.assertEqual(flav_id, self.nova_plugin.get_flavor_id(flav_id))
ex = self.assertRaises(exception.EntityNotFound,
self.nova_plugin.get_flavor_id, 'noflavor')
self.assertEqual('Flavor', ex.kwargs.get('entity'))
self.assertEqual(3, self.nova_client.flavors.list.call_count)
self.assertEqual([(), (), ()],
self.nova_client.flavors.list.call_args_list)
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,
self.nova_plugin.find_flavor_by_name_or_id(flav_name))
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)
def test_get_host(self):
"""Tests the get_host function."""
@ -469,17 +475,19 @@ class FlavorConstraintTest(common.HeatTestCase):
flavor = collections.namedtuple("Flavor", ["id", "name"])
flavor.id = "1234"
flavor.name = "foo"
client.flavors.list.return_value = [flavor]
client.flavors.find.side_effect = [flavor,
nova_exceptions.NotFound(''),
flavor,
nova_exceptions.NotFound(''),
nova_exceptions.NotFound('')]
constraint = nova.FlavorConstraint()
ctx = utils.dummy_context()
self.assertFalse(constraint.validate("bar", ctx))
self.assertTrue(constraint.validate("foo", ctx))
self.assertTrue(constraint.validate("1234", ctx))
nova.NovaClientPlugin._create.assert_called_once_with()
self.assertEqual(3, client.flavors.list.call_count)
self.assertEqual([(), (), ()],
client.flavors.list.call_args_list)
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)
class NetworkConstraintTest(common.HeatTestCase):

View File

@ -311,6 +311,9 @@ class FakeHTTPClient(base_client.HTTPClient):
#
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,
'OS-FLV-EXT-DATA:ephemeral': 10},
@ -320,6 +323,21 @@ class FakeHTTPClient(base_client.HTTPClient):
'OS-FLV-EXT-DATA:ephemeral': 30}
]})
def get_flavors_1(self, **kw):
return (200, {'flavor': {
'id': 1, 'name': '256 MB Server', 'ram': 256, 'disk': 10,
'OS-FLV-EXT-DATA:ephemeral': 10}})
def get_flavors_2(self, **kw):
return (200, {'flavor': {
'id': 2, 'name': 'm1.small', 'ram': 512, 'disk': 20,
'OS-FLV-EXT-DATA:ephemeral': 20}})
def get_flavors_3(self, **kw):
return (200, {'flavor': {
'id': 3, 'name': 'm1.large', 'ram': 512, 'disk': 20,
'OS-FLV-EXT-DATA:ephemeral': 30}})
#
# Floating ips
#

View File

@ -111,7 +111,7 @@ class SaharaNodeGroupTemplateTest(common.HeatTestCase):
self.stub_FlavorConstraint_validate()
self.stub_SaharaPluginConstraint()
self.stub_VolumeTypeConstraint_validate()
self.patchobject(nova.NovaClientPlugin, 'get_flavor_id'
self.patchobject(nova.NovaClientPlugin, 'find_flavor_by_name_or_id'
).return_value = 'someflavorid'
self.patchobject(neutron.NeutronClientPlugin, '_create')
self.patchobject(neutron.NeutronClientPlugin, 'find_neutron_resource'

View File

@ -85,11 +85,11 @@ class TroveClusterTest(common.HeatTestCase):
'client')
mock_client = self.patcher_client.start()
self.client = mock_client.return_value
client = mock.Mock()
client.flavors.list.return_value = [FakeFlavor(1, 'm1.heat')]
client.datastore_versions.list.return_value = [FakeVersion()]
self.troveclient = mock.Mock()
self.troveclient.flavors.find.return_value = FakeFlavor(1, 'm1.heat')
self.troveclient.datastore_versions.list.return_value = [FakeVersion()]
self.patchobject(trove.TroveClientPlugin, 'client',
return_value=client)
return_value=self.troveclient)
def tearDown(self):
super(TroveClusterTest, self).tearDown()
@ -158,11 +158,10 @@ 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.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: "
"Error validating value 'm1.small': "
"The Flavor (m1.small) could not be found.")
"resources.cluster.properties.instances[0].flavor: ")
self.assertEqual(error_msg, six.text_type(ex))

View File

@ -120,8 +120,9 @@ class OSDBInstanceTest(common.HeatTestCase):
def _stubout_common_create(self):
trove.TroveClientPlugin._create().AndReturn(self.fc)
self.fc.flavors = self.m.CreateMockAnything()
self.m.StubOutWithMock(trove.TroveClientPlugin, 'get_flavor_id')
trove.TroveClientPlugin.get_flavor_id('1GB').AndReturn(1)
self.m.StubOutWithMock(trove.TroveClientPlugin,
'find_flavor_by_name_or_id')
trove.TroveClientPlugin.find_flavor_by_name_or_id('1GB').AndReturn(1)
self.fc.instances = self.m.CreateMockAnything()
self.m.StubOutWithMock(self.fc.instances, 'create')
self.m.StubOutWithMock(self.fc.instances, 'get')
@ -233,9 +234,8 @@ class OSDBInstanceTest(common.HeatTestCase):
trove.TroveClientPlugin._create().AndReturn(self.fc)
self.fc.flavors = self.m.CreateMockAnything()
self.m.StubOutWithMock(self.fc.flavors, "list")
self.fc.flavors.list().AndReturn([FakeFlavor(1, '1GB'),
FakeFlavor(2, '2GB')])
self.m.StubOutWithMock(self.fc.flavors, "find")
self.fc.flavors.find(id=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,11 +1563,11 @@ class StackTest(common.HeatTestCase):
flavor = collections.namedtuple("Flavor", ["id", "name"])
flavor.id = "1234"
flavor.name = "dummy"
fc.flavors.list().AndReturn([flavor])
fc.flavors.find(id='1234').AndReturn(flavor)
self.m.ReplayAll()
test_env = environment.Environment({'flavor': 'dummy'})
test_env = environment.Environment({'flavor': '1234'})
self.stack = stack.Stack(self.ctx, 'stack_with_custom_constraint',
template.Template(tmpl, env=test_env))