Skip to content

Commit

Permalink
bump to 0.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulyalin committed Apr 23, 2024
1 parent 49d364b commit 8a03c8e
Show file tree
Hide file tree
Showing 7 changed files with 663 additions and 601 deletions.
15 changes: 13 additions & 2 deletions nornir_salt/plugins/tasks/nr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def nr_test(
ret_data="__undefined_value__",
excpt=None,
excpt_msg="",
use_task_data=False,
**kwargs
):
"""
Expand All @@ -50,7 +51,9 @@ def nr_test(
:return result: ``ret_data`` or ``**kwargs`` passed to the task
:param excpt: (obj or True) exception object to raise; if True, raises ``RuntimeError``
:param excpt_msg: (str) message to use with exception
:param use_task_data: (bool, str) use host's ``__task__`` data as a return result,
if string returns ``host.data["__task__"][use_task_data]`` value
Order of preference of return data:
1. If ``ret_data_per_host`` present, it is used to form results
Expand All @@ -60,7 +63,15 @@ def nr_test(
"""
ret_data_per_host = ret_data_per_host or {}

if ret_data_per_host:
if use_task_data is True:
return Result(
host=task.host, result=task.host.get("__task__", None)
)
elif use_task_data and isinstance(use_task_data, str):
return Result(
host=task.host, result=task.host.get("__task__", {}).get(use_task_data, "")
)
elif ret_data_per_host:
return Result(
host=task.host, result=ret_data_per_host.get(task.host.name, None)
)
Expand Down
6 changes: 4 additions & 2 deletions nornir_salt/utils/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
StrictFloat,
StrictStr,
conlist,
root_validator,
# root_validator,
model_validator,
Field,
)
from typing import Union, Optional, List, Any, Dict, Callable, Tuple
Expand Down Expand Up @@ -571,7 +572,8 @@ class modelTestsProcessorTest(BaseModel):
class Config:
extra = "allow"

@root_validator(pre=True)
@model_validator(mode='before')
@classmethod
def check_commands_given(cls, values):
test = values["test"]
# verify that test has task defined except for when use_all_tasks is True
Expand Down
1,198 changes: 608 additions & 590 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nornir_salt"
version = "0.21.0"
version = "0.21.1"
description = "Nornir plugins used with SaltStack Salt-Nornir Proxy Minion"
authors = ["Denis Mulyalin <d.mulyalin@gmail.com>"]
maintainers = ["Denis Mulyalin <d.mulyalin@gmail.com>"]
Expand All @@ -24,7 +24,7 @@ classifiers = [
# mandatory dependencies
python = ">=3.8,<4.0"
nornir = ">=3.3.0,<=3.4.1"
pydantic = ">=1.10.2,<=2.5.3"
pydantic = ">=2.0.0,<3.0.0"

# optional dependencies for extras definition
cerberus = { version = "1.3.5", optional = true }
Expand Down Expand Up @@ -60,7 +60,7 @@ robotframework = { version = "7.0", optional = true }
dnspython = { version = "2.4.2", optional = true, python = ">=3.8,<4.0"}
diskcache = { version = "5.6.3", optional = true }
pythonping = { version = "1.1.4", optional = true }
picle = { version = "0.2.*", optional = true }
picle = { version = ">=0.1.0,<1.0.0", optional = true }

# docs dependencies for extras definition
readthedocs-sphinx-search = { version = "0.3.2", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion tests/test_TabulateFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_tabulate_from_aggregatedresult_brief():
["show run | inc ntp", "contains", "7.7.7.8"],
["show run | inc ntp", "contains", "7.7.7.7"],
]
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True)])
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True, build_per_host_tests=True)])
output = nr_with_tests.run(
task=nr_test, ret_data="""ntp server 7.7.7.8""", name="show run | inc ntp"
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_TestsProcessor_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_contains_check_list_of_lists():
["show run | inc ntp", "contains", "ntp server 7.7.7.7"],
]
nr.data.reset_failed_hosts()
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True)])
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True, build_per_host_tests=True)])
output = nr_with_tests.run(
task=nr_test,
ret_data_per_host={
Expand Down Expand Up @@ -1988,7 +1988,7 @@ def test_eval_when_tests_are_list_of_lists():
["show run | inc ntp", "eval", "'7.7.7.7' in result"],
]
nr.data.reset_failed_hosts()
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True)])
nr_with_tests = nr.with_processors([TestsProcessor(tests, remove_tasks=True, build_per_host_tests=True)])
output = nr_with_tests.run(
task=nr_test,
ret_data_per_host={
Expand Down
33 changes: 32 additions & 1 deletion tests/test_task_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,35 @@ def test_task_sleep_random_digit():
assert "Slept for" in result["IOL1"]["sleep"]
assert "Slept for" in result["IOL2"]["sleep"]

# test_task_sleep_random_digit()
# test_task_sleep_random_digit()


@skip_if_no_nornir
def test_nr_test_ret_task_data():
# add __task__data
for host_name, host_object in nr.inventory.hosts.items():
_ = host_object.data.pop("__task__", None) # rewrite task data
host_object.data["__task__"] = {"cmds": ["foo", "bar"]}
output = nr.run(
task=nr_test,
use_task_data=True
)
result = ResultSerializer(output)
pprint.pprint(result)
assert result == {'IOL1': {'nr_test': {'cmds': ['foo', 'bar']}},
'IOL2': {'nr_test': {'cmds': ['foo', 'bar']}}}

@skip_if_no_nornir
def test_nr_test_ret_task_data_key():
# add __task__data
for host_name, host_object in nr.inventory.hosts.items():
_ = host_object.data.pop("__task__", None) # rewrite task data
host_object.data["__task__"] = {"cmds": ["foo", "bar"]}
output = nr.run(
task=nr_test,
use_task_data="cmds"
)
result = ResultSerializer(output)
pprint.pprint(result)
assert result == {'IOL1': {'nr_test': ['foo', 'bar']}, 'IOL2': {'nr_test': ['foo', 'bar']}}

0 comments on commit 8a03c8e

Please sign in to comment.