Raise an error if deployment_name cannot be determined

Parameter deployment_name should be set via the name property on
the Deployment resources in the templates, however, if it's None,
it will be defaulted to the name of the parent_resource.
for resources such as ExtraConfigPost, PreConfig, PostConfig,
there is no default parent resource name which means the
generated ansible group_vars will be an empty string if custom
templates don't contain a name property; this in turn will
cause syntax error when executing ansible playbooks.

This code prevents such situations and raises an error if
the value cannpt be determined.

Change-Id: Ibe884331b5fb9269b796a13beb489e91aeca71ed
Closes-bug: #1755078
(cherry picked from commit 3fd4f24e90)
This commit is contained in:
Mike Fedosin 2018-03-20 16:59:39 +01:00 committed by James Slagle
parent 8d42051055
commit 3b0f8cf289
2 changed files with 26 additions and 1 deletions

View File

@ -449,3 +449,22 @@ class TestConfig(base.TestCase):
with warnings.catch_warnings(record=True) as w:
self.config.download_config(stack, self.tmp_dir)
assert "Skipping deployment" in str(w[-1].message)
@patch('tripleo_common.utils.config.Config.get_deployment_data')
def test_config_download_no_deployment_name(self, mock_deployment_data):
heat = mock.MagicMock()
self.config = ooo_config.Config(heat)
stack = mock.MagicMock()
heat.stacks.get.return_value = stack
deployment_data, _ = self._get_config_data('config_data.yaml')
# Delete the name of the first deployment and his parent.
del deployment_data[0].attributes['value']['name']
deployment_data[0].parent_resource = None
mock_deployment_data.return_value = deployment_data
self.tmp_dir = self.useFixture(fixtures.TempDir()).path
self.assertRaises(ValueError,
self.config.download_config, stack, self.tmp_dir)

View File

@ -207,7 +207,7 @@ class Config(object):
server_names = self.get_server_names()
server_ids = dict([(v, k) for (k, v) in server_names.items()])
# role_deployment_names is a dict of role names to deployment names for
# that role. The deployment names are futher separated in their own
# that role. The deployment names are further separated in their own
# dict with keys of pre_deployment/post_deployment.
role_deployment_names = {}
# server_roles is a dict of server name to server role for easier
@ -238,6 +238,11 @@ class Config(object):
# or empty string, default to the name of the parent_resource.
deployment_name = deployment.attributes['value'].get(
'name') or deployment.parent_resource
if not deployment_name:
message = "The deployment name cannot be determined. It " \
"should be set via the name property on the " \
"Deployment resources in the templates."
raise ValueError(message)
try:
int(deployment_name)
@ -269,6 +274,7 @@ class Config(object):
"not be determined."
raise ValueError(message)
deployment_name = resources[0].resource_name
config_dict['deployment_name'] = deployment_name
# reset deploy_server_id to the actual server_id since we have to