From 3e9109bc7c9511b45568b299da897d977852d76d Mon Sep 17 00:00:00 2001
From: Huanxuan Ao <huanxuan.ao@easystack.cn>
Date: Wed, 7 Dec 2016 11:32:44 +0800
Subject: [PATCH] Add "consistency-group-snapshot" option to consistency group
 create

Add "consistency-group-snapshot" option to
"consistency group create" command to support
for creating consistency group from existing
consistency group snapshot

Implements: bp cinder-command-support
Partial-Bug: #1613964
Change-Id: I54c265d38299f4973945ba99e30042bcf47859c0
---
 .../command-objects/consistency-group.rst     |  6 ++-
 .../unit/volume/v2/test_consistency_group.py  | 39 +++++++++++++++++++
 .../volume/v2/consistency_group.py            | 23 ++++++++---
 3 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/doc/source/command-objects/consistency-group.rst b/doc/source/command-objects/consistency-group.rst
index 46682a56e4..05b52dd66e 100644
--- a/doc/source/command-objects/consistency-group.rst
+++ b/doc/source/command-objects/consistency-group.rst
@@ -13,7 +13,7 @@ Create new consistency group.
 .. code:: bash
 
     os consistency group create
-        --volume-type <volume-type> | --consistency-group-source <consistency-group>
+        --volume-type <volume-type> | --consistency-group-source <consistency-group> | --consistency-group-snapshot <consistency-group-snapshot>
         [--description <description>]
         [--availability-zone <availability-zone>]
         [<name>]
@@ -26,6 +26,10 @@ Create new consistency group.
 
     Existing consistency group (name or ID)
 
+.. option:: --consistency-group-snapshot <consistency-group-snapshot>
+
+    Existing consistency group snapshot (name or ID)
+
 .. option:: --description <description>
 
     Description of this consistency group
diff --git a/openstackclient/tests/unit/volume/v2/test_consistency_group.py b/openstackclient/tests/unit/volume/v2/test_consistency_group.py
index 5beb6ef28d..0e2f162e98 100644
--- a/openstackclient/tests/unit/volume/v2/test_consistency_group.py
+++ b/openstackclient/tests/unit/volume/v2/test_consistency_group.py
@@ -32,6 +32,10 @@ class TestConsistencyGroup(volume_fakes.TestVolume):
             self.app.client_manager.volume.consistencygroups)
         self.consistencygroups_mock.reset_mock()
 
+        self.cgsnapshots_mock = (
+            self.app.client_manager.volume.cgsnapshots)
+        self.cgsnapshots_mock.reset_mock()
+
         self.types_mock = self.app.client_manager.volume.volume_types
         self.types_mock.reset_mock()
 
@@ -41,6 +45,11 @@ class TestConsistencyGroupCreate(TestConsistencyGroup):
     volume_type = volume_fakes.FakeType.create_one_type()
     new_consistency_group = (
         volume_fakes.FakeConsistencyGroup.create_one_consistency_group())
+    consistency_group_snapshot = (
+        volume_fakes.
+        FakeConsistencyGroupSnapshot.
+        create_one_consistency_group_snapshot()
+    )
 
     columns = (
         'availability_zone',
@@ -70,6 +79,8 @@ class TestConsistencyGroupCreate(TestConsistencyGroup):
         self.consistencygroups_mock.get.return_value = (
             self.new_consistency_group)
         self.types_mock.get.return_value = self.volume_type
+        self.cgsnapshots_mock.get.return_value = (
+            self.consistency_group_snapshot)
 
         # Get the command object to test
         self.cmd = consistency_group.CreateConsistencyGroup(self.app, None)
@@ -164,6 +175,34 @@ class TestConsistencyGroupCreate(TestConsistencyGroup):
         self.assertEqual(self.columns, columns)
         self.assertEqual(self.data, data)
 
+    def test_consistency_group_create_from_snapshot(self):
+        arglist = [
+            '--consistency-group-snapshot', self.consistency_group_snapshot.id,
+            '--description', self.new_consistency_group.description,
+            self.new_consistency_group.name,
+        ]
+        verifylist = [
+            ('consistency_group_snapshot', self.consistency_group_snapshot.id),
+            ('description', self.new_consistency_group.description),
+            ('name', self.new_consistency_group.name),
+        ]
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+        columns, data = self.cmd.take_action(parsed_args)
+
+        self.types_mock.get.assert_not_called()
+        self.cgsnapshots_mock.get.assert_called_once_with(
+            self.consistency_group_snapshot.id)
+        self.consistencygroups_mock.create_from_src.assert_called_with(
+            self.consistency_group_snapshot.id,
+            None,
+            name=self.new_consistency_group.name,
+            description=self.new_consistency_group.description,
+        )
+
+        self.assertEqual(self.columns, columns)
+        self.assertEqual(self.data, data)
+
 
 class TestConsistencyGroupDelete(TestConsistencyGroup):
 
diff --git a/openstackclient/volume/v2/consistency_group.py b/openstackclient/volume/v2/consistency_group.py
index 661bcbe51f..fbeae745ea 100644
--- a/openstackclient/volume/v2/consistency_group.py
+++ b/openstackclient/volume/v2/consistency_group.py
@@ -94,6 +94,11 @@ class CreateConsistencyGroup(command.ShowOne):
             metavar="<consistency-group>",
             help=_("Existing consistency group (name or ID)")
         )
+        exclusive_group.add_argument(
+            "--consistency-group-snapshot",
+            metavar="<consistency-group-snapshot>",
+            help=_("Existing consistency group snapshot (name or ID)")
+        )
         parser.add_argument(
             "--description",
             metavar="<description>",
@@ -120,17 +125,23 @@ class CreateConsistencyGroup(command.ShowOne):
                 description=parsed_args.description,
                 availability_zone=parsed_args.availability_zone
             )
-        elif parsed_args.consistency_group_source:
+        else:
             if parsed_args.availability_zone:
                 msg = _("'--availability-zone' option will not work "
                         "if creating consistency group from source")
                 LOG.warning(msg)
-            consistency_group_id = utils.find_resource(
-                volume_client.consistencygroups,
-                parsed_args.consistency_group_source).id
+
+            consistency_group_id = None
             consistency_group_snapshot = None
-            # TODO(Huanxuan Ao): Support for creating from consistency group
-            # snapshot after adding "consistency_group_snapshot" resource
+            if parsed_args.consistency_group_source:
+                consistency_group_id = utils.find_resource(
+                    volume_client.consistencygroups,
+                    parsed_args.consistency_group_source).id
+            elif parsed_args.consistency_group_snapshot:
+                consistency_group_snapshot = utils.find_resource(
+                    volume_client.cgsnapshots,
+                    parsed_args.consistency_group_snapshot).id
+
             consistency_group = (
                 volume_client.consistencygroups.create_from_src(
                     consistency_group_snapshot,