Merge "Converting snapshot/backup to use instance objects"

This commit is contained in:
Jenkins
2011-11-11 19:06:18 +00:00
committed by Gerrit Code Review
3 changed files with 35 additions and 15 deletions

View File

@@ -567,8 +567,10 @@ class Controller(object):
msg = _("Invalid metadata")
raise exc.HTTPBadRequest(explanation=msg)
instance = self._get_server(context, instance_id)
image = self.compute_api.backup(context,
instance_id,
instance,
image_name,
backup_type,
rotation,
@@ -846,9 +848,11 @@ class Controller(object):
msg = _("Invalid metadata")
raise exc.HTTPBadRequest(explanation=msg)
instance = self._get_server(context, instance_id)
try:
image = self.compute_api.snapshot(context,
instance_id,
instance,
image_name,
extra_properties=props)
except exception.InstanceBusy:

View File

@@ -1111,41 +1111,41 @@ class API(base.Base):
% instance_id)
@scheduler_api.reroute_compute("backup")
def backup(self, context, instance_id, name, backup_type, rotation,
def backup(self, context, instance, name, backup_type, rotation,
extra_properties=None):
"""Backup the given instance
:param instance_id: nova.db.sqlalchemy.models.Instance.Id
:param instance: nova.db.sqlalchemy.models.Instance
:param name: name of the backup or snapshot
name = backup_type # daily backups are called 'daily'
:param rotation: int representing how many backups to keep around;
None if rotation shouldn't be used (as in the case of snapshots)
:param extra_properties: dict of extra image properties to include
"""
recv_meta = self._create_image(context, instance_id, name, 'backup',
recv_meta = self._create_image(context, instance, name, 'backup',
backup_type=backup_type, rotation=rotation,
extra_properties=extra_properties)
return recv_meta
@scheduler_api.reroute_compute("snapshot")
def snapshot(self, context, instance_id, name, extra_properties=None):
def snapshot(self, context, instance, name, extra_properties=None):
"""Snapshot the given instance.
:param instance_id: nova.db.sqlalchemy.models.Instance.Id
:param instance: nova.db.sqlalchemy.models.Instance
:param name: name of the backup or snapshot
:param extra_properties: dict of extra image properties to include
:returns: A dict containing image metadata
"""
return self._create_image(context, instance_id, name, 'snapshot',
return self._create_image(context, instance, name, 'snapshot',
extra_properties=extra_properties)
def _create_image(self, context, instance_id, name, image_type,
def _create_image(self, context, instance, name, image_type,
backup_type=None, rotation=None, extra_properties=None):
"""Create snapshot or backup for an instance on this host.
:param context: security context
:param instance_id: nova.db.sqlalchemy.models.Instance.Id
:param instance: nova.db.sqlalchemy.models.Instance
:param name: string for name of the snapshot
:param image_type: snapshot | backup
:param backup_type: daily | weekly
@@ -1154,8 +1154,8 @@ class API(base.Base):
:param extra_properties: dict of extra image properties to include
"""
instance = self.db.instance_get(context, instance_id)
task_state = instance["task_state"]
instance_id = instance['id']
if task_state == task_states.IMAGE_BACKUP:
raise exception.InstanceBackingUp(instance_id=instance_id)

View File

@@ -1190,32 +1190,48 @@ class ComputeAPITestCase(BaseTestCase):
self.compute.terminate_instance(self.context, instance_id)
def test_snapshot_conflict_backup(self):
def test_snapshot(self):
"""Can't backup an instance which is already being backed up."""
instance_id = self._create_instance()
instance = self.compute_api.get(self.context, instance_id)
self.compute_api.snapshot(self.context, instance, None, None)
db.instance_destroy(self.context, instance_id)
def test_backup(self):
"""Can't backup an instance which is already being backed up."""
instance_id = self._create_instance()
instance = self.compute_api.get(self.context, instance_id)
self.compute_api.backup(self.context, instance, None, None, None)
db.instance_destroy(self.context, instance_id)
def test_backup_conflict(self):
"""Can't backup an instance which is already being backed up."""
instance_id = self._create_instance()
instance_values = {'task_state': task_states.IMAGE_BACKUP}
db.instance_update(self.context, instance_id, instance_values)
instance = self.compute_api.get(self.context, instance_id)
self.assertRaises(exception.InstanceBackingUp,
self.compute_api.backup,
self.context,
instance_id,
instance,
None,
None,
None)
db.instance_destroy(self.context, instance_id)
def test_snapshot_conflict_snapshot(self):
def test_snapshot_conflict(self):
"""Can't snapshot an instance which is already being snapshotted."""
instance_id = self._create_instance()
instance_values = {'task_state': task_states.IMAGE_SNAPSHOT}
db.instance_update(self.context, instance_id, instance_values)
instance = self.compute_api.get(self.context, instance_id)
self.assertRaises(exception.InstanceSnapshotting,
self.compute_api.snapshot,
self.context,
instance_id,
instance,
None)
db.instance_destroy(self.context, instance_id)