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 <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane
2019-05-31 16:01:06 +01:00
parent 7c2303ea22
commit 35ca7a6b4c
2 changed files with 5 additions and 9 deletions

View File

@@ -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)

View File

@@ -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']: