diff options
| author | Jenkins <jenkins@review.openstack.org> | 2013-06-12 19:42:33 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2013-06-12 19:42:33 +0000 |
| commit | 06855306d532c318508baf8a39760c77ae6e85ae (patch) | |
| tree | d8ead3f0e3e74b109713b5913f21e03bfddcebc4 /nova/api | |
| parent | 99f00ff09bb38e9d2be5d6b897b146331bd433a4 (diff) | |
| parent | 6fcf4133b49cfefa77151937dec4db097a85c349 (diff) | |
| download | nova-06855306d532c318508baf8a39760c77ae6e85ae.tar.gz nova-06855306d532c318508baf8a39760c77ae6e85ae.tar.xz nova-06855306d532c318508baf8a39760c77ae6e85ae.zip | |
Merge "Use Instance Objects for Start/Stop"
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 537f74efa..00a35d05e 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 @@ -1341,13 +1352,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 @@ -1373,7 +1389,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) @@ -1382,7 +1398,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) @@ -1636,7 +1652,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() @@ -1678,7 +1695,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) |
