From 9143c3955f2dc4223183a068bac1fcb5e7edba55 Mon Sep 17 00:00:00 2001 From: Ben Thomasson Date: Wed, 9 Aug 2023 12:51:48 -0400 Subject: [PATCH] Refactor Action message --- ansible_rulebook/builtin.py | 318 +++++++++++++++++++---------------- ansible_rulebook/messages.py | 31 +++- 2 files changed, 199 insertions(+), 150 deletions(-) diff --git a/ansible_rulebook/builtin.py b/ansible_rulebook/builtin.py index ffb56a0e6..a64892fb6 100644 --- a/ansible_rulebook/builtin.py +++ b/ansible_rulebook/builtin.py @@ -45,7 +45,7 @@ WorkflowJobTemplateNotFoundException, ) from .job_template_runner import job_template_runner -from .messages import Shutdown +from .messages import Shutdown, Action, serialize from .util import get_horizontal_rule, run_at logger = logging.getLogger(__name__) @@ -70,19 +70,21 @@ async def none( ruleset: str, ): await event_log.put( - dict( - type="Action", - action="noop", - action_uuid=str(uuid.uuid4()), - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - activation_id=settings.identifier, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="noop", + action_uuid=str(uuid.uuid4()), + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + activation_id=settings.identifier, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) ) @@ -111,20 +113,22 @@ async def debug(event_log, **kwargs): print(get_horizontal_rule("=")) sys.stdout.flush() await event_log.put( - dict( - type="Action", - action="debug", - action_uuid=str(uuid.uuid4()), - playbook_name=kwargs.get("name"), - ruleset=kwargs.get("source_ruleset_name"), - ruleset_uuid=kwargs.get("source_ruleset_uuid"), - rule=kwargs.get("source_rule_name"), - rule_uuid=kwargs.get("source_rule_uuid"), - rule_run_at=kwargs.get("rule_run_at"), - activation_id=settings.identifier, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(kwargs.get("variables")), + serialize( + Action( + type="Action", + action="debug", + action_uuid=str(uuid.uuid4()), + playbook_name=kwargs.get("name"), + ruleset=kwargs.get("source_ruleset_name"), + ruleset_uuid=kwargs.get("source_ruleset_uuid"), + rule=kwargs.get("source_rule_name"), + rule_uuid=kwargs.get("source_rule_uuid"), + rule_run_at=kwargs.get("rule_run_at"), + activation_id=settings.identifier, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(kwargs.get("variables")), + ) ) ) @@ -153,20 +157,22 @@ async def print_event( print_fn(variables[var_name]) sys.stdout.flush() await event_log.put( - dict( - type="Action", - action="print_event", - action_uuid=str(uuid.uuid4()), - activation_id=settings.identifier, - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - playbook_name=name, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="print_event", + action_uuid=str(uuid.uuid4()), + activation_id=settings.identifier, + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + playbook_name=name, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) ) @@ -189,20 +195,22 @@ async def set_fact( logger.debug("set_fact %s %s", ruleset, fact) lang.assert_fact(ruleset, _embellish_internal_event(fact, "set_fact")) await event_log.put( - dict( - type="Action", - action="set_fact", - action_uuid=str(uuid.uuid4()), - activation_id=settings.identifier, - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - playbook_name=name, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="set_fact", + action_uuid=str(uuid.uuid4()), + activation_id=settings.identifier, + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + playbook_name=name, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) ) @@ -231,20 +239,22 @@ async def retract_fact( lang.retract_matching_facts(ruleset, fact, partial, exclude_keys) await event_log.put( - dict( - type="Action", - action="retract_fact", - action_uuid=str(uuid.uuid4()), - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - activation_id=settings.identifier, - playbook_name=name, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="retract_fact", + action_uuid=str(uuid.uuid4()), + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + activation_id=settings.identifier, + playbook_name=name, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) ) @@ -266,19 +276,21 @@ async def post_event( lang.post(ruleset, _embellish_internal_event(event, "post_event")) await event_log.put( - dict( - type="Action", - action="post_event", - action_uuid=str(uuid.uuid4()), - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - activation_id=settings.identifier, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="post_event", + action_uuid=str(uuid.uuid4()), + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + activation_id=settings.identifier, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) ) @@ -685,22 +697,24 @@ async def post_process_runner( error_message = _get_latest_artifact(private_data_dir, "stdout") logger.error(error_message) - result = dict( - type="Action", - action=action, - action_uuid=str(uuid.uuid4()), - activation_id=activation_id, - playbook_name=name, - job_id=job_id, - ruleset=ruleset, - ruleset_uuid=ruleset_uuid, - rule=rule, - rule_uuid=rule_uuid, - rc=rc, - status=status, - run_at=run_at, - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + result = serialize( + Action( + type="Action", + action=action, + action_uuid=str(uuid.uuid4()), + activation_id=activation_id, + playbook_name=name, + job_id=job_id, + ruleset=ruleset, + ruleset_uuid=ruleset_uuid, + rule=rule, + rule_uuid=rule_uuid, + rc=rc, + status=status, + run_at=run_at, + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) await event_log.put(result) @@ -806,23 +820,25 @@ async def run_job_template( controller_job["created"] = run_at() controller_job["error"] = str(ex) - a_log = dict( - type="Action", - action="run_job_template", - action_uuid=str(uuid.uuid4()), - activation_id=settings.identifier, - job_template_name=name, - organization=organization, - job_id=job_id, - ruleset=ruleset, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - status=controller_job["status"], - run_at=controller_job["created"], - url=_controller_job_url(controller_job), - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + a_log = serialize( + Action( + type="Action", + action="run_job_template", + action_uuid=str(uuid.uuid4()), + activation_id=settings.identifier, + job_template_name=name, + organization=organization, + job_id=job_id, + ruleset=ruleset, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + status=controller_job["status"], + run_at=controller_job["created"], + url=_controller_job_url(controller_job), + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) if "error" in controller_job: a_log["message"] = controller_job["error"] @@ -925,23 +941,25 @@ async def run_workflow_template( controller_job["created"] = run_at() controller_job["error"] = str(ex) - a_log = dict( - type="Action", - action="run_workflow_template", - action_uuid=str(uuid.uuid4()), - activation_id=settings.identifier, - job_template_name=name, - organization=organization, - job_id=job_id, - ruleset=ruleset, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - status=controller_job["status"], - run_at=controller_job["created"], - url=_controller_job_url(controller_job), - matching_events=_get_events(variables), - rule_run_at=rule_run_at, + a_log = serialize( + Action( + type="Action", + action="run_workflow_template", + action_uuid=str(uuid.uuid4()), + activation_id=settings.identifier, + job_template_name=name, + organization=organization, + job_id=job_id, + ruleset=ruleset, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + status=controller_job["status"], + run_at=controller_job["created"], + url=_controller_job_url(controller_job), + matching_events=_get_events(variables), + rule_run_at=rule_run_at, + ) ) if "error" in controller_job: a_log["message"] = controller_job["error"] @@ -988,22 +1006,24 @@ async def shutdown( kind: str = "graceful", ): await event_log.put( - dict( - type="Action", - action="shutdown", - action_uuid=str(uuid.uuid4()), - activation_id=settings.identifier, - ruleset=source_ruleset_name, - ruleset_uuid=source_ruleset_uuid, - rule=source_rule_name, - rule_uuid=source_rule_uuid, - run_at=run_at(), - status=INTERNAL_ACTION_STATUS, - matching_events=_get_events(variables), - delay=delay, - message=message, - kind=kind, - rule_run_at=rule_run_at, + serialize( + Action( + type="Action", + action="shutdown", + action_uuid=str(uuid.uuid4()), + activation_id=settings.identifier, + ruleset=source_ruleset_name, + ruleset_uuid=source_ruleset_uuid, + rule=source_rule_name, + rule_uuid=source_rule_uuid, + run_at=run_at(), + status=INTERNAL_ACTION_STATUS, + matching_events=_get_events(variables), + delay=delay, + message=message, + kind=kind, + rule_run_at=rule_run_at, + ) ) ) diff --git a/ansible_rulebook/messages.py b/ansible_rulebook/messages.py index 1cfdb59de..9fe2bd495 100644 --- a/ansible_rulebook/messages.py +++ b/ansible_rulebook/messages.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dataclasses import dataclass +from dataclasses import dataclass, asdict DEFAULT_SHUTDOWN_DELAY = 60.0 @@ -23,3 +23,32 @@ class Shutdown: delay: float = DEFAULT_SHUTDOWN_DELAY kind: str = "graceful" source_plugin: str = "" + + +@dataclass(frozen=True) +class Action: + action: str + action_uuid: str + ruleset: str + ruleset_uuid: str + rule: str + rule_uuid: str + activation_id: str + run_at: str + status: str + matching_events: list + rule_run_at: str + playbook_name: str = None + job_id: str = None + rc: int = None + delay: int = None + message: str = None + kind: str = None + job_template_name: str = None + organization: str = None + url: str = None + type: str = "Action" + + +serialize = asdict +