diff --git a/os_collect_config/cfn.py b/os_collect_config/cfn.py index 1dd917a..f2709aa 100644 --- a/os_collect_config/cfn.py +++ b/os_collect_config/cfn.py @@ -23,6 +23,7 @@ import six.moves.urllib.parse as urlparse from os_collect_config import common from os_collect_config import exc +from os_collect_config import merger from os_collect_config.openstack.common import log CONF = cfg.CONF @@ -55,6 +56,7 @@ name = 'cfn' class Collector(object): + def __init__(self, requests_impl=common.requests): self._requests_impl = requests_impl self._session = requests_impl.Session() @@ -134,26 +136,6 @@ class Collector(object): 'Sub-key %s does not exist. (%s)' % (subkey, path)) raise exc.CfnMetadataNotAvailable final_content.update(value) - final_list = [] - for depkey in cfg.CONF.cfn.deployment_key: - if depkey in final_content: - deployments = final_content[depkey] - if not isinstance(deployments, list): - logger.warn( - 'Deployment-key %s was found but does not contain a ' - 'list.' % (depkey,)) - continue - logger.debug( - 'Deployment found for %s' % (depkey,)) - for deployment in deployments: - if 'name' not in deployment: - logger.warn( - 'No name found for a deployment under %s.' % - (depkey,)) - continue - if deployment.get('group', 'Heat::Ungrouped') in ( - 'os-apply-config', 'Heat::Ungrouped'): - final_list.append((deployment['name'], - deployment['config'])) - final_list.insert(0, ('cfn', final_content)) + final_list = merger.merged_list_from_content( + final_content, cfg.CONF.cfn.deployment_key, name) return final_list diff --git a/os_collect_config/merger.py b/os_collect_config/merger.py new file mode 100644 index 0000000..16755ce --- /dev/null +++ b/os_collect_config/merger.py @@ -0,0 +1,45 @@ +# +# 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 os_collect_config.openstack.common import log + + +logger = log.getLogger(__name__) + + +def merged_list_from_content(final_content, deployment_keys, collector_name): + final_list = [] + for depkey in deployment_keys: + if depkey in final_content: + deployments = final_content[depkey] + if not isinstance(deployments, list): + logger.warn( + 'Deployment-key %s was found but does not contain a ' + 'list.' % (depkey,)) + continue + logger.debug( + 'Deployment found for %s' % (depkey,)) + for deployment in deployments: + if 'name' not in deployment: + logger.warn( + 'No name found for a deployment under %s.' % + (depkey,)) + continue + if deployment.get('group', 'Heat::Ungrouped') in ( + 'os-apply-config', 'Heat::Ungrouped'): + final_list.append((deployment['name'], + deployment['config'])) + final_list.insert(0, (collector_name, final_content)) + return final_list \ No newline at end of file diff --git a/os_collect_config/tests/test_merger.py b/os_collect_config/tests/test_merger.py new file mode 100644 index 0000000..dabd210 --- /dev/null +++ b/os_collect_config/tests/test_merger.py @@ -0,0 +1,109 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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. + +import testtools + +from os_collect_config import merger + + +META_DATA = {u'int1': 1, + u'strfoo': u'foo', + u'map_ab': { + u'a': 'apple', + u'b': 'banana', + }} + + +SOFTWARE_CONFIG_DATA = { + u'old-style': u'value', + u'deployments': [ + { + u'inputs': [ + { + u'type': u'String', + u'name': u'input1', + u'value': u'value1' + } + ], + u'group': 'Heat::Ungrouped', + u'name': 'dep-name1', + u'outputs': None, + u'options': None, + u'config': { + u'config1': 'value1' + } + }, + { + u'inputs': [ + { + u'type': u'String', + u'name': u'input1', + u'value': u'value1' + } + ], + u'group': 'os-apply-config', + u'name': 'dep-name2', + u'outputs': None, + u'options': None, + u'config': { + u'config2': 'value2' + } + }, + { + u'inputs': [ + { + u'type': u'String', + u'name': u'input1', + u'value': u'value1' + } + ], + u'name': 'dep-name3', + u'outputs': None, + u'options': None, + u'config': { + u'config3': 'value3' + } + }, + { + u'inputs': [], + u'group': 'ignore_me', + u'name': 'ignore_me_name', + u'outputs': None, + u'options': None, + u'config': 'ignore_me_config' + }, + { + u'inputs': [], # to test missing name + } + ] +} + + +class TestMerger(testtools.TestCase): + + def test_merged_list_from_content(self): + req_md = merger.merged_list_from_content( + SOFTWARE_CONFIG_DATA, + ['deployments'], + 'collectme') + self.assertEqual(4, len(req_md)) + self.assertEqual( + SOFTWARE_CONFIG_DATA['deployments'], req_md[0][1]['deployments']) + self.assertEqual( + ('dep-name1', {'config1': 'value1'}), req_md[1]) + self.assertEqual( + ('dep-name2', {'config2': 'value2'}), req_md[2]) + self.assertEqual( + ('dep-name3', {'config3': 'value3'}), req_md[3])