From 35ca7a6b4c4079dcbb95407d951d826d26124218 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 31 May 2019 16:01:06 +0100 Subject: [PATCH] ec2: Pre-move cleanup of utils Since most of these functions only have a single caller now, we're going to move them to their call site. Prepare for that by fixing some issues: - The 'image_ec2_id' function was unnecessarily split out and are now folded back in - The 'id_to_ec2_inst_id' function now takes a 'context' argument rather than having to retrieve an admin context from scratch Change-Id: I48250eb573134e5bd0c183edaf07a8d685573c68 Signed-off-by: Stephen Finucane --- nova/api/ec2/ec2utils.py | 11 +++-------- nova/objects/ec2.py | 3 ++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py index 7646688d4dce..28f1f6e09766 100644 --- a/nova/api/ec2/ec2utils.py +++ b/nova/api/ec2/ec2utils.py @@ -19,7 +19,6 @@ import functools from oslo_utils import uuidutils from nova import cache_utils -from nova import context from nova import exception from nova.network import model as network_model from nova import objects @@ -67,6 +66,7 @@ def glance_id_to_id(context, glance_id): """Convert a glance id to an internal (db) id.""" if not glance_id: return + try: return objects.S3ImageMapping.get_by_uuid(context, glance_id).id except exception.NotFound: @@ -79,11 +79,7 @@ def glance_id_to_ec2_id(context, glance_id, image_type='ami'): image_id = glance_id_to_id(context, glance_id) if image_id is None: return - return image_ec2_id(image_id, image_type=image_type) - -def image_ec2_id(image_id, image_type='ami'): - """Returns image ec2_id using id and three letter type.""" template = image_type + '-%08x' return id_to_ec2_id(image_id, template=template) @@ -108,13 +104,12 @@ def id_to_ec2_id(instance_id, template='i-%08x'): return template % int(instance_id) -def id_to_ec2_inst_id(instance_id): +def id_to_ec2_inst_id(context, instance_id): """Get or create an ec2 instance ID (i-[base 16 number]) from uuid.""" if instance_id is None: return None elif uuidutils.is_uuid_like(instance_id): - ctxt = context.get_admin_context() - int_id = get_int_id_from_instance_uuid(ctxt, instance_id) + int_id = get_int_id_from_instance_uuid(context, instance_id) return id_to_ec2_id(int_id) else: return id_to_ec2_id(instance_id) diff --git a/nova/objects/ec2.py b/nova/objects/ec2.py index 6f0c4451759b..9761b29fd870 100644 --- a/nova/objects/ec2.py +++ b/nova/objects/ec2.py @@ -119,7 +119,8 @@ class EC2Ids(base.NovaObject): def _get_ec2_ids(context, instance): ec2_ids = {} - ec2_ids['instance_id'] = ec2utils.id_to_ec2_inst_id(instance.uuid) + ec2_ids['instance_id'] = ec2utils.id_to_ec2_inst_id(context, + instance.uuid) ec2_ids['ami_id'] = ec2utils.glance_id_to_ec2_id(context, instance.image_ref) for image_type in ['kernel', 'ramdisk']: