diff options
-rw-r--r-- | snaps/openstack/create_image.py | 8 | ||||
-rw-r--r-- | snaps/openstack/create_router.py | 3 | ||||
-rw-r--r-- | snaps/openstack/tests/create_image_tests.py | 40 | ||||
-rw-r--r-- | snaps/openstack/utils/glance_utils.py | 30 | ||||
-rw-r--r-- | snaps/openstack/utils/nova_utils.py | 2 | ||||
-rw-r--r-- | snaps/openstack/utils/tests/glance_utils_tests.py | 10 |
6 files changed, 52 insertions, 41 deletions
diff --git a/snaps/openstack/create_image.py b/snaps/openstack/create_image.py index 9ed813c..a4c9357 100644 --- a/snaps/openstack/create_image.py +++ b/snaps/openstack/create_image.py @@ -56,8 +56,8 @@ class OpenStackImage: :return: The OpenStack Image object """ self.__glance = glance_utils.glance_client(self.__os_creds) - self.__image = glance_utils.get_image(self.__glance, - self.image_settings.name) + self.__image = glance_utils.get_image( + self.__glance, image_settings=self.image_settings) if self.__image: logger.info('Found image with name - ' + self.image_settings.name) return self.__image @@ -72,7 +72,7 @@ class OpenStackImage: if self.image_settings.kernel_image_settings: self.__kernel_image = glance_utils.get_image( self.__glance, - self.image_settings.kernel_image_settings.name) + image_settings=self.image_settings.kernel_image_settings) if not self.__kernel_image and not cleanup: logger.info( @@ -85,7 +85,7 @@ class OpenStackImage: if self.image_settings.ramdisk_image_settings: self.__ramdisk_image = glance_utils.get_image( self.__glance, - self.image_settings.ramdisk_image_settings.name) + image_settings=self.image_settings.ramdisk_image_settings) if not self.__ramdisk_image and not cleanup: logger.info( diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 335be2c..209f9d2 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -253,8 +253,7 @@ class RouterSettings: if self.admin_state_up is not None: out['admin_state_up'] = self.admin_state_up if self.external_gateway: - ext_net = neutron_utils.get_network(neutron, self.external_gateway, - project_id) + ext_net = neutron_utils.get_network(neutron, self.external_gateway) if ext_net: ext_gw['network_id'] = ext_net.id out['external_gateway_info'] = ext_gw diff --git a/snaps/openstack/tests/create_image_tests.py b/snaps/openstack/tests/create_image_tests.py index 5fb39dc..7a6db86 100644 --- a/snaps/openstack/tests/create_image_tests.py +++ b/snaps/openstack/tests/create_image_tests.py @@ -316,8 +316,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): created_image = self.image_creator.create() self.assertIsNotNone(created_image) - retrieved_image = glance_utils.get_image(self.glance, - self.image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=self.image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(created_image.size, retrieved_image.size) self.assertEqual(get_image_size(self.image_settings), @@ -337,8 +337,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): created_image = self.image_creator.create() self.assertIsNotNone(created_image) - retrieved_image = glance_utils.get_image(self.glance, - self.image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=self.image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creator.get_image().size, retrieved_image.size) @@ -370,7 +370,7 @@ class CreateImageSuccessTests(OSIntegrationTestCase): self.assertEqual(self.image_name, created_image.name) retrieved_image = glance_utils.get_image( - self.glance, file_image_settings.name) + self.glance, image_settings=file_image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creator.get_image().size, retrieved_image.size) @@ -394,8 +394,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): created_image = self.image_creator.create() self.assertIsNotNone(created_image) - retrieved_image = glance_utils.get_image(self.glance, - self.image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=self.image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creator.get_image().size, retrieved_image.size) @@ -406,7 +406,7 @@ class CreateImageSuccessTests(OSIntegrationTestCase): glance_utils.delete_image(self.glance, created_image) self.assertIsNone(glance_utils.get_image( - self.glance, self.image_creator.image_settings.name)) + self.glance, image_settings=self.image_creator.image_settings)) # Must not throw an exception when attempting to cleanup non-existent # image @@ -422,8 +422,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): self.image_settings) image1 = self.image_creator.create() - retrieved_image = glance_utils.get_image(self.glance, - self.image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=self.image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creator.get_image().size, retrieved_image.size) @@ -449,8 +449,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): self.image_settings) image1 = self.image_creator.create() - retrieved_image = glance_utils.get_image(self.glance, - self.image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=self.image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creator.get_image().size, retrieved_image.size) @@ -616,20 +616,22 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): image_creator.create() main_image = glance_utils.get_image(self.glance, - image_settings.name) + image_settings=image_settings) self.assertIsNotNone(main_image) self.assertIsNotNone(image_creator.get_image()) self.assertEqual(image_creator.get_image().id, main_image.id) kernel_image = glance_utils.get_image( - self.glance, image_settings.kernel_image_settings.name) + self.glance, + image_settings=image_settings.kernel_image_settings) self.assertIsNotNone(kernel_image) self.assertIsNotNone(image_creator.get_kernel_image()) self.assertEqual(kernel_image.id, image_creator.get_kernel_image().id) ramdisk_image = glance_utils.get_image( - self.glance, image_settings.ramdisk_image_settings.name) + self.glance, + image_settings=image_settings.ramdisk_image_settings) self.assertIsNotNone(ramdisk_image) self.assertIsNotNone(image_creator.get_ramdisk_image()) self.assertEqual(ramdisk_image.id, @@ -732,8 +734,8 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): self.assertIsNotNone(created_image) self.assertEqual(self.image_name, created_image.name) - retrieved_image = glance_utils.get_image(self.glance, - file_image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=file_image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creators[-1].get_image().size, retrieved_image.size) @@ -805,8 +807,8 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): self.assertIsNotNone(created_image) self.assertEqual(self.image_name, created_image.name) - retrieved_image = glance_utils.get_image(self.glance, - os_image_settings.name) + retrieved_image = glance_utils.get_image( + self.glance, image_settings=os_image_settings) self.assertIsNotNone(retrieved_image) self.assertEqual(self.image_creators[-1].get_image().size, diff --git a/snaps/openstack/utils/glance_utils.py b/snaps/openstack/utils/glance_utils.py index ad9c5e5..2606e32 100644 --- a/snaps/openstack/utils/glance_utils.py +++ b/snaps/openstack/utils/glance_utils.py @@ -44,26 +44,34 @@ def glance_client(os_creds): region_name=os_creds.region_name) -def get_image(glance, image_name=None): +def get_image(glance, image_name=None, image_settings=None): """ Returns an OpenStack image object for a given name :param glance: the Glance client :param image_name: the image name to lookup + :param image_settings: the image settings used for lookups :return: the image object or None """ - images = glance.images.list() + img_filter = dict() + if image_settings: + if image_settings.exists: + img_filter = {'name': image_settings.name} + else: + img_filter = {'name': image_settings.name, + 'disk_format': image_settings.format} + elif image_name: + img_filter = {'name': image_name} + + images = glance.images.list(**{'filters': img_filter}) for image in images: if glance.version == VERSION_1: - if image.name == image_name: - image = glance.images.get(image.id) - return Image(name=image.name, image_id=image.id, - size=image.size, properties=image.properties) + image = glance.images.get(image.id) + return Image(name=image.name, image_id=image.id, + size=image.size, properties=image.properties) elif glance.version == VERSION_2: - if image['name'] == image_name: - return Image( - name=image['name'], image_id=image['id'], - size=image['size'], properties=image.get('properties')) - return None + return Image( + name=image['name'], image_id=image['id'], + size=image['size'], properties=image.get('properties')) def get_image_by_id(glance, image_id): diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index b148bc5..a0c028a 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -83,7 +83,7 @@ def create_server(nova, neutron, glance, instance_settings, image_settings, raise NovaException( 'Flavor not found with name - %s', instance_settings.flavor) - image = glance_utils.get_image(glance, image_settings.name) + image = glance_utils.get_image(glance, image_settings=image_settings) if image: args = {'name': instance_settings.name, 'flavor': flavor, diff --git a/snaps/openstack/utils/tests/glance_utils_tests.py b/snaps/openstack/utils/tests/glance_utils_tests.py index 85b59ab..e61b795 100644 --- a/snaps/openstack/utils/tests/glance_utils_tests.py +++ b/snaps/openstack/utils/tests/glance_utils_tests.py @@ -40,7 +40,7 @@ class GlanceSmokeTests(OSComponentTestCase): Tests to ensure that the proper credentials can connect. """ glance = glance_utils.glance_client(self.os_creds) - image = glance_utils.get_image(glance, 'foo') + image = glance_utils.get_image(glance, image_name='foo') self.assertIsNone(image) def test_glance_connect_fail(self): @@ -53,7 +53,7 @@ class GlanceSmokeTests(OSComponentTestCase): glance = glance_utils.glance_client(OSCreds( username='user', password='pass', auth_url='url', project_name='project')) - glance_utils.get_image(glance, 'foo') + glance_utils.get_image(glance, image_name='foo') class GlanceUtilsTests(OSComponentTestCase): @@ -104,7 +104,8 @@ class GlanceUtilsTests(OSComponentTestCase): self.assertEqual(self.image_name, self.image.name) - image = glance_utils.get_image(self.glance, os_image_settings.name) + image = glance_utils.get_image(self.glance, + image_settings=os_image_settings) self.assertIsNotNone(image) validation_utils.objects_equivalent(self.image, image) @@ -132,6 +133,7 @@ class GlanceUtilsTests(OSComponentTestCase): self.assertIsNotNone(self.image) self.assertEqual(self.image_name, self.image.name) - image = glance_utils.get_image(self.glance, file_image_settings.name) + image = glance_utils.get_image( + self.glance, image_settings=file_image_settings) self.assertIsNotNone(image) validation_utils.objects_equivalent(self.image, image) |