ec2: Move ec2utils functions to their callers

All of these functions only have a single caller now, so move them to
their calling site. Two functions are renamed to better fit their
purpose but there is otherwise no change in functionality.

Change-Id: I48237f306fb901dec9ec805704c891fd3cbb12e3
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2019-05-31 16:34:30 +01:00
parent 35ca7a6b4c
commit 4e1eb8a2b5
7 changed files with 131 additions and 154 deletions

View File

@ -1,129 +0,0 @@
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
from oslo_utils import uuidutils
from nova import cache_utils
from nova import exception
from nova.network import model as network_model
from nova import objects
# NOTE(vish): cache mapping for one week
_CACHE_TIME = 7 * 24 * 60 * 60
_CACHE = None
def memoize(func):
@functools.wraps(func)
def memoizer(context, reqid):
global _CACHE
if not _CACHE:
_CACHE = cache_utils.get_client(expiration_time=_CACHE_TIME)
key = "%s:%s" % (func.__name__, reqid)
key = str(key)
value = _CACHE.get(key)
if value is None:
value = func(context, reqid)
_CACHE.set(key, value)
return value
return memoizer
def image_type(image_type):
"""Converts to a three letter image type.
aki, kernel => aki
ari, ramdisk => ari
anything else => ami
"""
if image_type == 'kernel':
return 'aki'
if image_type == 'ramdisk':
return 'ari'
if image_type not in ['aki', 'ari']:
return 'ami'
return image_type
@memoize
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:
s3imap = objects.S3ImageMapping(context, uuid=glance_id)
s3imap.create()
return s3imap.id
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
template = image_type + '-%08x'
return id_to_ec2_id(image_id, template=template)
def get_ip_info_for_instance_from_nw_info(nw_info):
if not isinstance(nw_info, network_model.NetworkInfo):
nw_info = network_model.NetworkInfo.hydrate(nw_info)
ip_info = {}
fixed_ips = nw_info.fixed_ips()
ip_info['fixed_ips'] = [ip['address'] for ip in fixed_ips
if ip['version'] == 4]
ip_info['fixed_ip6s'] = [ip['address'] for ip in fixed_ips
if ip['version'] == 6]
ip_info['floating_ips'] = [ip['address'] for ip in nw_info.floating_ips()]
return ip_info
def id_to_ec2_id(instance_id, template='i-%08x'):
"""Convert an instance ID (int) to an ec2 ID (i-[base 16 number])."""
return template % int(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):
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)
@memoize
def get_int_id_from_instance_uuid(context, instance_uuid):
if instance_uuid is None:
return
try:
imap = objects.EC2InstanceMapping.get_by_uuid(context, instance_uuid)
return imap.id
except exception.NotFound:
imap = objects.EC2InstanceMapping(context)
imap.uuid = instance_uuid
imap.create()
return imap.id

View File

@ -25,7 +25,6 @@ from oslo_serialization import jsonutils
from oslo_utils import timeutils
import six
from nova.api.ec2 import ec2utils
from nova.api.metadata import password
from nova.api.metadata import vendordata_dynamic
from nova.api.metadata import vendordata_json
@ -174,8 +173,7 @@ class InstanceMetadata(object):
else:
self.network_metadata = network_metadata
self.ip_info = \
ec2utils.get_ip_info_for_instance_from_nw_info(network_info)
self.ip_info = netutils.get_ec2_ip_info(network_info)
self.network_config = None
cfg = netutils.get_injected_network_template(network_info)

View File

@ -12,13 +12,108 @@
# License for the specific language governing permissions and limitations
# under the License.
from nova.api.ec2 import ec2utils
import functools
from oslo_utils import uuidutils
from nova import cache_utils
from nova.db import api as db
from nova import exception
from nova.objects import base
from nova.objects import fields
# NOTE(vish): cache mapping for one week
_CACHE_TIME = 7 * 24 * 60 * 60
_CACHE = None
def memoize(func):
@functools.wraps(func)
def memoizer(context, reqid):
global _CACHE
if not _CACHE:
_CACHE = cache_utils.get_client(expiration_time=_CACHE_TIME)
key = "%s:%s" % (func.__name__, reqid)
key = str(key)
value = _CACHE.get(key)
if value is None:
value = func(context, reqid)
_CACHE.set(key, value)
return value
return memoizer
def id_to_ec2_id(instance_id, template='i-%08x'):
"""Convert an instance ID (int) to an ec2 ID (i-[base 16 number])."""
return template % int(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):
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)
@memoize
def get_int_id_from_instance_uuid(context, instance_uuid):
if instance_uuid is None:
return
try:
imap = EC2InstanceMapping.get_by_uuid(context, instance_uuid)
return imap.id
except exception.NotFound:
imap = EC2InstanceMapping(context)
imap.uuid = instance_uuid
imap.create()
return imap.id
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
template = image_type + '-%08x'
return id_to_ec2_id(image_id, template=template)
@memoize
def glance_id_to_id(context, glance_id):
"""Convert a glance id to an internal (db) id."""
if not glance_id:
return
try:
return S3ImageMapping.get_by_uuid(context, glance_id).id
except exception.NotFound:
s3imap = S3ImageMapping(context, uuid=glance_id)
s3imap.create()
return s3imap.id
def glance_type_to_ec2_type(image_type):
"""Converts to a three letter image type.
aki, kernel => aki
ari, ramdisk => ari
anything else => ami
"""
if image_type == 'kernel':
return 'aki'
if image_type == 'ramdisk':
return 'ari'
if image_type not in ['aki', 'ari']:
return 'ami'
return image_type
@base.NovaObjectRegistry.register
class EC2InstanceMapping(base.NovaPersistentObject, base.NovaObject):
# Version 1.0: Initial version
@ -119,17 +214,14 @@ class EC2Ids(base.NovaObject):
def _get_ec2_ids(context, instance):
ec2_ids = {}
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)
ec2_ids['instance_id'] = id_to_ec2_inst_id(context, instance.uuid)
ec2_ids['ami_id'] = glance_id_to_ec2_id(context, instance.image_ref)
for image_type in ['kernel', 'ramdisk']:
image_id = getattr(instance, '%s_id' % image_type)
ec2_id = None
if image_id is not None:
ec2_image_type = ec2utils.image_type(image_type)
ec2_id = ec2utils.glance_id_to_ec2_id(context, image_id,
ec2_image_type)
ec2_image_type = glance_type_to_ec2_type(image_type)
ec2_id = glance_id_to_ec2_id(context, image_id, ec2_image_type)
ec2_ids['%s_id' % image_type] = ec2_id
return ec2_ids

