Optimize checks to set image properties in metadata

This cleans up the redundant checks we have around
instance.image and ensure we only check if image info
exists once and set the appropriate values and default
to None if missing. Also updated the tests to use the
right default of None instead of empty string and
None assert check for image and new test to validate
image from conductor with no links.

Change-Id: Idb4aa85ae132ec60175ada8a6e304c7eb27f485b
This commit is contained in:
Pradeep Kilambi 2014-04-08 09:36:08 -07:00
parent b27a819d96
commit 4112741276
2 changed files with 22 additions and 8 deletions

View File

@ -45,16 +45,21 @@ def _get_metadata_from_object(instance):
'host': instance.hostId, 'host': instance.hostId,
'flavor': instance.flavor, 'flavor': instance.flavor,
'status': instance.status.lower(), 'status': instance.status.lower(),
# Image properties
'image': instance.image,
'image_ref': (instance.image['id'] if instance.image else None),
} }
# Images that come through the conductor API in the nova notifier # Image properties
# plugin will not have links. if instance.image:
if instance.image and instance.image.get('links'): metadata['image'] = instance.image
metadata['image_ref_url'] = instance.image['links'][0]['href'] metadata['image_ref'] = instance.image['id']
# Images that come through the conductor API in the nova notifier
# plugin will not have links.
if instance.image.get('links'):
metadata['image_ref_url'] = instance.image['links'][0]['href']
else:
metadata['image_ref_url'] = None
else: else:
metadata['image'] = None
metadata['image_ref'] = None
metadata['image_ref_url'] = None metadata['image_ref_url'] = None
for name in INSTANCE_PROPERTIES: for name in INSTANCE_PROPERTIES:

View File

@ -99,8 +99,17 @@ class TestLocationMetadata(test.BaseTestCase):
self.assertEqual(1, len(user_metadata)) self.assertEqual(1, len(user_metadata))
def test_metadata_empty_image(self): def test_metadata_empty_image(self):
self.INSTANCE_PROPERTIES['image'] = '' self.INSTANCE_PROPERTIES['image'] = None
self.instance = FauxInstance(**self.INSTANCE_PROPERTIES) self.instance = FauxInstance(**self.INSTANCE_PROPERTIES)
md = util._get_metadata_from_object(self.instance) md = util._get_metadata_from_object(self.instance)
self.assertIsNone(md['image'])
self.assertIsNone(md['image_ref']) self.assertIsNone(md['image_ref'])
self.assertIsNone(md['image_ref_url']) self.assertIsNone(md['image_ref_url'])
def test_metadata_image_through_conductor(self):
# There should be no links here, should default to None
self.INSTANCE_PROPERTIES['image'] = {'id': 1}
self.instance = FauxInstance(**self.INSTANCE_PROPERTIES)
md = util._get_metadata_from_object(self.instance)
self.assertEqual(1, md['image_ref'])
self.assertIsNone(md['image_ref_url'])