From a5c6470f2d76afb9ac87d05c4326bebf925a55da Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 5 Nov 2020 11:49:11 +0000 Subject: [PATCH] compute: Add 'server group create --rule' option This closes the remaining gap with the 2.64 compute API microversion. Change-Id: Ia42b23d813b7af6ddb1a41f4e9bdc8a6160b908c Signed-off-by: Stephen Finucane --- openstackclient/compute/v2/server_group.py | 70 +++++++++++++------ .../unit/compute/v2/test_server_group.py | 31 +++++++- ...p-create_rule_option-9f84e52f35e7c3ba.yaml | 4 ++ 3 files changed, 82 insertions(+), 23 deletions(-) create mode 100644 releasenotes/notes/server-group-create_rule_option-9f84e52f35e7c3ba.yaml diff --git a/openstackclient/compute/v2/server_group.py b/openstackclient/compute/v2/server_group.py index d245c0927f..783fdbfea3 100644 --- a/openstackclient/compute/v2/server_group.py +++ b/openstackclient/compute/v2/server_group.py @@ -19,6 +19,7 @@ import logging from novaclient import api_versions from osc_lib.cli import format_columns +from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -30,8 +31,9 @@ LOG = logging.getLogger(__name__) _formatters = { - 'policies': format_columns.ListColumn, 'members': format_columns.ListColumn, + 'policies': format_columns.ListColumn, + 'rules': format_columns.DictColumn, } @@ -68,7 +70,19 @@ class CreateServerGroup(command.ShowOne): "Add a policy to " "Specify --os-compute-api-version 2.15 or higher for the " "'soft-affinity' or 'soft-anti-affinity' policy." - ) + ), + ) + parser.add_argument( + '--rule', + metavar='', + action=parseractions.KeyValueAction, + default={}, + dest='rules', + help=_( + "A rule for the policy. Currently, only the " + "'max_server_per_host' rule is supported for the " + "'anti-affinity' policy." + ), ) return parser @@ -84,12 +98,24 @@ class CreateServerGroup(command.ShowOne): ) raise exceptions.CommandError(msg % parsed_args.policy) - policy_arg = {'policies': [parsed_args.policy]} - if compute_client.api_version >= api_versions.APIVersion("2.64"): - policy_arg = {'policy': parsed_args.policy} + if parsed_args.rules: + if compute_client.api_version < api_versions.APIVersion('2.64'): + msg = _( + '--os-compute-api-version 2.64 or greater is required to ' + 'support the --rule option' + ) + raise exceptions.CommandError(msg) + + if compute_client.api_version < api_versions.APIVersion('2.64'): + kwargs = {'policies': [parsed_args.policy]} + else: + kwargs = { + 'policy': parsed_args.policy, + 'rules': parsed_args.rules or None, + } server_group = compute_client.server_groups.create( - name=parsed_args.name, **policy_arg) + name=parsed_args.name, **kwargs) info.update(server_group._info) @@ -161,31 +187,33 @@ class ListServerGroup(command.Lister): if compute_client.api_version >= api_versions.APIVersion("2.64"): policy_key = 'Policy' + columns = ( + 'id', + 'name', + policy_key.lower(), + ) + column_headers = ( + 'ID', + 'Name', + policy_key, + ) if parsed_args.long: - column_headers = columns = ( - 'ID', - 'Name', - policy_key, + columns += ( + 'members', + 'project_id', + 'user_id', + ) + column_headers += ( 'Members', 'Project Id', 'User Id', ) - else: - column_headers = columns = ( - 'ID', - 'Name', - policy_key, - ) return ( column_headers, ( utils.get_item_properties( - s, columns, - formatters={ - 'Policies': format_columns.ListColumn, - 'Members': format_columns.ListColumn, - } + s, columns, formatters=_formatters, ) for s in data ), ) diff --git a/openstackclient/tests/unit/compute/v2/test_server_group.py b/openstackclient/tests/unit/compute/v2/test_server_group.py index 8de7492c79..732c18810b 100644 --- a/openstackclient/tests/unit/compute/v2/test_server_group.py +++ b/openstackclient/tests/unit/compute/v2/test_server_group.py @@ -152,28 +152,55 @@ class TestServerGroupCreate(TestServerGroup): '--os-compute-api-version 2.15 or greater is required', str(ex)) - def test_server_group_create_v264(self): + def test_server_group_create_with_rules(self): self.app.client_manager.compute.api_version = api_versions.APIVersion( '2.64') arglist = [ '--policy', 'soft-anti-affinity', + '--rule', 'max_server_per_host=2', 'affinity_group', ] verifylist = [ ('policy', 'soft-anti-affinity'), + ('rules', {'max_server_per_host': '2'}), ('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, - policy=parsed_args.policy, + policy=parsed_args.policy, # should be 'policy', not 'policies' + rules=parsed_args.rules, ) self.assertCountEqual(self.columns, columns) self.assertCountEqual(self.data, data) + def test_server_group_create_with_rules_pre_v264(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.63') + + arglist = [ + '--policy', 'soft-anti-affinity', + '--rule', 'max_server_per_host=2', + 'affinity_group', + ] + verifylist = [ + ('policy', 'soft-anti-affinity'), + ('rules', {'max_server_per_host': '2'}), + ('name', 'affinity_group'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + ex = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + '--os-compute-api-version 2.64 or greater is required', + str(ex)) + class TestServerGroupDelete(TestServerGroup): diff --git a/releasenotes/notes/server-group-create_rule_option-9f84e52f35e7c3ba.yaml b/releasenotes/notes/server-group-create_rule_option-9f84e52f35e7c3ba.yaml new file mode 100644 index 0000000000..29ed3e941d --- /dev/null +++ b/releasenotes/notes/server-group-create_rule_option-9f84e52f35e7c3ba.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Add support for ``--rule`` option for ``server group create``.