diff options
author | Dan Smith <danms@us.ibm.com> | 2013-05-22 17:30:36 -0700 |
---|---|---|
committer | Dan Smith <danms@us.ibm.com> | 2013-06-07 11:43:37 -0700 |
commit | 6fcf4133b49cfefa77151937dec4db097a85c349 (patch) | |
tree | 07436a73fb8f9ff6b154ffde59aaba52c3078072 /nova/api | |
parent | e86d5b063bbb741d411bf4b50b5cc8a9829960db (diff) | |
download | nova-6fcf4133b49cfefa77151937dec4db097a85c349.tar.gz nova-6fcf4133b49cfefa77151937dec4db097a85c349.tar.xz nova-6fcf4133b49cfefa77151937dec4db097a85c349.zip |
Use Instance Objects for Start/Stop
This patch makes the start and stop operations use the Instance
object instead of passing SQLA-derived dicts over RPC. Until
something more sophisticated is needed (and developed), it also
adds a nova.object.register_all() function which just triggers
imports of all the object models so that they are registered.
When adding a new object type, that register function should be
updated appropriately.
Related to bp/unified-object-model
Change-Id: I3c8d9cba07d34097a279502062906de802d19d1f
Diffstat (limited to 'nova/api')
-rw-r--r-- | nova/api/ec2/cloud.py | 30 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/server_start_stop.py | 5 |
2 files changed, 28 insertions, 7 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index da0a52caa..af50868d7 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -43,6 +43,7 @@ from nova import exception from nova.image import s3 from nova import network from nova.network.security_group import quantum_driver +from nova.objects import instance as instance_obj from nova.openstack.common import log as logging from nova.openstack.common import timeutils from nova import quota @@ -206,6 +207,16 @@ def _format_mappings(properties, result): result['blockDeviceMapping'] = mappings +def db_to_inst_obj(context, db_instance): + # NOTE(danms): This is a temporary helper method for converting + # Instance DB objects to NovaObjects without needing to re-query. + inst_obj = instance_obj.Instance._from_db_object( + instance_obj.Instance(), db_instance, + expected_attrs=['system_metadata', 'metadata']) + inst_obj._context = context + return inst_obj + + class CloudController(object): """CloudController provides the critical dispatch between inbound API calls through the endpoint and messages @@ -1340,13 +1351,18 @@ class CloudController(object): block_device_mapping=kwargs.get('block_device_mapping', {})) return self._format_run_instances(context, resv_id) - def _ec2_ids_to_instances(self, context, instance_id): + def _ec2_ids_to_instances(self, context, instance_id, objects=False): """Get all instances first, to prevent partial executions.""" instances = [] + extra = ['system_metadata', 'metadata'] for ec2_id in instance_id: validate_ec2_id(ec2_id) instance_uuid = ec2utils.ec2_inst_id_to_uuid(context, ec2_id) - instance = self.compute_api.get(context, instance_uuid) + if objects: + instance = instance_obj.Instance.get_by_uuid( + context, instance_uuid, expected_attrs=extra) + else: + instance = self.compute_api.get(context, instance_uuid) instances.append(instance) return instances @@ -1372,7 +1388,7 @@ class CloudController(object): 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) + instances = self._ec2_ids_to_instances(context, instance_id, True) LOG.debug(_("Going to stop instances")) for instance in instances: self.compute_api.stop(context, instance) @@ -1381,7 +1397,7 @@ class CloudController(object): 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) + instances = self._ec2_ids_to_instances(context, instance_id, True) LOG.debug(_("Going to start instances")) for instance in instances: self.compute_api.start(context, instance) @@ -1635,7 +1651,8 @@ class CloudController(object): if vm_state == vm_states.ACTIVE: restart_instance = True - self.compute_api.stop(context, instance) + inst_obj = db_to_inst_obj(context, instance) + self.compute_api.stop(context, inst_obj) # wait instance for really stopped start_time = time.time() @@ -1677,7 +1694,8 @@ class CloudController(object): ec2_id = ec2utils.glance_id_to_ec2_id(context, new_image['id']) if restart_instance: - self.compute_api.start(context, instance) + inst_obj = db_to_inst_obj(context, instance) + self.compute_api.start(context, inst_obj) return {'imageId': ec2_id} diff --git a/nova/api/openstack/compute/contrib/server_start_stop.py b/nova/api/openstack/compute/contrib/server_start_stop.py index c4d0d5c9e..2803cd04b 100644 --- a/nova/api/openstack/compute/contrib/server_start_stop.py +++ b/nova/api/openstack/compute/contrib/server_start_stop.py @@ -20,6 +20,7 @@ from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova import compute from nova import exception +from nova.objects import instance as instance_obj from nova.openstack.common import log as logging @@ -33,7 +34,9 @@ class ServerStartStopActionController(wsgi.Controller): def _get_instance(self, context, instance_uuid): try: - return self.compute_api.get(context, instance_uuid) + attrs = ['system_metadata', 'metadata'] + return instance_obj.Instance.get_by_uuid(context, instance_uuid, + expected_attrs=attrs) except exception.NotFound: msg = _("Instance not found") raise webob.exc.HTTPNotFound(explanation=msg) |