From a1e9f46a53abd039b6be4ab7d7114d09f8722232 Mon Sep 17 00:00:00 2001
From: zhanggang <zhanggang@cmss.chinamobile.com>
Date: Thu, 21 Dec 2017 00:23:55 -0500
Subject: [PATCH] Add configuration-delete to OSC

This change adds database support to the python-openstackclient
project for the configuration-delete command.

The trove command configuration-delete is now:
    openstack database configuration delete

Change-Id: I747566e5a1ddf415c3127a86debc2ba477256141
Partially-Implements: trove-support-in-python-openstackclient
---
 ...ration-delete-to-osc-d52e6a2cc84994e5.yaml |  6 ++++
 setup.cfg                                     |  1 +
 troveclient/osc/v1/database_configurations.py | 25 +++++++++++++++
 .../osc/v1/test_database_configurations.py    | 31 +++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 releasenotes/notes/add-configuration-delete-to-osc-d52e6a2cc84994e5.yaml

diff --git a/releasenotes/notes/add-configuration-delete-to-osc-d52e6a2cc84994e5.yaml b/releasenotes/notes/add-configuration-delete-to-osc-d52e6a2cc84994e5.yaml
new file mode 100644
index 00000000..dc133e39
--- /dev/null
+++ b/releasenotes/notes/add-configuration-delete-to-osc-d52e6a2cc84994e5.yaml
@@ -0,0 +1,6 @@
+---
+features:
+  - |
+    The command ``trove configuration-delete`` is now available
+    to use in the python-openstackclient CLI as ``openstack
+    database configuration delete``
diff --git a/setup.cfg b/setup.cfg
index 793fb35e..c7df212a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -36,6 +36,7 @@ openstack.database.v1 =
     database_cluster_delete = troveclient.osc.v1.database_clusters:DeleteDatabaseCluster
     database_cluster_list = troveclient.osc.v1.database_clusters:ListDatabaseClusters
     database_cluster_show = troveclient.osc.v1.database_clusters:ShowDatabaseCluster
+    database_configuration_delete = troveclient.osc.v1.database_configurations:DeleteDatabaseConfiguration
     database_configuration_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurations
     database_configuration_parameter_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurationParameters
     database_configuration_parameter_show = troveclient.osc.v1.database_configurations:ShowDatabaseConfigurationParameter
diff --git a/troveclient/osc/v1/database_configurations.py b/troveclient/osc/v1/database_configurations.py
index d51490ec..b9639f0d 100644
--- a/troveclient/osc/v1/database_configurations.py
+++ b/troveclient/osc/v1/database_configurations.py
@@ -178,3 +178,28 @@ class ShowDatabaseConfigurationParameter(command.ShowOne):
                                              ' configuration group'
                                              ' by name.'))
         return zip(*sorted(six.iteritems(param._info)))
+
+
+class DeleteDatabaseConfiguration(command.Command):
+
+    _description = _("Deletes a configuration group.")
+
+    def get_parser(self, prog_name):
+        parser = super(DeleteDatabaseConfiguration, self).get_parser(prog_name)
+        parser.add_argument(
+            'configuration_group',
+            metavar='<configuration_group>',
+            help=_('ID or name of the configuration group'),
+        )
+        return parser
+
+    def take_action(self, parsed_args):
+        db_configurations = self.app.client_manager.database.configurations
+        try:
+            configuration = osc_utils.find_resource(
+                db_configurations, parsed_args.configuration_group)
+            db_configurations.delete(configuration)
+        except Exception as e:
+            msg = (_("Failed to delete configuration %(c_group)s: %(e)s")
+                   % {'c_group': parsed_args.configuration_group, 'e': e})
+            raise exceptions.CommandError(msg)
diff --git a/troveclient/tests/osc/v1/test_database_configurations.py b/troveclient/tests/osc/v1/test_database_configurations.py
index 9e26f394..636ff6f8 100644
--- a/troveclient/tests/osc/v1/test_database_configurations.py
+++ b/troveclient/tests/osc/v1/test_database_configurations.py
@@ -10,6 +10,10 @@
 #   License for the specific language governing permissions and limitations
 #   under the License.
 
+import mock
+
+from osc_lib import utils
+
 from troveclient import common
 from troveclient import exceptions
 from troveclient.osc.v1 import database_configurations
@@ -169,3 +173,30 @@ class TestConfigurationParameterShow(TestConfigurations):
         self.assertRaises(exceptions.NoUniqueMatch,
                           self.cmd.take_action,
                           parsed_args)
+
+
+class TestDatabaseConfigurationDelete(TestConfigurations):
+
+    def setUp(self):
+        super(TestDatabaseConfigurationDelete, self).setUp()
+        self.cmd = database_configurations.\
+            DeleteDatabaseConfiguration(self.app, None)
+
+    @mock.patch.object(utils, 'find_resource')
+    def test_configuration_delete(self, mock_find):
+        args = ['config1']
+        mock_find.return_value = args[0]
+        parsed_args = self.check_parser(self.cmd, args, [])
+        result = self.cmd.take_action(parsed_args)
+        self.configuration_client.delete.assert_called_with('config1')
+        self.assertIsNone(result)
+
+    @mock.patch.object(utils, 'find_resource')
+    def test_configuration_delete_with_exception(self, mock_find):
+        args = ['fakeconfig']
+        parsed_args = self.check_parser(self.cmd, args, [])
+
+        mock_find.side_effect = exceptions.CommandError
+        self.assertRaises(exceptions.CommandError,
+                          self.cmd.take_action,
+                          parsed_args)