From 2ea2f0d7058d55a01307a951770efd0df2ba2484 Mon Sep 17 00:00:00 2001 From: jichenjc Date: Tue, 26 Sep 2017 19:57:31 +0800 Subject: [PATCH] remove glance usage inside compute refactor compute code to remove glance direct usage, 1) use image_api.get instead of get glance code then show the image detail information 2) according to TODO, create a new api in image.api and use it Change-Id: I0c8dee5d0d18adeaa83183da81c85f378ae5f8fd --- nova/compute/manager.py | 10 ++++------ nova/image/api.py | 10 ++++++++++ nova/tests/unit/compute/test_compute_mgr.py | 5 ++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 6d76561aaea4..42cdcc5b6285 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -74,7 +74,6 @@ from nova import exception_wrapper from nova import hooks from nova.i18n import _ from nova import image -from nova.image import glance from nova import manager from nova import network from nova.network import base_api as base_net_api @@ -2974,8 +2973,8 @@ class ComputeManager(manager.Manager): # This instance.exists message should contain the original # image_ref, not the new one. Since the DB has been updated # to point to the new one... we have to override it. - # TODO(jaypipes): Move generate_image_url() into the nova.image.api - orig_image_ref_url = glance.generate_image_url(orig_image_ref, context) + orig_image_ref_url = self.image_api.generate_image_url(orig_image_ref, + context) extra_usage_info = {'image_ref_url': orig_image_ref_url} compute_utils.notify_usage_exists( self.notifier, context, instance, @@ -3307,10 +3306,9 @@ class ComputeManager(manager.Manager): msg = 'Instance disappeared during snapshot' LOG.debug(msg, instance=instance) try: - image_service = glance.get_default_image_service() - image = image_service.show(context, image_id) + image = self.image_api.get(context, image_id) if image['status'] != 'active': - image_service.delete(context, image_id) + self.image_api.delete(context, image_id) except Exception: LOG.warning("Error while trying to clean up image %s", image_id, instance=instance) diff --git a/nova/image/api.py b/nova/image/api.py index e2d6192c59bb..43b9a2c20e03 100644 --- a/nova/image/api.py +++ b/nova/image/api.py @@ -55,6 +55,16 @@ class API(object): # the context alive... return glance.get_default_image_service() + @staticmethod + def generate_image_url(image_ref, context): + """Generate an image URL from an image_ref. + + :param image_ref: The image ref to generate URL + :param context: The `nova.context.Context` object for the request + """ + return "%s/images/%s" % (next(glance.get_api_servers(context)), + image_ref) + def get_all(self, context, **kwargs): """Retrieves all information records about all disk images available to show to the requesting user. If the requesting user is an admin, diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py index 189f74d6b3f1..a60e6654e933 100644 --- a/nova/tests/unit/compute/test_compute_mgr.py +++ b/nova/tests/unit/compute/test_compute_mgr.py @@ -6574,13 +6574,16 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase): migration.status = 'running' migration.id = 0 + # TODO(jichenjc): will be removed when remove glance.generate_image_url @mock.patch('nova.image.glance.generate_image_url', return_value='fake-url') + @mock.patch('nova.image.api.API.generate_image_url', + return_value='fake-url') @mock.patch.object(objects.Migration, 'get_by_id', return_value=migration) @mock.patch.object(self.compute.driver, 'live_migration_force_complete') - def _do_test(force_complete, get_by_id, gen_img_url): + def _do_test(force_complete, get_by_id, gen_img_url, glance_image_url): self.compute.live_migration_force_complete( self.context, self.instance, migration.id)