diff --git a/ec2api/api/snapshot.py b/ec2api/api/snapshot.py index 820053b0..cdde6594 100644 --- a/ec2api/api/snapshot.py +++ b/ec2api/api/snapshot.py @@ -61,7 +61,8 @@ def delete_snapshot(context, snapshot_id): cinder.volume_snapshots.delete(snapshot['os_id']) except cinder_exception.NotFound: pass - db_api.delete_item(context, snapshot['id']) + # NOTE(andrey-mp) Don't delete item from DB until it disappears from Cloud + # It will be deleted by describer in the future return True @@ -122,11 +123,18 @@ def _format_snapshot(context, snapshot, os_snapshot, volumes={}, context, 'vol', os_snapshot.volume_id, volumes) volume_id = volume['id'] + # NOTE(andrey-mp): ownerId and progress are empty in just created snapshot + ownerId = os_snapshot.project_id + if not ownerId: + ownerId = context.project_id + progress = os_snapshot.progress + if not progress: + progress = '0%' return {'snapshotId': snapshot['id'], 'volumeId': volume_id, 'status': mapped_status, 'startTime': os_snapshot.created_at, - 'progress': os_snapshot.progress, - 'ownerId': os_snapshot.project_id, + 'progress': progress, + 'ownerId': ownerId, 'volumeSize': os_snapshot.size, 'description': os_snapshot.display_description} diff --git a/ec2api/api/volume.py b/ec2api/api/volume.py index 302efb5d..eeeffb2c 100644 --- a/ec2api/api/volume.py +++ b/ec2api/api/volume.py @@ -62,6 +62,7 @@ def attach_volume(context, volume_id, instance_id, device): nova.volumes.create_server_volume(instance['os_id'], volume['os_id'], device) except (nova_exception.Conflict, nova_exception.BadRequest): + # TODO(andrey-mp): raise correct errors for different cases raise exception.UnsupportedOperation() cinder = clients.cinder(context) os_volume = cinder.volumes.get(volume['os_id']) @@ -96,6 +97,7 @@ def delete_volume(context, volume_id): try: cinder.volumes.delete(volume['os_id']) except cinder_exception.BadRequest: + # TODO(andrey-mp): raise correct errors for different cases raise exception.UnsupportedOperation() except cinder_exception.NotFound: pass diff --git a/ec2api/tests/test_snapshot.py b/ec2api/tests/test_snapshot.py index 2ef8b260..2daa74ca 100644 --- a/ec2api/tests/test_snapshot.py +++ b/ec2api/tests/test_snapshot.py @@ -72,6 +72,26 @@ class SnapshotTestCase(base.ApiTestCase): 'DescribeSnapshots', 'snapshotSet', fakes.ID_EC2_SNAPSHOT_1, 'snapshotId') + def test_describe_snapshots_auto_remove(self): + self.cinder.volume_snapshots.list.return_value = [] + + self.db_api.get_items.side_effect = ( + fakes.get_db_api_get_items({ + 'snap': [fakes.DB_SNAPSHOT_1], + 'vol': [fakes.DB_VOLUME_2]})) + + resp = self.execute('DescribeSnapshots', {}) + self.assertEqual(200, resp['http_status_code']) + resp.pop('http_status_code') + self.assertThat(resp, matchers.DictMatches( + {'snapshotSet': []}, + orderless_lists=True)) + + self.db_api.get_items.assert_any_call(mock.ANY, 'vol') + self.db_api.get_items.assert_any_call(mock.ANY, 'snap') + self.db_api.delete_item.assert_any_call(mock.ANY, + fakes.ID_EC2_SNAPSHOT_1) + def test_describe_snapshots_invalid_parameters(self): self.cinder.volume_snapshots.list.return_value = [ fakes.OSSnapshot(fakes.OS_SNAPSHOT_1),