Skip to content

Commit

Permalink
specify explicit kwargs for pillar data and item funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes authored and s0undt3ch committed Mar 30, 2024
1 parent 63d8335 commit 2f24eeb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
79 changes: 69 additions & 10 deletions salt/modules/pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def items(*args, pillar=None, pillar_enc=None, pillarenv=None, saltenv=None):
"""
# Preserve backwards compatibility
if args:
return item(*args)
return item(*args, pillarenv=pillarenv, saltenv=saltenv)

if pillarenv is None:
if __opts__.get("pillarenv_from_saltenv", False):
Expand Down Expand Up @@ -275,7 +275,56 @@ def items(*args, pillar=None, pillar_enc=None, pillarenv=None, saltenv=None):


# Allow pillar.data to also be used to return pillar data
data = salt.utils.functools.alias_function(items, "data")
def data(*args, pillar=None, pillar_enc=None, pillarenv=None, saltenv=None):
"""
Calls the master for a fresh pillar, generates the pillar data on the
fly (same as :py:func:`items`)
pillar
If specified, allows for a dictionary of pillar data to be made
available to pillar and ext_pillar rendering. these pillar variables
will also override any variables of the same name in pillar or
ext_pillar.
pillar_enc
If specified, the data passed in the ``pillar`` argument will be passed
through this renderer to decrypt it.
.. note::
This will decrypt on the minion side, so the specified renderer
must be set up on the minion for this to work. Alternatively,
pillar data can be decrypted master-side. For more information, see
the :ref:`Pillar Encryption <pillar-encryption>` documentation.
Pillar data that is decrypted master-side, is not decrypted until
the end of pillar compilation though, so minion-side decryption
will be necessary if the encrypted pillar data must be made
available in an decrypted state pillar/ext_pillar rendering.
pillarenv
Pass a specific pillar environment from which to compile pillar data.
If not specified, then the minion's :conf_minion:`pillarenv` option is
not used, and if that also is not specified then all configured pillar
environments will be merged into a single pillar dictionary and
returned.
saltenv
Included only for compatibility with
:conf_minion:`pillarenv_from_saltenv`, and is otherwise ignored.
CLI Examples:
.. code-block:: bash
salt '*' pillar.data
"""

return items(
*args,
pillar=pillar,
pillar_enc=pillar_enc,
pillarenv=pillarenv,
saltenv=saltenv,
)


def _obfuscate_inner(var):
Expand All @@ -294,7 +343,7 @@ def _obfuscate_inner(var):
return f"<{var.__class__.__name__}>"


def obfuscate(*args, **kwargs):
def obfuscate(*args, pillar=None, pillar_enc=None, pillarenv=None, saltenv=None):
"""
.. versionadded:: 2015.8.0
Expand All @@ -321,7 +370,15 @@ def obfuscate(*args, **kwargs):
salt '*' pillar.obfuscate
"""
return _obfuscate_inner(items(*args, **kwargs))
return _obfuscate_inner(
items(
*args,
pillar=pillar,
pillar_enc=pillar_enc,
pillarenv=pillarenv,
saltenv=saltenv,
)
)


# naming chosen for consistency with grains.ls, although it breaks the short
Expand Down Expand Up @@ -382,7 +439,7 @@ def ls(*args, pillar=None, pillar_enc=None, pillarenv=None, saltenv=None):
)


def item(*args, **kwargs):
def item(*args, default=None, delimiter=None, pillarenv=None, saltenv=None):
"""
.. versionadded:: 0.16.2
Expand Down Expand Up @@ -436,10 +493,12 @@ def item(*args, **kwargs):
salt '*' pillar.item foo bar baz
"""
ret = {}
default = kwargs.get("default", "")
delimiter = kwargs.get("delimiter", DEFAULT_TARGET_DELIM)
pillarenv = kwargs.get("pillarenv", None)
saltenv = kwargs.get("saltenv", None)

if default is None:
default = ""

if delimiter is None:
delimiter = DEFAULT_TARGET_DELIM

pillar_dict = (
__pillar__
Expand All @@ -450,7 +509,7 @@ def item(*args, **kwargs):
try:
for arg in args:
ret[arg] = salt.utils.data.traverse_dict_and_list(
pillar_dict, arg, default, delimiter
data=pillar_dict, key=arg, default=default, delimiter=delimiter
)
except KeyError:
pass
Expand Down
2 changes: 1 addition & 1 deletion salt/utils/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def get_function_argspec(func, is_class_method=None):
defaults = []
varargs = keywords = None
for param in sig.parameters.values():
if param.kind == param.POSITIONAL_OR_KEYWORD:
if param.kind in [param.POSITIONAL_OR_KEYWORD, param.KEYWORD_ONLY]:
args.append(param.name)
if param.default is not inspect._empty:
defaults.append(param.default)
Expand Down
Empty file.
5 changes: 4 additions & 1 deletion tests/pytests/unit/modules/test_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def test_obfuscate_with_kwargs(pillar_value):
) as pillar_items_mock:
ret = pillarmod.obfuscate(saltenv="saltenv")
# ensure the kwargs are passed along to pillar.items
assert call(saltenv="saltenv") in pillar_items_mock.mock_calls
assert (
call(pillar=None, pillar_enc=None, pillarenv=None, saltenv="saltenv")
in pillar_items_mock.mock_calls
)
assert ret == dict(a="<int>", b="<str>")


Expand Down

0 comments on commit 2f24eeb

Please sign in to comment.