]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Update snapshot rest api to be consistent with volumes
authorMike Perez <thingee@gmail.com>
Tue, 12 Feb 2013 06:49:26 +0000 (22:49 -0800)
committerMike Perez <thingee@gmail.com>
Tue, 12 Feb 2013 10:36:15 +0000 (02:36 -0800)
display_name becomes name and display_description becomes description in
both parameters to pass in requests and keys returned from responses.
This does not update the snapshot model as it takes the same approach
with updating the volume rest api.

Change-Id: I413b5f87882a216b7a847fb12c31f5e5ed91510b

cinder/api/v2/snapshots.py
cinder/tests/api/v2/test_snapshots.py

index b8e00d93c4b2752617bd3df6b92b3f4e63fd3c4b..46813c9b766b586d56459102adf8e2504a1c9868 100644 (file)
@@ -50,8 +50,8 @@ def _translate_snapshot_summary_view(context, snapshot):
 
     d['id'] = snapshot['id']
     d['created_at'] = snapshot['created_at']
-    d['display_name'] = snapshot['display_name']
-    d['display_description'] = snapshot['display_description']
+    d['name'] = snapshot['display_name']
+    d['description'] = snapshot['display_description']
     d['volume_id'] = snapshot['volume_id']
     d['status'] = snapshot['status']
     d['size'] = snapshot['volume_size']
@@ -64,8 +64,8 @@ def make_snapshot(elem):
     elem.set('status')
     elem.set('size')
     elem.set('created_at')
-    elem.set('display_name')
-    elem.set('display_description')
+    elem.set('name')
+    elem.set('description')
     elem.set('volume_id')
 
 
@@ -134,10 +134,15 @@ class SnapshotsController(wsgi.Controller):
 
         search_opts = {}
         search_opts.update(req.GET)
-        allowed_search_options = ('status', 'volume_id', 'display_name')
+        allowed_search_options = ('status', 'volume_id', 'name')
         volumes.remove_invalid_options(context, search_opts,
                                        allowed_search_options)
 
+        # NOTE(thingee): v2 API allows name instead of display_name
+        if 'name' in search_opts:
+            search_opts['display_name'] = search_opts['name']
+            del search_opts['name']
+
         snapshots = self.volume_api.get_all_snapshots(context,
                                                       search_opts=search_opts)
         limited_list = common.limited(snapshots, req)
@@ -159,6 +164,11 @@ class SnapshotsController(wsgi.Controller):
         msg = _("Create snapshot from volume %s")
         LOG.audit(msg, volume_id, context=context)
 
+        # NOTE(thingee): v2 API allows name instead of display_name
+        if 'name' in snapshot:
+            snapshot['display_name'] = snapshot.get('name')
+            del snapshot['name']
+
         if not utils.is_valid_boolstr(force):
             msg = _("Invalid value '%s' for force. ") % force
             raise exception.InvalidParameterValue(err=msg)
@@ -168,13 +178,13 @@ class SnapshotsController(wsgi.Controller):
                 context,
                 volume,
                 snapshot.get('display_name'),
-                snapshot.get('display_description'))
+                snapshot.get('description'))
         else:
             new_snapshot = self.volume_api.create_snapshot(
                 context,
                 volume,
                 snapshot.get('display_name'),
-                snapshot.get('display_description'))
+                snapshot.get('description'))
 
         retval = _translate_snapshot_detail_view(context, new_snapshot)
 
@@ -195,14 +205,26 @@ class SnapshotsController(wsgi.Controller):
         update_dict = {}
 
         valid_update_keys = (
-            'display_name',
+            'name',
+            'description',
             'display_description',
         )
 
+        # NOTE(thingee): v2 API allows description instead of
+        # display_description
+        if 'description' in snapshot:
+            snapshot['display_description'] = snapshot['description']
+            del snapshot['description']
+
         for key in valid_update_keys:
             if key in snapshot:
                 update_dict[key] = snapshot[key]
 
