Skip to content

Commit

Permalink
Map cinder snapshot statuses to ec2.
Browse files Browse the repository at this point in the history
EC2 has way fewer statuses than cinder does, so we need to map them
to valid entries. Resolves bug 1080284.

DocImpact.

Change-Id: I73bb2905577fb6f727c0a805df2654ad1bb4ad72
  • Loading branch information
mikalstill authored and Michael Still committed Dec 10, 2012
1 parent 2f962f5 commit eed6267
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
26 changes: 23 additions & 3 deletions nova/api/ec2/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,34 @@ def describe_snapshots(self,
snapshots.append(snapshot)
else:
snapshots = self.volume_api.get_all_snapshots(context)
snapshots = [self._format_snapshot(context, s) for s in snapshots]
return {'snapshotSet': snapshots}

formatted_snapshots = []
for s in snapshots:
formatted = self._format_snapshot(context, s)
if formatted:
formatted_snapshots.append(formatted)
return {'snapshotSet': formatted_snapshots}

def _format_snapshot(self, context, snapshot):
# NOTE(mikal): this is just a set of strings in cinder. If they
# implement an enum, then we should move this code to use it. The
# valid ec2 statuses are "pending", "completed", and "error".
status_map = {'new': 'pending',
'creating': 'pending',
'available': 'completed',
'active': 'completed',
'deleting': 'pending',
'deleted': None,
'error': 'error'}

mapped_status = status_map.get(snapshot['status'], snapshot['status'])
if not mapped_status:
return None

s = {}
s['snapshotId'] = ec2utils.id_to_ec2_snap_id(snapshot['id'])
s['volumeId'] = ec2utils.id_to_ec2_vol_id(snapshot['volume_id'])
s['status'] = snapshot['status']
s['status'] = mapped_status
s['startTime'] = snapshot['created_at']
s['progress'] = snapshot['progress']
s['ownerId'] = snapshot['project_id']
Expand Down
49 changes: 49 additions & 0 deletions nova/tests/api/ec2/test_cinder_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import copy
import tempfile
import uuid

from nova.api.ec2 import cloud
from nova.api.ec2 import ec2utils
Expand Down Expand Up @@ -257,6 +258,54 @@ def test_describe_snapshots(self):
self.cloud.delete_snapshot(self.context, snap2['snapshotId'])
self.cloud.delete_volume(self.context, vol1['volumeId'])

def test_format_snapshot_maps_status(self):
fake_snapshot = {'status': 'new',
'id': 1,
'volume_id': 1,
'created_at': 1353560191.08117,
'progress': 90,
'project_id': str(uuid.uuid4()),
'volume_size': 10000,
'display_description': 'desc'}

self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'pending')

fake_snapshot['status'] = 'creating'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'pending')

fake_snapshot['status'] = 'available'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'completed')

fake_snapshot['status'] = 'active'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'completed')

fake_snapshot['status'] = 'deleting'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'pending')

fake_snapshot['status'] = 'deleted'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot), None)

fake_snapshot['status'] = 'error'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'error')

fake_snapshot['status'] = 'banana'
self.assertEqual(self.cloud._format_snapshot(self.context,
fake_snapshot)['status'],
'banana')

def test_create_snapshot(self):
"""Makes sure create_snapshot works."""
availability_zone = 'zone1:host1'
Expand Down

0 comments on commit eed6267

Please sign in to comment.