From 8144ccdd36063bc970c9e001088ea7f17bd28bdb Mon Sep 17 00:00:00 2001 From: Saravanan KR Date: Thu, 22 Dec 2016 18:20:49 +0530 Subject: [PATCH] User provided pwd has to be merged for get_passwords workflow The user provided passwords are merged directly on the mistral environment during the processing of templates. But the workflow get_passwords, does not consider the user passwords but returns the generated passwords only. The user given passwords and the generated passwords are merged before returning. Closes-Bug: #1652037 Change-Id: Ib8f144225a20105bb0e46a43f52b2f1a219b4832 --- setup.cfg | 1 + tripleo_common/actions/parameters.py | 29 ++++++ .../tests/actions/test_parameters.py | 96 +++++++++++++++++++ workbooks/plan_management.yaml | 10 +- 4 files changed, 131 insertions(+), 5 deletions(-) diff --git a/setup.cfg b/setup.cfg index 558d10991..318b47ae6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -74,6 +74,7 @@ mistral.actions = tripleo.parameters.update = tripleo_common.actions.parameters:UpdateParametersAction tripleo.parameters.update_role = tripleo_common.actions.parameters:UpdateRoleParametersAction tripleo.parameters.generate_passwords = tripleo_common.actions.parameters:GeneratePasswordsAction + tripleo.parameters.get_passwords = tripleo_common.actions.parameters:GetPasswordsAction tripleo.plan.create = tripleo_common.actions.plan:CreatePlanAction tripleo.plan.update = tripleo_common.actions.plan:UpdatePlanAction tripleo.plan.create_container = tripleo_common.actions.plan:CreateContainerAction diff --git a/tripleo_common/actions/parameters.py b/tripleo_common/actions/parameters.py index 42d23d5f9..bee8418f6 100644 --- a/tripleo_common/actions/parameters.py +++ b/tripleo_common/actions/parameters.py @@ -181,3 +181,32 @@ class GeneratePasswordsAction(base.TripleOAction): wc.environments.update(**env_kwargs) return wf_env.variables['passwords'] + + +class GetPasswordsAction(base.TripleOAction): + """Get passwords from the environment + + This method returns the list passwords which are used for the deployment. + It will return a merged list of user provided passwords and generated + passwords, giving priority to the user provided passwords. + """ + + def __init__(self, container=constants.DEFAULT_CONTAINER_NAME): + self.container = container + + def run(self): + wc = self.get_workflow_client() + try: + wf_env = wc.environments.get(self.container) + except Exception: + msg = "Error retrieving mistral environment: %s" % self.container + LOG.exception(msg) + return mistral_workflow_utils.Result(error=msg) + + parameter_defaults = wf_env.variables.get('parameter_defaults', {}) + passwords = wf_env.variables.get('passwords', {}) + for name in constants.PASSWORD_PARAMETER_NAMES: + if name in parameter_defaults: + passwords[name] = parameter_defaults[name] + + return passwords diff --git a/tripleo_common/tests/actions/test_parameters.py b/tripleo_common/tests/actions/test_parameters.py index 26a366f90..bf6ed58cf 100644 --- a/tripleo_common/tests/actions/test_parameters.py +++ b/tripleo_common/tests/actions/test_parameters.py @@ -333,3 +333,99 @@ class GeneratePasswordsActionTest(base.TestCase): existing_passwords["AdminPassword"] = "ExistingPasswordInHeat" # ensure old passwords used and no new generation self.assertEqual(existing_passwords, result) + + +class GetPasswordsActionTest(base.TestCase): + + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_orchestration_client') + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_workflow_client') + @mock.patch('mistral.context.ctx') + def test_password_from_parameter_defaults(self, mock_ctx, + mock_get_workflow_client, + mock_get_orchestration_client): + + mock_ctx.return_value = mock.MagicMock() + mock_mistral = mock.MagicMock() + mock_env = mock.MagicMock() + mock_env.name = constants.DEFAULT_CONTAINER_NAME + mock_env.variables = { + "parameter_defaults": _EXISTING_PASSWORDS, + } + + mock_mistral.environments.get.return_value = mock_env + mock_get_workflow_client.return_value = mock_mistral + + mock_orchestration = mock.MagicMock() + mock_get_orchestration_client.return_value = mock_orchestration + + action = parameters.GetPasswordsAction() + result = action.run() + + # ensure old passwords used and no new generation + self.assertEqual(_EXISTING_PASSWORDS, result) + + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_orchestration_client') + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_workflow_client') + @mock.patch('mistral.context.ctx') + def test_password_from_generated_passwords(self, mock_ctx, + mock_get_workflow_client, + mock_get_orchestration_client): + + mock_ctx.return_value = mock.MagicMock() + mock_mistral = mock.MagicMock() + mock_env = mock.MagicMock() + mock_env.name = constants.DEFAULT_CONTAINER_NAME + + mock_env.variables = { + "parameter_defaults": {}, + "passwords": _EXISTING_PASSWORDS, + } + + mock_mistral.environments.get.return_value = mock_env + mock_get_workflow_client.return_value = mock_mistral + + mock_orchestration = mock.MagicMock() + mock_get_orchestration_client.return_value = mock_orchestration + + action = parameters.GetPasswordsAction() + result = action.run() + + # ensure old passwords used and no new generation + self.assertEqual(_EXISTING_PASSWORDS, result) + + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_orchestration_client') + @mock.patch('tripleo_common.actions.base.TripleOAction.' + 'get_workflow_client') + @mock.patch('mistral.context.ctx') + def test_password_merging_passwords(self, mock_ctx, + mock_get_workflow_client, + mock_get_orchestration_client): + + parameter_defaults = _EXISTING_PASSWORDS.copy() + passwords = {"AdminPassword": parameter_defaults.pop("AdminPassword")} + + mock_ctx.return_value = mock.MagicMock() + mock_mistral = mock.MagicMock() + mock_env = mock.MagicMock() + mock_env.name = constants.DEFAULT_CONTAINER_NAME + mock_env.variables = { + "parameter_defaults": parameter_defaults, + "passwords": passwords + } + + mock_mistral.environments.get.return_value = mock_env + mock_get_workflow_client.return_value = mock_mistral + + mock_orchestration = mock.MagicMock() + mock_get_orchestration_client.return_value = mock_orchestration + + action = parameters.GetPasswordsAction() + result = action.run() + + # ensure old passwords used and no new generation + self.assertEqual(_EXISTING_PASSWORDS, result) diff --git a/workbooks/plan_management.yaml b/workbooks/plan_management.yaml index b44d3dcef..71196da07 100644 --- a/workbooks/plan_management.yaml +++ b/workbooks/plan_management.yaml @@ -231,11 +231,11 @@ workflows: verify_environment_exists: action: mistral.environments_get name=<% $.container %> - on-success: get_or_generate_passwords + on-success: get_environement_passwords on-error: verify_environment_set_status_failed - get_or_generate_passwords: - action: tripleo.parameters.generate_passwords container=<% $.container %> + get_environement_passwords: + action: tripleo.parameters.get_passwords container=<% $.container %> on-success: get_passwords_set_status_success on-error: get_passwords_set_status_failed @@ -243,13 +243,13 @@ workflows: on-success: notify_zaqar publish: status: SUCCESS - message: <% task(get_or_generate_passwords).result %> + message: <% task(get_environement_passwords).result %> get_passwords_set_status_failed: on-success: notify_zaqar publish: status: FAILED - message: <% task(get_or_generate_passwords).result %> + message: <% task(get_environement_passwords).result %> verify_container_set_status_failed: on-success: notify_zaqar