+        # NOTE(thingee): v2 API allows name instead of display_name
+        if 'name' in update_dict:
+            update_dict['display_name'] = update_dict['name']
+            del update_dict['name']
+
         try:
             snapshot = self.volume_api.get_snapshot(context, id)
             self.volume_api.update_snapshot(context, snapshot, update_dict)
index aa9b1a9b235d51c5c3cf88ba6dfddcc73a4579c1..49cc6dee8c705847380df201934f607d288c9b33 100644 (file)
@@ -87,47 +87,52 @@ class SnapshotApiTest(test.TestCase):
     def test_snapshot_create(self):
         self.stubs.Set(volume.api.API, "create_snapshot", stub_snapshot_create)
         self.stubs.Set(volume.api.API, 'get', stubs.stub_volume_get)
+        snapshot_name = 'Snapshot Test Name'
+        snapshot_description = 'Snapshot Test Desc'
         snapshot = {
             "volume_id": '12',
             "force": False,
-            "display_name": "Snapshot Test Name",
-            "display_description": "Snapshot Test Desc"
+            "name": snapshot_name,
+            "description": snapshot_description
         }
+
         body = dict(snapshot=snapshot)
         req = fakes.HTTPRequest.blank('/v2/snapshots')
         resp_dict = self.controller.create(req, body)
 
         self.assertTrue('snapshot' in resp_dict)
-        self.assertEqual(resp_dict['snapshot']['display_name'],
-                         snapshot['display_name'])
-        self.assertEqual(resp_dict['snapshot']['display_description'],
-                         snapshot['display_description'])
+        self.assertEqual(resp_dict['snapshot']['name'],
+                         snapshot_name)
+        self.assertEqual(resp_dict['snapshot']['description'],
+                         snapshot_description)
 
     def test_snapshot_create_force(self):
         self.stubs.Set(volume.api.API, "create_snapshot_force",
                        stub_snapshot_create)
         self.stubs.Set(volume.api.API, 'get', stubs.stub_volume_get)
+        snapshot_name = 'Snapshot Test Name'
+        snapshot_description = 'Snapshot Test Desc'
         snapshot = {
             "volume_id": '12',
             "force": True,
-            "display_name": "Snapshot Test Name",
-            "display_description": "Snapshot Test Desc"
+            "name": snapshot_name,
+            "description": snapshot_description
         }
         body = dict(snapshot=snapshot)
         req = fakes.HTTPRequest.blank('/v2/snapshots')
         resp_dict = self.controller.create(req, body)
 
         self.assertTrue('snapshot' in resp_dict)
-        self.assertEqual(resp_dict['snapshot']['display_name'],
-                         snapshot['display_name'])
-        self.assertEqual(resp_dict['snapshot']['display_description'],
-                         snapshot['display_description'])
+        self.assertEqual(resp_dict['snapshot']['name'],
+                         snapshot_name)
+        self.assertEqual(resp_dict['snapshot']['description'],
+                         snapshot_description)
 
         snapshot = {
             "volume_id": "12",
             "force": "**&&^^%%$$##@@",
-            "display_name": "Snapshot Test Name",
-            "display_description": "Snapshot Test Desc"
+            "name": "Snapshot Test Name",
+            "description": "Snapshot Test Desc"
         }
         body = dict(snapshot=snapshot)
         req = fakes.HTTPRequest.blank('/v2/snapshots')
@@ -141,7 +146,7 @@ class SnapshotApiTest(test.TestCase):
         self.stubs.Set(volume.api.API, "update_snapshot",
                        stubs.stub_snapshot_update)
         updates = {
-            "display_name": "Updated Test Name",
+            "name": "Updated Test Name",
         }
         body = {"snapshot": updates}
         req = fakes.HTTPRequest.blank('/v2/snapshots/%s' % UUID)
