Skip to content

Commit

Permalink
Fix api direct use of instance module objects
Browse files Browse the repository at this point in the history
This switches the nova/api and nova/tests/api code to use
nova.objects.Instance* vs nova.objects.instance.Instance*.

Partial-Blueprint: object-subclassing

Change-Id: I146f1d082fa37e1a5c09aa8c5363746daffbc219
  • Loading branch information
comstud committed Jun 3, 2014
1 parent 3c1085f commit a9f54de
Show file tree
Hide file tree
Showing 42 changed files with 153 additions and 144 deletions.
31 changes: 12 additions & 19 deletions nova/api/ec2/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
from nova.objects import base as obj_base
from nova.objects import ec2 as ec2_obj
from nova.objects import flavor as flavor_obj
from nova.objects import instance as instance_obj
from nova.objects import security_group as sec_group_obj
from nova.objects import service as service_obj
from nova.openstack.common.gettextutils import _
Expand Down Expand Up @@ -801,8 +800,7 @@ def _format_volume(self, context, volume):
if volume.get('instance_uuid', None):
instance_uuid = volume['instance_uuid']
# Make sure instance exists
instance_obj.Instance.get_by_uuid(context.elevated(),
instance_uuid)
objects.Instance.get_by_uuid(context.elevated(), instance_uuid)

instance_ec2_id = ec2utils.id_to_ec2_inst_id(instance_uuid)

