summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrabi <ramishra@redhat.com>2016-08-04 09:32:51 +0530
committerrabi <ramishra@redhat.com>2016-08-22 13:46:34 +0530
commit36a0ad29e53e30528a03577f71b2f6fc396daa05 (patch)
treee62064bfd8d95ead1480a946446c0140325941a9
parent110cf140b1a6b1a36c5da69ec7e099a0dba0635a (diff)
downloadheat-36a0ad29e53e30528a03577f71b2f6fc396daa05.tar.gz
Add valid merge strategies
This adds three merge startegies(overwrite, merge, deep-merge). There can be a default merge startegy for an env or parameter specific merge strategy. if none of the above is specified then it defaults to 'overwrite'. Change-Id: I05f56887d762342981e3cb9885872ef58ecd0ad8 Blueprint: environment-merging
-rw-r--r--heat/common/environment_util.py29
-rw-r--r--heat/tests/test_common_env_util.py50
2 files changed, 79 insertions, 0 deletions
diff --git a/heat/common/environment_util.py b/heat/common/environment_util.py
new file mode 100644
index 000000000..5ee4f4984
--- /dev/null
+++ b/heat/common/environment_util.py
@@ -0,0 +1,29 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+ALLOWED_PARAM_MERGE_STRATEGIES = (OVERWRITE, MERGE, DEEP_MERGE) = (
+ 'overwrite', 'merge', 'deep_merge')
+
+
+def get_param_merge_strategy(merge_strategies, param_key):
+
+ if merge_strategies is None:
+ return OVERWRITE
+
+ env_default = merge_strategies.get('default', OVERWRITE)
+
+ merge_strategy = merge_strategies.get(param_key, env_default)
+ if merge_strategy in ALLOWED_PARAM_MERGE_STRATEGIES:
+ return merge_strategy
+
+ return env_default
diff --git a/heat/tests/test_common_env_util.py b/heat/tests/test_common_env_util.py
new file mode 100644
index 000000000..bfcb8665e
--- /dev/null
+++ b/heat/tests/test_common_env_util.py
@@ -0,0 +1,50 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from heat.common import environment_util as env_util
+from heat.tests import common
+
+
+class TestEnvironmentUtil(common.HeatTestCase):
+
+ def test_empty_merge_strategies(self):
+ merge_strategies = {}
+ param_strategy = env_util.get_param_merge_strategy(merge_strategies,
+ 'param1')
+ self.assertEqual(env_util.OVERWRITE, param_strategy)
+
+ def test_default_merge_strategy(self):
+ merge_strategies = {'default': 'deep_merge'}
+ param_strategy = env_util.get_param_merge_strategy(merge_strategies,
+ 'param1')
+ self.assertEqual(env_util.DEEP_MERGE, param_strategy)
+
+ def test_param_sepcific_merge_strategy(self):
+ merge_strategies = {'default': 'merge',
+ 'param1': 'deep_merge'}
+ param_strategy = env_util.get_param_merge_strategy(merge_strategies,
+ 'param1')
+ self.assertEqual(env_util.DEEP_MERGE, param_strategy)
+
+ def test_wrong_param_strategy(self):
+ merge_strategies = {'default': 'merge',
+ 'param1': 'unknown'}
+ param_strategy = env_util.get_param_merge_strategy(merge_strategies,
+ 'param1')
+ self.assertEqual(env_util.MERGE, param_strategy)
+
+ def test_merge_startegies_none(self):
+ merge_strategies = None
+ param_strategy = env_util.get_param_merge_strategy(merge_strategies,
+ 'param1')
+ self.assertEqual(env_util.OVERWRITE, param_strategy)