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:
parent
b27a819d96
commit
4112741276
@ -45,16 +45,21 @@ def _get_metadata_from_object(instance):
|
||||
'host': instance.hostId,
|
||||
'flavor': instance.flavor,
|
||||
'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
|
||||
# plugin will not have links.
|
||||
if instance.image and instance.image.get('links'):
|
||||
metadata['image_ref_url'] = instance.image['links'][0]['href']
|
||||
# Image properties
|
||||
if instance.image:
|
||||
metadata['image'] = instance.image
|
||||
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:
|
||||
metadata['image'] = None
|
||||
metadata['image_ref'] = None
|
||||
metadata['image_ref_url'] = None
|
||||
|
||||
for name in INSTANCE_PROPERTIES:
|
||||
|
@ -99,8 +99,17 @@ class TestLocationMetadata(test.BaseTestCase):
|
||||
self.assertEqual(1, len(user_metadata))
|
||||
|
||||
def test_metadata_empty_image(self):
|
||||
self.INSTANCE_PROPERTIES['image'] = ''
|
||||
self.INSTANCE_PROPERTIES['image'] = None
|
||||
self.instance = FauxInstance(**self.INSTANCE_PROPERTIES)
|
||||
md = util._get_metadata_from_object(self.instance)
|
||||
self.assertIsNone(md['image'])
|
||||
self.assertIsNone(md['image_ref'])
|
||||
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'])
|
||||
|
Loading…
x
Reference in New Issue
Block a user