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 <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-11-05 11:49:11 +00:00
parent bf834f6d75
commit a5c6470f2d
3 changed files with 82 additions and 23 deletions

View File

@ -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 <name> "
"Specify --os-compute-api-version 2.15 or higher for the "
"'soft-affinity' or 'soft-anti-affinity' policy."
)
),
)
parser.add_argument(
'--rule',
metavar='<key=value>',
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
),
)

View File

@ -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):

View File

@ -0,0 +1,4 @@
---
features:
- |
Add support for ``--rule`` option for ``server group create``.