Add cluster-create to OSC

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

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

Change-Id: Iff5557411e498a1657e2f0f60fc7c46e16a42f2a
Partially-Implements: blueprint trove-support-in-python-openstackclient
This commit is contained in:
wangyao 2017-12-23 20:27:09 +08:00
parent 896e1cb143
commit 96a6059b6f
4 changed files with 111 additions and 0 deletions

View File

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

View File

@ -33,6 +33,7 @@ openstack.database.v1 =
database_backup_delete = troveclient.osc.v1.database_backups:DeleteDatabaseBackup
database_backup_list = troveclient.osc.v1.database_backups:ListDatabaseBackups
database_backup_show = troveclient.osc.v1.database_backups:ShowDatabaseBackup
database_cluster_create = troveclient.osc.v1.database_clusters:CreateDatabaseCluster
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

View File

@ -18,6 +18,7 @@ from osc_lib import utils
import six
from troveclient.i18n import _
from troveclient.v1.shell import _parse_instance_options
def set_attributes_for_print_detail(cluster):
@ -123,3 +124,64 @@ class DeleteDatabaseCluster(command.Command):
msg = (_("Failed to delete cluster %(cluster)s: %(e)s")
% {'cluster': parsed_args.cluster, 'e': e})
raise exceptions.CommandError(msg)
class CreateDatabaseCluster(command.ShowOne):
_description = _("Creates a new database cluster.")
def get_parser(self, prog_name):
parser = super(CreateDatabaseCluster, self).get_parser(prog_name)
parser.add_argument(
'name',
metavar='<name>',
type=str,
help=_('Name of the cluster.'),
)
parser.add_argument(
'datastore',
metavar='<datastore>',
help=_('A datastore name or ID.'),
)
parser.add_argument(
'datastore_version',
metavar='<datastore_version>',
help=_('A datastore version name or ID.'),
)
parser.add_argument(
'--instance',
metavar='"opt=<value>[,opt=<value> ...] "',
action='append',
dest='instances',
default=[],
help=_("Add an instance to the cluster. Specify multiple "
"times to create multiple instances. "
"Valid options are: flavor=<flavor_name_or_id>, "
"volume=<disk_size_in_GB>, volume_type=<type>, "
"nic='<net-id=<net-uuid>, v4-fixed-ip=<ip-addr>, "
"port-id=<port-uuid>>' "
"(where net-id=network_id, "
"v4-fixed-ip=IPv4r_fixed_address, port-id=port_id), "
"availability_zone=<AZ_hint_for_Nova>, "
"module=<module_name_or_id>, type=<type_of_cluster_node>, "
"related_to=<related_attribute>."),
)
parser.add_argument(
'--locality',
metavar='<policy>',
default=None,
choices=['affinity', 'anti-affinity'],
help=_('Locality policy to use when creating cluster. '
'Choose one of %(choices)s.'),
)
return parser
def take_action(self, parsed_args):
database = self.app.client_manager.database
instances = _parse_instance_options(database, parsed_args.instances)
cluster = database.clusters.create(parsed_args.name,
parsed_args.datastore,
parsed_args.datastore_version,
instances=instances,
locality=parsed_args.locality)
cluster = set_attributes_for_print_detail(cluster)
return zip(*sorted(six.iteritems(cluster)))

View File

@ -18,6 +18,7 @@ from osc_lib import utils
from troveclient import common
from troveclient.osc.v1 import database_clusters
from troveclient.tests.osc.v1 import fakes
from troveclient.v1 import shell
class TestClusters(fakes.TestDatabasev1):
@ -108,3 +109,44 @@ class TestDatabaseClusterDelete(TestClusters):
self.assertRaises(exceptions.CommandError,
self.cmd.take_action,
parsed_args)
class TestDatabaseClusterCreate(TestClusters):
values = ('2015-05-02T10:37:04', 'vertica', '7.1', 'cls-1234',
2, 'test-clstr', 'No tasks for the cluster.',
'NONE', '2015-05-02T11:06:19')
columns = (
'created',
'datastore',
'datastore_version',
'id',
'instance_count',
'name',
'task_description',
'task_name',
'updated',
)
def setUp(self):
super(TestDatabaseClusterCreate, self).setUp()
self.cmd = database_clusters.CreateDatabaseCluster(self.app, None)
self.data = self.fake_clusters.get_clusters_cls_1234()
self.cluster_client.create.return_value = self.data
@mock.patch.object(shell, '_parse_instance_options')
def test_cluster_create(self, mock_find):
instance = 'flavor=02,volume=2'
mock_find.return_value = instance
args = ['test-name', 'vertica', '7.1',
'--instance', instance]
verifylist = [
('name', 'test-name'),
('datastore', 'vertica'),
('datastore_version', '7.1'),
('instances', [instance]),
]
parsed_args = self.check_parser(self.cmd, args, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
self.assertEqual(self.values, data)