Expand Down Expand Up @@ -900,7 +898,7 @@ def _get_instance_from_volume(self, context, volume):
if volume.get('instance_uuid'):
try:
inst_uuid = volume['instance_uuid']
return instance_obj.Instance.get_by_uuid(context, inst_uuid)
return objects.Instance.get_by_uuid(context, inst_uuid)
except exception.InstanceNotFound:
pass
raise exception.VolumeUnattached(volume_id=volume['id'])
Expand Down Expand Up @@ -1362,15 +1360,15 @@ def _add_client_token(self, context, client_token, instance_ids):
if client_token:
for ec2_id in instance_ids:
instance_uuid = ec2utils.ec2_inst_id_to_uuid(context, ec2_id)
instance = instance_obj.Instance.get_by_uuid(context,
instance = objects.Instance.get_by_uuid(context,
instance_uuid, expected_attrs=['system_metadata'])
instance.system_metadata.update(
{'EC2_client_token': client_token})
instance.save()

def _get_client_token(self, context, instance_uuid):
"""Get client token for a given instance."""
instance = instance_obj.Instance.get_by_uuid(context,
instance = objects.Instance.get_by_uuid(context,
instance_uuid, expected_attrs=['system_metadata'])
return instance.system_metadata.get('EC2_client_token')

Expand All @@ -1379,7 +1377,7 @@ def _remove_client_token(self, context, instance_ids):

for ec2_id in instance_ids:
instance_uuid = ec2utils.ec2_inst_id_to_uuid(context, ec2_id)
instance = instance_obj.Instance.get_by_uuid(context,
instance = objects.Instance.get_by_uuid(context,
instance_uuid, expected_attrs=['system_metadata'])
instance.system_metadata.pop('EC2_client_token', None)
instance.save()
Expand All @@ -1393,33 +1391,29 @@ def _resv_id_from_token(self, context, client_token):

for sys_meta in sys_metas:
if sys_meta and sys_meta.get('value') == client_token:
instance = instance_obj.Instance.get_by_uuid(
instance = objects.Instance.get_by_uuid(
context, sys_meta['instance_id'], expected_attrs=None)
resv_id = instance.get('reservation_id')
break
return resv_id

def _ec2_ids_to_instances(self, context, instance_id, objects=False):
def _ec2_ids_to_instances(self, context, instance_id):
"""Get all instances first, to prevent partial executions."""
instances = []
extra = ['system_metadata', 'metadata', 'info_cache']
for ec2_id in instance_id:
validate_ec2_id(ec2_id)
instance_uuid = ec2utils.ec2_inst_id_to_uuid(context, ec2_id)
if objects:
instance = instance_obj.Instance.get_by_uuid(
instance = objects.Instance.get_by_uuid(
context, instance_uuid, expected_attrs=extra)
else:
instance = self.compute_api.get(context, instance_uuid)
instances.append(instance)
return instances

def terminate_instances(self, context, instance_id, **kwargs):
"""Terminate each instance in instance_id, which is a list of ec2 ids.
instance_id is a kwarg so its name cannot be modified.
"""
previous_states = self._ec2_ids_to_instances(context, instance_id,
objects=True)
previous_states = self._ec2_ids_to_instances(context, instance_id)
self._remove_client_token(context, instance_id)
LOG.debug("Going to start terminating instances")
for instance in previous_states:
Expand All @@ -1430,8 +1424,7 @@ def terminate_instances(self, context, instance_id, **kwargs):

def reboot_instances(self, context, instance_id, **kwargs):
"""instance_id is a list of instance ids."""
instances = self._ec2_ids_to_instances(context, instance_id,
objects=True)
instances = self._ec2_ids_to_instances(context, instance_id)
LOG.audit(_("Reboot instance %r"), instance_id, context=context)
for instance in instances:
self.compute_api.reboot(context, instance, 'HARD')
Expand All @@ -1441,7 +1434,7 @@ def stop_instances(self, context, instance_id, **kwargs):
"""Stop each instances in instance_id.
Here instance_id is a list of instance ids
"""
instances = self._ec2_ids_to_instances(context, instance_id, True)
instances = self._ec2_ids_to_instances(context, instance_id)
LOG.debug("Going to stop instances")
for instance in instances:
extensions.check_compute_policy(context, 'stop', instance)
Expand All @@ -1452,7 +1445,7 @@ def start_instances(self, context, instance_id, **kwargs):
"""Start each instances in instance_id.
Here instance_id is a list of instance ids
"""
instances = self._ec2_ids_to_instances(context, instance_id, True)
instances = self._ec2_ids_to_instances(context, instance_id)
LOG.debug("Going to start instances")
for instance in instances:
extensions.check_compute_policy(context, 'start', instance)
Expand Down
4 changes: 2 additions & 2 deletions nova/api/ec2/ec2utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from nova import db
from nova import exception
from nova.network import model as network_model
from nova.objects import base as obj_base
from nova.objects import ec2 as ec2_obj
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import memorycache
Expand Down Expand Up @@ -160,7 +160,7 @@ def get_ip_info_for_instance_from_nw_info(nw_info):
def get_ip_info_for_instance(context, instance):
"""Return a dictionary of IP information for an instance."""

if isinstance(instance, instance_obj.Instance):
if isinstance(instance, obj_base.NovaObject):
nw_info = instance.info_cache.network_info
else:
# FIXME(comstud): Temporary as we transition to objects.
Expand Down
9 changes: 4 additions & 5 deletions nova/api/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from nova import network
from nova import objects
from nova.objects import base as obj_base
from nova.objects import instance as instance_obj
from nova.objects import security_group as secgroup_obj
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
Expand Down Expand Up @@ -116,12 +115,12 @@ def __init__(self, instance, address=None, content=None, extra_md=None,
ctxt = context.get_admin_context()

# NOTE(danms): This should be removed after bp:compute-manager-objects
if not isinstance(instance, instance_obj.Instance):
if not isinstance(instance, obj_base.NovaObject):
expected = ['metadata', 'system_metadata']
if 'info_cache' in instance:
expected.append('info_cache')
instance = instance_obj.Instance._from_db_object(
ctxt, instance_obj.Instance(), instance,
instance = objects.Instance._from_db_object(
ctxt, objects.Instance(), instance,
expected_attrs=expected)

# The default value of mimeType is set to MIME_TYPE_TEXT_PLAIN
Expand Down Expand Up @@ -519,7 +518,7 @@ def get_metadata_by_address(conductor_api, address):
def get_metadata_by_instance_id(conductor_api, instance_id, address,
ctxt=None):
ctxt = ctxt or context.get_admin_context()
instance = instance_obj.Instance.get_by_uuid(ctxt, instance_id)
instance = objects.Instance.get_by_uuid(ctxt, instance_id)
return InstanceMetadata(instance, address)


Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/server_external_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from nova.api.openstack import xmlutil
from nova import compute
from nova import exception
from nova import objects
from nova.objects import external_event as external_event_obj
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging

Expand Down Expand Up @@ -104,7 +104,7 @@ def create(self, req, body):
events.append(_event)
if event.instance_uuid not in instances:
try:
instance = instance_obj.Instance.get_by_uuid(
instance = objects.Instance.get_by_uuid(
context, event.instance_uuid)
instances[event.instance_uuid] = instance
except exception.InstanceNotFound:
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/server_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from nova.api.openstack import wsgi
from nova.api.openstack import xmlutil
import nova.exception
from nova.objects import instance as instance_obj
from nova import objects
from nova.objects import instance_group as instance_group_obj
from nova.openstack.common.gettextutils import _
from nova import utils
Expand Down Expand Up @@ -157,7 +157,7 @@ def _format_server_group(self, context, group):
if group.members:
# Display the instances that are not deleted.
filters = {'uuid': group.members, 'deleted': False}
instances = instance_obj.InstanceList.get_by_filters(
instances = objects.InstanceList.get_by_filters(
context, filters=filters)
members = [instance.uuid for instance in instances]
server_group['members'] = members
Expand Down
6 changes: 3 additions & 3 deletions nova/api/openstack/compute/contrib/server_start_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from nova.api.openstack import wsgi
from nova import compute
from nova import exception
from nova.objects import instance as instance_obj
from nova import objects
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging

Expand All @@ -34,8 +34,8 @@ def __init__(self, *args, **kwargs):
def _get_instance(self, context, instance_uuid):
try:
attrs = ['system_metadata', 'metadata']
return instance_obj.Instance.get_by_uuid(context, instance_uuid,
expected_attrs=attrs)
return objects.Instance.get_by_uuid(context, instance_uuid,
expected_attrs=attrs)
except exception.NotFound:
msg = _("Instance not found")
raise webob.exc.HTTPNotFound(explanation=msg)
Expand Down
3 changes: 2 additions & 1 deletion nova/api/openstack/compute/contrib/simple_tenant_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from nova.api.openstack import wsgi
from nova.api.openstack import xmlutil
from nova import exception
from nova import objects
from nova.objects import flavor as flavor_obj
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
Expand Down Expand Up @@ -132,7 +133,7 @@ def _get_flavor(self, context, instance, flavors_cache):
def _tenant_usages_for_period(self, context, period_start,
period_stop, tenant_id=None, detailed=True):

instances = instance_obj.InstanceList.get_active_by_window_joined(
instances = objects.InstanceList.get_active_by_window_joined(
context, period_start, period_stop, tenant_id,
expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS)
rval = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from nova.api.openstack import wsgi
from nova import compute
from nova import exception
from nova import objects
from nova.objects import external_event as external_event_obj
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging

Expand Down Expand Up @@ -77,7 +77,7 @@ def create(self, req, body):
events.append(_event)
if event.instance_uuid not in instances:
try:
instance = instance_obj.Instance.get_by_uuid(
instance = objects.Instance.get_by_uuid(
context, event.instance_uuid)
instances[event.instance_uuid] = instance
except exception.InstanceNotFound:
Expand Down
7 changes: 3 additions & 4 deletions nova/api/openstack/compute/plugins/v3/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from nova import exception
from nova.image import glance
from nova import objects
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import strutils
Expand Down Expand Up @@ -284,7 +283,7 @@ def _get_servers(self, req, is_detail):
log_msg = _("Flavor '%s' could not be found ")
LOG.debug(log_msg, search_opts['flavor'])
# TODO(mriedem): Move to ObjectListBase.__init__ for empty lists.
instance_list = instance_obj.InstanceList(objects=[])
instance_list = objects.InstanceList(objects=[])

if is_detail:
instance_list.fill_faults()
Expand Down Expand Up @@ -958,8 +957,8 @@ def _get_server_search_options(self):
def _get_instance(self, context, instance_uuid):
try:
attrs = ['system_metadata', 'metadata']
return instance_obj.Instance.get_by_uuid(context, instance_uuid,
expected_attrs=attrs)
return objects.Instance.get_by_uuid(context, instance_uuid,
expected_attrs=attrs)
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())

Expand Down
3 changes: 1 addition & 2 deletions nova/api/openstack/compute/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from nova.compute import flavors
from nova import exception
from nova import objects
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import strutils
Expand Down Expand Up @@ -607,7 +606,7 @@ def _get_servers(self, req, is_detail):
log_msg = _("Flavor '%s' could not be found ")
LOG.debug(log_msg, search_opts['flavor'])
# TODO(mriedem): Move to ObjectListBase.__init__ for empty lists.
instance_list = instance_obj.InstanceList(objects=[])
instance_list = objects.InstanceList(objects=[])

if is_detail:
instance_list.fill_faults()
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/views/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from nova.api.openstack.compute.views import flavors as views_flavors
from nova.api.openstack.compute.views import images as views_images
from nova.compute import flavors
from nova.objects import instance as instance_obj
from nova.objects import base as obj_base
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import timeutils
Expand Down Expand Up @@ -148,7 +148,7 @@ def _list_view(self, func, request, servers, coll_name):
def _get_metadata(instance):
# FIXME(danms): Transitional support for objects
metadata = instance.get('metadata')
if isinstance(instance, instance_obj.Instance):
if isinstance(instance, obj_base.NovaObject):
return metadata or {}
else:
return utils.instance_meta(instance)
Expand Down
5 changes: 2 additions & 3 deletions nova/tests/api/ec2/test_cinder_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from nova import context
from nova import db
from nova import exception
from nova.objects import instance as instance_obj
from nova import objects
from nova import test
from nova.tests import cast_as_call
from nova.tests import fake_network
Expand Down Expand Up @@ -898,8 +898,7 @@ def test_stop_with_attached_volume(self):
vol = self.volume_api.get(self.context, vol2_uuid)
self._assert_volume_detached(vol)

inst_obj = instance_obj.Instance.get_by_uuid(self.context,
instance_uuid)
inst_obj = objects.Instance.get_by_uuid(self.context, instance_uuid)
self.cloud.compute_api.attach_volume(self.context,
inst_obj,
volume_id=vol2_uuid,
Expand Down
11 changes: 6 additions & 5 deletions nova/tests/api/ec2/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
from nova.network import base_api as base_network_api
from nova.network import model
from nova.network import neutronv2
from nova.objects import instance as instance_obj
from nova import objects
from nova.objects import base as obj_base
from nova.objects import instance_info_cache as instance_info_cache_obj
from nova.objects import security_group as security_group_obj
from nova.openstack.common import log as logging
Expand Down Expand Up @@ -123,7 +124,7 @@ def get_instances_with_cached_ips(orig_func, get_floating,
else:
info_cache = {'network_info': get_fake_cache(get_floating)}

if isinstance(instances, (list, instance_obj.InstanceList)):
if isinstance(instances, (list, obj_base.ObjectListBase)):
for instance in instances:
instance['info_cache'] = info_cache
else:
Expand Down Expand Up @@ -1381,15 +1382,15 @@ def test_describe_instances_dnsName_set(self):
def test_describe_instances_booting_from_a_volume(self):
sys_meta = flavors.save_flavor_info(
{}, flavors.get_flavor(1))
inst = instance_obj.Instance()
inst = objects.Instance(self.context)
inst.reservation_id = 'a'
inst.image_ref = ''
inst.root_device_name = '/dev/sdh'
inst.instance_type_id = 1
inst.vm_state = vm_states.ACTIVE
inst.host = 'host1'
inst.system_metadata = sys_meta
inst.create(self.context)
inst.create()
result = self.cloud.describe_instances(self.context)
result = result['reservationSet'][0]
instance = result['instancesSet'][0]
Expand Down Expand Up @@ -2667,7 +2668,7 @@ def fake_get(ctxt, instance_id, want_objects=False):
security_group_obj.SecurityGroup(name='fake0'))
secgroups.objects.append(
security_group_obj.SecurityGroup(name='fake1'))
instance = instance_obj.Instance()
instance = objects.Instance(ctxt)
instance.id = 0
instance.uuid = 'e5fe5518-0288-4fa3-b0c4-c79764101b85'
instance.root_device_name = '/dev/sdh'
Expand Down
Loading

0 comments on commit a9f54de

Please sign in to comment.