@@ -153,8 +158,8 @@ class SnapshotApiTest(test.TestCase):
                 'status': 'available',
                 'size': 100,
                 'created_at': None,
-                'display_name': 'Updated Test Name',
-                'display_description': 'Default description',
+                'name': 'Updated Test Name',
+                'description': 'Default description',
             }
         }
         self.assertEquals(expected, res_dict)
@@ -166,7 +171,7 @@ class SnapshotApiTest(test.TestCase):
                           self.controller.update, req, UUID, body)
 
     def test_snapshot_update_invalid_body(self):
-        body = {'display_name': 'missing top level snapshot key'}
+        body = {'name': 'missing top level snapshot key'}
         req = fakes.HTTPRequest.blank('/v2/snapshots/%s' % UUID)
         self.assertRaises(webob.exc.HTTPUnprocessableEntity,
                           self.controller.update, req, UUID, body)
@@ -174,7 +179,7 @@ class SnapshotApiTest(test.TestCase):
     def test_snapshot_update_not_found(self):
         self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get)
         updates = {
-            "display_name": "Updated Test Name",
+            "name": "Updated Test Name",
         }
         body = {"snapshot": updates}
         req = fakes.HTTPRequest.blank('/v2/snapshots/not-the-uuid')
@@ -296,17 +301,17 @@ class SnapshotApiTest(test.TestCase):
         self.stubs.Set(db, 'snapshot_get_all_by_project',
                        stub_snapshot_get_all_by_project)
 
-        # no display_name filter
+        # no name filter
         req = fakes.HTTPRequest.blank('/v2/snapshots')
         resp = self.controller.index(req)
         self.assertEqual(len(resp['snapshots']), 3)
         # filter by one name
-        req = fakes.HTTPRequest.blank('/v2/snapshots?display_name=backup2')
+        req = fakes.HTTPRequest.blank('/v2/snapshots?name=backup2')
         resp = self.controller.index(req)
         self.assertEqual(len(resp['snapshots']), 1)
-        self.assertEquals(resp['snapshots'][0]['display_name'], 'backup2')
+        self.assertEquals(resp['snapshots'][0]['name'], 'backup2')
         # filter no match
-        req = fakes.HTTPRequest.blank('/v2/snapshots?display_name=backup4')
+        req = fakes.HTTPRequest.blank('/v2/snapshots?name=backup4')
         resp = self.controller.index(req)
         self.assertEqual(len(resp['snapshots']), 0)
 
@@ -343,7 +348,7 @@ class SnapshotSerializerTest(test.TestCase):
         self.assertEqual(tree.tag, 'snapshot')
 
         for attr in ('id', 'status', 'size', 'created_at',
-                     'display_name', 'display_description', 'volume_id'):
+                     'name', 'description', 'volume_id'):
             self.assertEqual(str(snap[attr]), tree.get(attr))
 
     def test_snapshot_show_create_serializer(self):
@@ -353,7 +358,8 @@ class SnapshotSerializerTest(test.TestCase):
             status='snap_status',
             size=1024,
             created_at=datetime.datetime.now(),
-            display_name='snap_name',
+            name='snap_name',
+            description='snap_desc',
             display_description='snap_desc',
             volume_id='vol_id',
         )
@@ -372,8 +378,8 @@ class SnapshotSerializerTest(test.TestCase):
                 status='snap1_status',
                 size=1024,
                 created_at=datetime.datetime.now(),
-                display_name='snap1_name',
-                display_description='snap1_desc',
+                name='snap1_name',
+                description='snap1_desc',
                 volume_id='vol1_id',
             ),
             dict(
@@ -381,8 +387,8 @@ class SnapshotSerializerTest(test.TestCase):
                 status='snap2_status',
                 size=1024,
                 created_at=datetime.datetime.now(),
-                display_name='snap2_name',
-                display_description='snap2_desc',
+                name='snap2_name',
+                description='snap2_desc',
                 volume_id='vol2_id',
             )
         ]