Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for standalone booleans in conditions #297

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ansible_rulebook/rules_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ def parse_action(action: Dict) -> rt.Action:
def parse_condition(condition: Any) -> rt.Condition:
if isinstance(condition, str):
return rt.Condition("all", [parse_condition_value(condition)])
elif isinstance(condition, bool):
return rt.Condition("all", [parse_condition_value(str(condition))])
elif isinstance(condition, dict):
keys = list(condition.keys())
if len(condition) == 1 and keys[0] in ["any", "all"]:
when = keys[0]
return rt.Condition(
when, [parse_condition_value(c) for c in condition[when]]
when, [parse_condition_value(str(c)) for c in condition[when]]
)
else:
raise Exception(
Expand Down
1 change: 1 addition & 0 deletions ansible_rulebook/schema/ruleset_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"condition": {
"anyOf": [
{ "type": "string" },
{ "type": "boolean" },
{"$ref": "#/$defs/all-condition"},
{"$ref": "#/$defs/any-condition"}
]
Expand Down
21 changes: 21 additions & 0 deletions tests/examples/51_literal_boolean.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: 51 literal boolean
hosts: all
sources:
- name: generic
generic:
payload:
- e1:
data:
port: 5000
enabled: true
- e2:
extra:
msg: Hello World

rules:
- name: print event always
condition: true
action:
print_event:
pretty: true
40 changes: 39 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import json
import os

import pytest

from ansible_rulebook.engine import run_rulesets
from ansible_rulebook.engine import run_rulesets, start_source
from ansible_rulebook.messages import Shutdown
from ansible_rulebook.util import load_inventory

Expand Down Expand Up @@ -1294,3 +1295,40 @@ async def test_48_echo():
assert event["type"] == "ProcessedEvent", "2"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "5"


@pytest.mark.asyncio
@pytest.mark.skipif(
os.environ.get("EDA_RULES_ENGINE", "drools") == "durable_rules",
reason="durable rules does not support single booleans",
)
async def test_51_literal_boolean():
ruleset_queues, event_log = load_rulebook(
"examples/51_literal_boolean.yml"
)

queue = ruleset_queues[0][1]
rs = ruleset_queues[0][0]
source_task = asyncio.create_task(
start_source(rs.sources[0], ["sources"], {}, queue)
)

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "print_event", "1"
event = event_log.get_nowait()
assert event["type"] == "ProcessedEvent", "2"
event = event_log.get_nowait()
assert event["type"] == "Action", "3"
assert event["action"] == "print_event", "3"
event = event_log.get_nowait()
assert event["type"] == "ProcessedEvent", "4"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "5"
source_task.cancel()