Add cluster-delete to OSC

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

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

Change-Id: Ibf8f0670231799508a902cffd332c844584ed407
Partially-Implements: trove-support-in-python-openstackclient
This commit is contained in:
zhanggang 2017-12-08 02:43:33 -05:00
parent 854d3e2a31
commit f116f37611
4 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,6 @@
---
features:
- |
The command ``trove cluster-delete`` is now available to use
in the python-openstackclient CLI as ``openstack database
cluster delete``

View File

@ -32,6 +32,7 @@ openstack.cli.extension =
openstack.database.v1 =
database_backup_list = troveclient.osc.v1.database_backups:ListDatabaseBackups
database_backup_show = troveclient.osc.v1.database_backups:ShowDatabaseBackup
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_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurations

View File

@ -13,6 +13,7 @@
"""Database v1 Clusters action implementations"""
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
import six
@ -97,3 +98,28 @@ class ShowDatabaseCluster(command.ShowOne):
cluster = utils.find_resource(database_clusters, parsed_args.cluster)
cluster = set_attributes_for_print_detail(cluster)
return zip(*sorted(six.iteritems(cluster)))
class DeleteDatabaseCluster(command.Command):
_description = _("Deletes a cluster.")
def get_parser(self, prog_name):
parser = super(DeleteDatabaseCluster, self).get_parser(prog_name)
parser.add_argument(
'cluster',
metavar='<cluster>',
help=_('ID or name of the cluster.'),
)
return parser
def take_action(self, parsed_args):
database_clusters = self.app.client_manager.database.clusters
try:
cluster = utils.find_resource(database_clusters,
parsed_args.cluster)
database_clusters.delete(cluster)
except Exception as e:
msg = (_("Failed to delete cluster %(cluster)s: %(e)s")
% {'cluster': parsed_args.cluster, 'e': e})
raise exceptions.CommandError(msg)

View File

@ -10,6 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from osc_lib import exceptions
from osc_lib import utils
from troveclient import common
from troveclient.osc.v1 import database_clusters
from troveclient.tests.osc.v1 import fakes
@ -77,3 +82,29 @@ class TestClusterShow(TestClusters):
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
self.assertEqual(self.values, data)
class TestDatabaseClusterDelete(TestClusters):
def setUp(self):
super(TestDatabaseClusterDelete, self).setUp()
self.cmd = database_clusters.DeleteDatabaseCluster(self.app, None)
@mock.patch.object(utils, 'find_resource')
def test_cluster_delete(self, mock_find):
args = ['cluster1']
mock_find.return_value = args[0]
parsed_args = self.check_parser(self.cmd, args, [])
result = self.cmd.take_action(parsed_args)
self.cluster_client.delete.assert_called_with('cluster1')
self.assertIsNone(result)
@mock.patch.object(utils, 'find_resource')
def test_cluster_delete_with_exception(self, mock_find):
args = ['fakecluster']
parsed_args = self.check_parser(self.cmd, args, [])
mock_find.side_effect = exceptions.CommandError
self.assertRaises(exceptions.CommandError,
self.cmd.take_action,
parsed_args)