From: Deepti Ramakrishna <deepti.ramakrishna@intel.com>
Date: Thu, 19 Nov 2015 08:43:49 +0000 (-0800)
Subject: CG API should return volume type IDs
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=926c6f9fb5da28cc09499d7539581581e825e62c;p=openstack-build%2Fcinder-build.git

CG API should return volume type IDs

The create method of consistency group API requires a list of
volume type ids; volumes belonging to only these types can be added
to the consistency group. The get method of the consistency group API
does not, however, return the volume type id information.

Horizon needs this value for its "Add volumes to consistency group"
panel so that it can filter the "available volumes" list to only
show volumes that match the volume type associated with the consistency
group.

This was fixed by propagating the volume type id information in
the api layer (this information is stored in the db, it just wasn't
being propagated).

APIImpact
The get method of the consistency group API now returns the volume
type ids information.

DocImpact
The screenshots in the admin guide (see
http://docs.openstack.org/admin-guide-cloud/blockstorage-consistency-groups.html)
should be updated to show this new field.

Change-Id: Icc3206c67227af38a7bf89ea0145942f750b506b
Closes-Bug: #1489957
---

diff --git a/cinder/api/views/consistencygroups.py b/cinder/api/views/consistencygroups.py
index 4337c1c8e..306790032 100644
--- a/cinder/api/views/consistencygroups.py
+++ b/cinder/api/views/consistencygroups.py
@@ -49,6 +49,12 @@ class ViewBuilder(common.ViewBuilder):
 
     def detail(self, request, consistencygroup):
         """Detailed view of a single consistency group."""
+        if consistencygroup.volume_type_id:
+            volume_types = consistencygroup.volume_type_id.split(",")
+            volume_types = [type_id for type_id in volume_types if type_id]
+        else:
+            volume_types = []
+
         return {
             'consistencygroup': {
                 'id': consistencygroup.id,
@@ -56,7 +62,8 @@ class ViewBuilder(common.ViewBuilder):
                 'availability_zone': consistencygroup.availability_zone,
                 'created_at': consistencygroup.created_at,
                 'name': consistencygroup.name,
-                'description': consistencygroup.description
+                'description': consistencygroup.description,
+                'volume_types': volume_types,
             }
         }
 
diff --git a/cinder/tests/unit/api/contrib/test_consistencygroups.py b/cinder/tests/unit/api/contrib/test_consistencygroups.py
index 2bb047f2e..d723f03bb 100644
--- a/cinder/tests/unit/api/contrib/test_consistencygroups.py
+++ b/cinder/tests/unit/api/contrib/test_consistencygroups.py
@@ -86,6 +86,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
                          res_dict['consistencygroup']['name'])
         self.assertEqual('creating',
                          res_dict['consistencygroup']['status'])
+        self.assertEqual(['123456'],
+                         res_dict['consistencygroup']['volume_types'])
 
         consistencygroup.destroy()
 
@@ -116,6 +118,28 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
         self.assertEqual('ConsistencyGroup 9999 could not be found.',
                          res_dict['itemNotFound']['message'])
 
+    def test_show_consistencygroup_with_null_volume_type(self):
+        consistencygroup = self._create_consistencygroup(volume_type_id=None)
+        req = webob.Request.blank('/v2/fake/consistencygroups/%s' %
+                                  consistencygroup.id)
+        req.method = 'GET'
+        req.headers['Content-Type'] = 'application/json'
+        res = req.get_response(fakes.wsgi_app())
+        res_dict = json.loads(res.body)
+
+        self.assertEqual(200, res.status_int)
+        self.assertEqual('az1',
+                         res_dict['consistencygroup']['availability_zone'])
+        self.assertEqual('this is a test consistency group',
+                         res_dict['consistencygroup']['description'])
+        self.assertEqual('test_consistencygroup',
+                         res_dict['consistencygroup']['name'])
+        self.assertEqual('creating',
+                         res_dict['consistencygroup']['status'])
+        self.assertEqual([], res_dict['consistencygroup']['volume_types'])
+
+        consistencygroup.destroy()
+
     def test_list_consistencygroups_json(self):
         consistencygroup1 = self._create_consistencygroup()
         consistencygroup2 = self._create_consistencygroup()
@@ -174,7 +198,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
     def test_list_consistencygroups_detail_json(self):
         consistencygroup1 = self._create_consistencygroup()
         consistencygroup2 = self._create_consistencygroup()
-        consistencygroup3 = self._create_consistencygroup()
+        consistencygroup3 = self._create_consistencygroup(volume_type_id=(
+                                                          'uuid1,uuid2'))
 
         req = webob.Request.blank('/v2/fake/consistencygroups/detail')
         req.method = 'GET'
@@ -194,6 +219,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
                          res_dict['consistencygroups'][0]['id'])
         self.assertEqual('creating',
                          res_dict['consistencygroups'][0]['status'])
+        self.assertEqual(['123456'],
+                         res_dict['consistencygroups'][0]['volume_types'])
 
         self.assertEqual('az1',
                          res_dict['consistencygroups'][1]['availability_zone'])
@@ -205,6 +232,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
                          res_dict['consistencygroups'][1]['id'])
         self.assertEqual('creating',
                          res_dict['consistencygroups'][1]['status'])
+        self.assertEqual(['123456'],
+                         res_dict['consistencygroups'][1]['volume_types'])
 
         self.assertEqual('az1',
                          res_dict['consistencygroups'][2]['availability_zone'])
@@ -216,6 +245,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
                          res_dict['consistencygroups'][2]['id'])
         self.assertEqual('creating',
                          res_dict['consistencygroups'][2]['status'])
+        self.assertEqual(['uuid1', 'uuid2'],
+                         res_dict['consistencygroups'][2]['volume_types'])
 
         consistencygroup1.destroy()
         consistencygroup2.destroy()