Remove patch_previous_parameters() from Environment

As noted in https://review.openstack.org/#/c/154618/ reviews,
the Environment class already has a good mechanism to enable
initializing with an existing environment or parameters, and
then subsequently load additional parameters.

Switching to this method makes things cleaner when we move to
a full PATCH update, e.g one which enables merging all of a
new environment with the existing one, not just parameters,
so refactoring to remove patch_previous_parameters enables
this to be handled more consistently.

Tests have been removed for the removed code, but note that
the updated logic in service.py is covered by existing tests
in test_engine_service which prove the previous parameter
patching behavior is maintained.

Change-Id: Idb2c00d0486b80a8aabf7902b8a7df2e13f9e914
This commit is contained in:
Steven Hardy 2015-07-22 19:04:11 +01:00 committed by Zane Bitter
parent f9117d24a8
commit d069207fb0
3 changed files with 12 additions and 81 deletions

View File

@ -12,7 +12,6 @@
# under the License.
import collections
import copy
import fnmatch
import glob
import itertools
@ -509,19 +508,6 @@ class Environment(object):
self.param_defaults.update(
env_snippet.get(env_fmt.PARAMETER_DEFAULTS, {}))
def patch_previous_parameters(self, previous_env, clear_parameters=[]):
"""This instance of Environment is the new environment where
we are reusing as default the previous parameter values.
"""
previous_parameters = copy.deepcopy(previous_env.params)
# clear the parameters from the previous set as requested
for p in clear_parameters:
previous_parameters.pop(p, None)
# patch the new set of parameters
previous_parameters.update(self.params)
self.params = previous_parameters
def user_env_as_dict(self):
"""Get the environment as a dict, ready for storing in the db."""
return {env_fmt.RESOURCE_REGISTRY: self.registry.as_dict(),

View File

@ -30,6 +30,7 @@ import six
import webob
from heat.common import context
from heat.common import environment_format as env_fmt
from heat.common import exception
from heat.common.i18n import _
from heat.common.i18n import _LE
@ -755,13 +756,18 @@ class EngineService(service.Service):
raise exception.NotSupported(feature=msg)
# Now parse the template and any parameters for the updated
# stack definition.
env = environment.Environment(params)
# stack definition. If PARAM_EXISTING is specified, we merge
# any environment provided into the existing one.
if args.get(rpc_api.PARAM_EXISTING, None):
env.patch_previous_parameters(
current_stack.env,
args.get(rpc_api.PARAM_CLEAR_PARAMETERS, []))
tmpl = templatem.Template(template, files=files, env=env)
existing_params = current_stack.env.params
clear_params = set(args.get(rpc_api.PARAM_CLEAR_PARAMETERS, []))
retained = dict((k, v) for k, v in existing_params.items()
if k not in clear_params)
new_env = environment.Environment({env_fmt.PARAMETERS: retained})
new_env.load(params)
else:
new_env = environment.Environment(params)
tmpl = templatem.Template(template, files=files, env=new_env)
max_resources = cfg.CONF.max_resources_per_stack
if max_resources != -1 and len(tmpl[tmpl.RESOURCES]) > max_resources:
raise exception.RequestLimitExceeded(

View File

@ -54,67 +54,6 @@ class EnvironmentTest(common.HeatTestCase):
env = environment.Environment(new_env)
self.assertEqual(new_env, env.user_env_as_dict())
def test_existing_parameters(self):
# This tests reusing the existing parameters as is
prev_params = {'foo': 'bar', 'tester': 'Yes'}
params = {}
expected = {'parameters': prev_params,
'encrypted_param_names': [],
'parameter_defaults': {},
'resource_registry': {'resources': {}}}
prev_env = environment.Environment(
{'parameters': prev_params,
'resource_registry': {'resources': {}}})
env = environment.Environment(params)
env.patch_previous_parameters(prev_env)
self.assertEqual(expected, env.user_env_as_dict())
def test_patch_existing_parameters(self):
# This tests patching cli parameters over the existing parameters
prev_params = {'foo': 'bar', 'tester': 'Yes'}
params = {'tester': 'patched'}
expected = {'parameters': {'foo': 'bar', 'tester': 'patched'},
'encrypted_param_names': [],
'parameter_defaults': {},
'resource_registry': {'resources': {}}}
prev_env = environment.Environment(
{'parameters': prev_params,
'resource_registry': {'resources': {}}})
env = environment.Environment(params)
env.patch_previous_parameters(prev_env)
self.assertEqual(expected, env.user_env_as_dict())
def test_patch_and_clear_existing_parameters(self):
# This tests patching cli parameters over the existing parameters
prev_params = {'foo': 'bar', 'tester': 'Yes',
'another_tester': 'Yes'}
params = {'tester': 'patched'}
expected = {'parameters': {'foo': 'bar', 'tester': 'patched'},
'encrypted_param_names': [],
'parameter_defaults': {},
'resource_registry': {'resources': {}}}
prev_env = environment.Environment(
{'parameters': prev_params,
'resource_registry': {'resources': {}}})
env = environment.Environment(params)
env.patch_previous_parameters(prev_env, ['another_tester'])
self.assertEqual(expected, env.user_env_as_dict())
def test_clear_existing_parameters(self):
# This tests removing some parameters in the existing set of parameters
prev_params = {'foo': 'bar', 'tester': 'Yes'}
params = {}
expected = {'parameters': {'foo': 'bar'},
'encrypted_param_names': [],
'parameter_defaults': {},
'resource_registry': {'resources': {}}}
prev_env = environment.Environment(
{'parameters': prev_params,
'resource_registry': {'resources': {}}})
env = environment.Environment(params)
env.patch_previous_parameters(prev_env, ['tester'])
self.assertEqual(expected, env.user_env_as_dict())
def test_global_registry(self):
self.g_env.register_class('CloudX::Nova::Server',
generic_resource.GenericResource)