View File

@ -76,15 +76,15 @@ class MetadataTest(test.TestCase):
'nova.network.api.API.get_fixed_ip_by_address',
fake_get_fixed_ip_by_address))
def fake_get_ip_info_for_instance_from_nw_info(nw_info):
def fake_get_ec2_ip_info(nw_info):
return {'fixed_ips': ['127.0.0.2'],
'fixed_ip6s': [],
'floating_ips': []}
self.useFixture(
fixtures.MonkeyPatch(
'nova.api.ec2.ec2utils.get_ip_info_for_instance_from_nw_info',
fake_get_ip_info_for_instance_from_nw_info))
'nova.virt.netutils.get_ec2_ip_info',
fake_get_ec2_ip_info))
def test_lookup_metadata_root_url(self):
res = requests.request('GET', self.md_url, timeout=5)

View File

@ -113,9 +113,9 @@ class TestRemoteS3ImageMapping(test_objects._RemoteTest, _TestS3ImageMapping):
class _TestEC2Ids(object):
@mock.patch('nova.api.ec2.ec2utils.image_type')
@mock.patch('nova.api.ec2.ec2utils.glance_id_to_ec2_id')
@mock.patch('nova.api.ec2.ec2utils.id_to_ec2_inst_id')
@mock.patch('nova.objects.ec2.glance_type_to_ec2_type')
@mock.patch('nova.objects.ec2.glance_id_to_ec2_id')
@mock.patch('nova.objects.ec2.id_to_ec2_inst_id')
def test_get_by_instance(self, mock_inst, mock_glance, mock_type):
mock_inst.return_value = 'fake-ec2-inst-id'
mock_glance.side_effect = ['fake-ec2-ami-id',
@ -135,8 +135,8 @@ class _TestEC2Ids(object):
self.assertEqual('fake-ec2-kernel-id', result.kernel_id)
self.assertEqual('fake-ec2-ramdisk-id', result.ramdisk_id)
@mock.patch('nova.api.ec2.ec2utils.glance_id_to_ec2_id')
@mock.patch('nova.api.ec2.ec2utils.id_to_ec2_inst_id')
@mock.patch('nova.objects.ec2.glance_id_to_ec2_id')
@mock.patch('nova.objects.ec2.id_to_ec2_inst_id')
def test_get_by_instance_no_image_ref(self, mock_inst, mock_glance):
mock_inst.return_value = 'fake-ec2-inst-id'
mock_glance.return_value = None
@ -150,9 +150,9 @@ class _TestEC2Ids(object):
self.assertIsNone(result.kernel_id)
self.assertIsNone(result.ramdisk_id)
@mock.patch('nova.api.ec2.ec2utils.image_type')
@mock.patch('nova.api.ec2.ec2utils.glance_id_to_ec2_id')
@mock.patch('nova.api.ec2.ec2utils.id_to_ec2_inst_id')
@mock.patch('nova.objects.ec2.glance_type_to_ec2_type')
@mock.patch('nova.objects.ec2.glance_id_to_ec2_id')
@mock.patch('nova.objects.ec2.id_to_ec2_inst_id')
def test_get_by_instance_no_kernel_id(self, mock_inst, mock_glance,
mock_type):
mock_inst.return_value = 'fake-ec2-inst-id'
@ -170,9 +170,9 @@ class _TestEC2Ids(object):
self.assertIsNone(result.kernel_id)
self.assertEqual('fake-ec2-ramdisk-id', result.ramdisk_id)
@mock.patch('nova.api.ec2.ec2utils.image_type')
@mock.patch('nova.api.ec2.ec2utils.glance_id_to_ec2_id')
@mock.patch('nova.api.ec2.ec2utils.id_to_ec2_inst_id')
@mock.patch('nova.objects.ec2.glance_type_to_ec2_type')
@mock.patch('nova.objects.ec2.glance_id_to_ec2_id')
@mock.patch('nova.objects.ec2.id_to_ec2_inst_id')
def test_get_by_instance_no_ramdisk_id(self, mock_inst, mock_glance,
mock_type):
mock_inst.return_value = 'fake-ec2-inst-id'

View File

@ -224,6 +224,22 @@ def get_network_metadata(network_info):
}
def get_ec2_ip_info(network_info):
if not isinstance(network_info, model.NetworkInfo):
network_info = model.NetworkInfo.hydrate(network_info)
ip_info = {}
fixed_ips = network_info.fixed_ips()
ip_info['fixed_ips'] = [
ip['address'] for ip in fixed_ips if ip['version'] == 4]
ip_info['fixed_ip6s'] = [
ip['address'] for ip in fixed_ips if ip['version'] == 6]
ip_info['floating_ips'] = [
ip['address'] for ip in network_info.floating_ips()]
return ip_info
def _get_eth_link(vif, ifc_num):
"""Get a VIF or physical NIC representation.