From ae8f7b6b039c5858984eae0143dbe148b0f5be29 Mon Sep 17 00:00:00 2001 From: Shuangtai Tian Date: Tue, 28 Jan 2014 19:19:24 +0800 Subject: [PATCH] Adds flavor_id in the nova_notifier The instance_type_id is the primary key stored in Nova's instance_types table. The flavorid is the public id provided at the creation of the flavor. When we create an instance using the flavor m1.tiny, we can see instance info contains below keys: "instance_type_name" :"m1.tiny" "instance_type_id": 2, Actually, the "m1.tiny" flavorid should be a string '1'. Considering changing the id's tpye maybe cause confusion with the already saved old data, add flavor_id to the flavor dictionary. The instance_ref has all the flavor infomation, not need to get flavor info from DB again. Change-Id: I5bd19b2bc2f874d31fd4609306c24e0f51517281 Closes-Bug: #1273638 --- ceilometer/compute/nova_notifier.py | 8 ++++++-- nova_tests/test_notifier.py | 25 ++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ceilometer/compute/nova_notifier.py b/ceilometer/compute/nova_notifier.py index bb1946b3ec..f4f0482040 100644 --- a/ceilometer/compute/nova_notifier.py +++ b/ceilometer/compute/nova_notifier.py @@ -30,6 +30,7 @@ import ceilometer # noqa for name in ['openstack', 'openstack.common', 'openstack.common.log']: sys.modules['ceilometer.' + name] = sys.modules['nova.' + name] +from nova.compute import flavors from nova import conductor from nova import utils @@ -106,8 +107,10 @@ class Instance(object): setattr(self, k, utils.metadata_to_dict(v)) else: setattr(self, k, v) - self.flavor_name = conductor_api.instance_type_get( - context, self.instance_type_id).get('name', 'UNKNOWN') + + instance_type = flavors.extract_flavor(info) + self.flavor_name = instance_type.get('name', 'UNKNOWN') + self.instance_flavor_id = instance_type.get('flavorid', '') LOG.debug(_('INFO %r'), info) @property @@ -118,6 +121,7 @@ class Instance(object): def flavor(self): return { 'id': self.instance_type_id, + 'flavor_id': self.instance_flavor_id, 'name': self.flavor_name, 'vcpus': self.vcpus, 'ram': self.memory_mb, diff --git a/nova_tests/test_notifier.py b/nova_tests/test_notifier.py index 7348f99da6..6adec5408e 100644 --- a/nova_tests/test_notifier.py +++ b/nova_tests/test_notifier.py @@ -180,8 +180,7 @@ class TestNovaNotifier(test.BaseTestCase): lambda context, instance: {}) self.stubs.Set(db, 'instance_update_and_get_original', lambda *args, **kwargs: (self.instance, self.instance)) - self.stubs.Set(flavors, 'extract_flavor', - lambda ref: {}) + self.stubs.Set(flavors, 'extract_flavor', self.fake_extract_flavor) # Set up to capture the notification messages generated by the # plugin and to invoke our notifier plugin. @@ -217,9 +216,6 @@ class TestNovaNotifier(test.BaseTestCase): mock.patch.object(nova_notifier.conductor_api, 'instance_get_by_uuid', self.fake_instance_ref_get), - mock.patch.object(nova_notifier.conductor_api, - 'instance_type_get', - self.fake_instance_type_get), mock.patch('nova.openstack.common.notifier.rpc_notifier.notify', self.notify) ): @@ -242,8 +238,18 @@ class TestNovaNotifier(test.BaseTestCase): def fake_instance_ref_get(self, context, id_): return self.instance_data - def fake_instance_type_get(self, context, id_): - return {'id': '1', 'name': 'm1.tiny'} + @staticmethod + def fake_extract_flavor(instance_ref): + return {'ephemeral_gb': 0, + 'flavorid': '1', + 'id': 2, + 'memory_mb': 512, + 'name': 'm1.tiny', + 'root_gb': 1, + 'rxtx_factor': 1.0, + 'swap': 0, + 'vcpu_weight': None, + 'vcpus': 1} @staticmethod def do_nothing(*args, **kwargs): @@ -267,6 +273,11 @@ class TestNovaNotifier(test.BaseTestCase): for i, (gatherer, inst) in enumerate(self.Pollster.instances): self.assertEqual((i, gatherer), (i, self.gatherer)) + def test_instance_flavor(self): + inst = nova_notifier.Instance(context, self.instance) + self.assertEqual(inst.flavor['name'], 'm1.tiny') + self.assertEqual(inst.flavor['flavor_id'], '1') + def test_samples(self): # Ensure that the outgoing notification looks like what we expect for message in self.notifications: