Fix ServerGroup create

Nova api has a different signature from microversion
2.64 onwards[1].

Also adds support_status for 'rules' property missed
earlier in I8e77f54303298da00cbe719afccb449f10fe387c.

[1] https://github.com/openstack/python-novaclient/blob/master/novaclient/v2/server_groups.py#L103-L104

Task: 43168
Task: 43661
Change-Id: If7138c71044adadaf7de255a2fac463b57ff3c47
(cherry picked from commit 9aedaa6d8a,
 e70ff2e153)
This commit is contained in:
rabi 2021-10-08 11:04:00 +05:30
parent 4cf0ff641a
commit 7416442401
2 changed files with 38 additions and 21 deletions

View File

@ -35,11 +35,13 @@ class ServerGroup(resource.Resource):
entity = 'server_groups'
PROPERTIES = (
NAME, POLICIES, RULE
NAME, POLICIES, RULES
) = (
'name', 'policies', 'rule'
'name', 'policies', 'rules'
)
_RULES = (MAX_SERVER_PER_HOST) = ('max_server_per_host')
properties_schema = {
NAME: properties.Schema(
properties.Schema.STRING,
@ -47,7 +49,7 @@ class ServerGroup(resource.Resource):
),
POLICIES: properties.Schema(
properties.Schema.LIST,
_('A list of string policies to apply. '
_('A list of exactly one policy to apply. '
'Defaults to anti-affinity.'),
default=['anti-affinity'],
constraints=[
@ -59,11 +61,17 @@ class ServerGroup(resource.Resource):
properties.Schema.STRING,
),
),
RULE: properties.Schema(
RULES: properties.Schema(
properties.Schema.MAP,
_('A rule for the policy. Currently, only the '
'"max_server_per_host" rule is supported for the '
'"anti-affinity" policy.'),
_('Rules for a policy.'),
schema={
MAX_SERVER_PER_HOST: properties.Schema(
properties.Schema.NUMBER,
_('Maximum servers in a group on a given host. '
'Rule for anti-affinity policy.')
)
},
support_status=support.SupportStatus(version='17.0.0'),
),
}
@ -77,26 +85,25 @@ class ServerGroup(resource.Resource):
msg = _('Required microversion for soft policies not supported.')
raise exception.StackValidationFailed(message=msg)
if self.properties[self.RULE]:
if self.properties[self.RULES]:
is_supported = self.client_plugin().is_version_supported(
MICROVERSION_RULE)
if not is_supported:
msg = _('Required microversion for rule not supported.')
msg = _('Required microversion for rules not supported.')
raise exception.StackValidationFailed(message=msg)
def handle_create(self):
name = self.physical_resource_name()
policies = self.properties[self.POLICIES]
if self.properties[self.RULE] and 'soft-affinity' in policies:
rule = self.properties[self.RULE]
client = self.client()
server_group = client.server_groups.create(name=name,
policies=policies,
rule=rule)
rules = self.properties[self.RULES]
rules_supported = self.client_plugin().is_version_supported(
MICROVERSION_RULE)
if rules_supported:
server_group = self.client().server_groups.create(
name=name, policy=policies[0], rules=rules)
else:
client = self.client()
server_group = client.server_groups.create(name=name,
policies=policies)
server_group = self.client().server_groups.create(
name=name, policies=policies)
self.resource_id_set(server_group.id)
def physical_resource_name(self):

View File

@ -26,7 +26,9 @@ sg_template = {
"type": "OS::Nova::ServerGroup",
"properties": {
"name": "test",
"policies": ["anti-affinity"]
"policies": ["anti-affinity"],
"rules": {
"max_server_per_host": 8}
}
}
}
@ -50,6 +52,12 @@ class NovaServerGroupTest(common.HeatTestCase):
# create mock clients and objects
nova = mock.MagicMock()
self.sg.client = mock.MagicMock(return_value=nova)
mock_plugin = mock.MagicMock()
self.patchobject(mock_plugin,
'is_version_supported',
return_value=True)
self.patchobject(self.sg, 'client_plugin',
return_value=mock_plugin)
self.sg_mgr = nova.server_groups
def _create_sg(self, name):
@ -68,7 +76,7 @@ class NovaServerGroupTest(common.HeatTestCase):
name = 'test'
n = name
def fake_create(name, policies):
def fake_create(name, policy, rules):
self.assertGreater(len(name), 1)
return FakeGroup(n)
self.sg_mgr.create = fake_create
@ -81,7 +89,9 @@ class NovaServerGroupTest(common.HeatTestCase):
self._create_sg('test')
expected_args = ()
expected_kwargs = {'name': 'test',
'policies': ["anti-affinity"],
'policy': "anti-affinity",
'rules': {
'max_server_per_host': 8}
}
self.sg_mgr.create.assert_called_once_with(*expected_args,
**expected_kwargs)