Merge pull request #43791 from alexbleotu/extra_minion_data_in_pillar-gh

External pillar that includes extra minion data in the minion pillar
This commit is contained in:
Mike Place 2017-09-29 12:02:06 +02:00 committed by GitHub
commit 2f3f4705bd
2 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
'''
Add all extra minion data to the pillar.
:codeauthor: Alexandru.Bleotu@morganstanley.ms.com
One can filter on the keys to include in the pillar by using the ``include``
parameter. For subkeys the ':' notation is supported (i.e. 'key:subkey')
The keyword ``<all>`` includes all keys.
Complete example in etc/salt/master
=====================================
.. code-block:: yaml
ext_pillar:
- extra_minion_data_in_pillar:
include: *
ext_pillar:
- extra_minion_data_in_pillar:
include:
- key1
- key2:subkey2
ext_pillar:
- extra_minion_data_in_pillar:
include: <all>
'''
from __future__ import absolute_import
import logging
# Set up logging
log = logging.getLogger(__name__)
__virtualname__ = 'extra_minion_data_in_pillar'
def __virtual__():
return __virtualname__
def ext_pillar(minion_id, pillar, include, extra_minion_data=None):
def get_subtree(key, source_dict):
'''
Returns a subtree corresponfing to the specified key.
key
Key. Supports the ':' notation (e.g. 'key:subkey')
source_dict
Source dictionary
'''
ret_dict = aux_dict = {}
subtree = source_dict
subkeys = key.split(':')
# Build an empty intermediate subtree following the subkeys
for subkey in subkeys[:-1]:
# The result will be built in aux_dict
aux_dict[subkey] = {}
aux_dict = aux_dict[subkey]
if subkey not in subtree:
# The subkey is not in
return {}
subtree = subtree[subkey]
if subkeys[-1] not in subtree:
# Final subkey is not in subtree
return {}
# Assign the subtree value to the result
aux_dict[subkeys[-1]] = subtree[subkeys[-1]]
return ret_dict
log.trace('minion_id = {0}'.format(minion_id))
log.trace('include = {0}'.format(include))
log.trace('extra_minion_data = {0}'.format(extra_minion_data))
data = {}
if not extra_minion_data:
return {}
if include in ['*', '<all>']:
return extra_minion_data
data = {}
for key in include:
data.update(get_subtree(key, extra_minion_data))
return data

View file

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import
# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock
# Import Salt Libs
from salt.pillar import extra_minion_data_in_pillar
@skipIf(NO_MOCK, NO_MOCK_REASON)
class ExtraMinionDataInPillarTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.pillar.extra_minion_data_in_pillar
'''
def setup_loader_modules(self):
return {
extra_minion_data_in_pillar: {
'__virtual__': True,
}
}
def setUp(self):
self.pillar = MagicMock()
self.extra_minion_data = {'key1': {'subkey1': 'value1'},
'key2': {'subkey2': {'subsubkey2': 'value2'}},
'key3': 'value3',
'key4': {'subkey4': 'value4'}}
def test_extra_values_none_or_empty(self):
ret = extra_minion_data_in_pillar.ext_pillar('fake_id', self.pillar,
'fake_include', None)
self.assertEqual(ret, {})
ret = extra_minion_data_in_pillar.ext_pillar('fake_id', self.pillar,
'fake_include', {})
self.assertEqual(ret, {})
def test_include_all(self):
for include_all in ['*', '<all>']:
ret = extra_minion_data_in_pillar.ext_pillar(
'fake_id', self.pillar, include_all, self.extra_minion_data)
self.assertEqual(ret, self.extra_minion_data)
def test_include_specific_keys(self):
# Tests partially existing key, key with and without subkey,
ret = extra_minion_data_in_pillar.ext_pillar(
'fake_id', self.pillar,
include=['key1:subkey1', 'key2:subkey3', 'key3', 'key4'],
extra_minion_data=self.extra_minion_data)
self.assertEqual(ret, {'key1': {'subkey1': 'value1'},
'key3': 'value3',
'key4': {'subkey4': 'value4'}})