Add choices for option '--policy'

When creating server group, the '--policy' option is required, but
valid choice is 'affinity' or 'anti-affinity', adding multiple
policies will produce an error. This patch add choices and default
value for option '--policy', and remove the former help message.

Change-Id: I312680af384363b3e9a7de9aa1f0946643e193e2
Closes-Bug: #1662771
This commit is contained in:
zhiyong.dai 2016-11-25 01:04:34 +08:00
parent 083b115d09
commit 26a74d4596
3 changed files with 10 additions and 40 deletions

View File

@ -15,13 +15,13 @@ Create a new server group
.. code-block:: bash .. code-block:: bash
openstack server group create openstack server group create
--policy <policy> [--policy <policy>] ... --policy <policy>
<name> <name>
.. option:: --policy <policy> .. option:: --policy <policy>
Add a policy to :ref:`\<name\> <server_group_create-name>` Add a policy to :ref:`\<name\> <server_group_create-name>`
(repeat option to add multiple policies) ('affinity' or 'anti-affinity', default to 'affinity')
.. _server_group_create-name: .. _server_group_create-name:
.. describe:: <name> .. describe:: <name>

View File

@ -55,10 +55,11 @@ class CreateServerGroup(command.ShowOne):
parser.add_argument( parser.add_argument(
'--policy', '--policy',
metavar='<policy>', metavar='<policy>',
action='append', choices=['affinity', 'anti-affinity'],
required=True, default='affinity',
help=_("Add a policy to <name> " help=_("Add a policy to <name> "
"(repeat option to add multiple policies)") "('affinity' or 'anti-affinity', "
"default to 'affinity')")
) )
return parser return parser
@ -67,7 +68,7 @@ class CreateServerGroup(command.ShowOne):
info = {} info = {}
server_group = compute_client.server_groups.create( server_group = compute_client.server_groups.create(
name=parsed_args.name, name=parsed_args.name,
policies=parsed_args.policy) policies=[parsed_args.policy])
info.update(server_group._info) info.update(server_group._info)
columns = _get_columns(info) columns = _get_columns(info)

View File

@ -63,54 +63,23 @@ class TestServerGroupCreate(TestServerGroup):
def test_server_group_create(self): def test_server_group_create(self):
arglist = [ arglist = [
'--policy', 'affinity', '--policy', 'anti-affinity',
'affinity_group', 'affinity_group',
] ]
verifylist = [ verifylist = [
('policy', ['affinity']), ('policy', 'anti-affinity'),
('name', 'affinity_group'), ('name', 'affinity_group'),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.create.assert_called_once_with( self.server_groups_mock.create.assert_called_once_with(
name=parsed_args.name, name=parsed_args.name,
policies=parsed_args.policy, policies=[parsed_args.policy],
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_server_group_create_with_multiple_policies(self):
arglist = [
'--policy', 'affinity',
'--policy', 'soft-affinity',
'affinity_group',
]
verifylist = [
('policy', ['affinity', 'soft-affinity']),
('name', 'affinity_group'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.create.assert_called_once_with(
name=parsed_args.name,
policies=parsed_args.policy,
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)
def test_server_group_create_no_policy(self):
arglist = [
'affinity_group',
]
verifylist = None
self.assertRaises(tests_utils.ParserException,
self.check_parser,
self.cmd,
arglist,
verifylist)
class TestServerGroupDelete(TestServerGroup): class TestServerGroupDelete(TestServerGroup):