-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52afaa5
commit 33f0ca9
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from types import SimpleNamespace | ||
import logging | ||
|
||
import jq | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def build_indirect_host_data(job, event_query: dict): | ||
results = [] | ||
compiled_jq_expressions = {} # Cache for compiled jq expressions | ||
facts_missing_logged = False | ||
for event in job.job_events.filter(task__in=event_query.keys()): | ||
if 'res' not in event.event_data: | ||
continue | ||
jq_str_for_event = event_query[event.task] | ||
if jq_str_for_event not in compiled_jq_expressions: | ||
compiled_jq_expressions[event.task] = jq.compile(jq_str_for_event) | ||
compiled_jq = compiled_jq_expressions[event.task] | ||
for data in compiled_jq.input(event.event_data['res']).all(): | ||
if not data.get('canonical_facts'): | ||
if not facts_missing_logged: | ||
logger.error(f'jq output missing canonical_facts for module {event.task} on event {event.id} using jq:{jq_str_for_event}') | ||
continue | ||
canonical_facts = data['canonical_facts'] | ||
facts = data.get('facts') | ||
results.append(SimpleNamespace(canonical_facts=canonical_facts, facts=facts)) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
{ | ||
"demo.query.example": "" | ||
"demo.query.example": "{canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 18 additions & 1 deletion
19
awx/main/tests/live/tests/projects/test_indirect_host_counting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
from awx.main.tasks.host_indirect import build_indirect_host_data | ||
from awx.main.models import Job | ||
|
||
|
||
def test_indirect_host_counting(live_tmp_folder, run_job_from_playbook): | ||
run_job_from_playbook('test_indirect_host_counting', 'run_task.yml', scm_url=f'file://{live_tmp_folder}/test_host_query') | ||
# TODO: add assertions that the host query data is populated | ||
job = Job.objects.filter(name__icontains='test_indirect_host_counting').order_by('-created').first() | ||
|
||
# Data matches to awx/main/tests/data/projects/host_query/meta/event_query.yml | ||
# this just does things in-line to be a more localized test for the immediate testing | ||
event_query = {'demo.query.example': '{canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}'} | ||
|
||
# Run the task logic directly with local data | ||
results = build_indirect_host_data(job, event_query) | ||
assert len(results) == 1 | ||
host_audit_entry = results[0] | ||
|
||
# Asserts on data that will match to the input jq string from above | ||
assert host_audit_entry.canonical_facts == {'host_name': 'foo_host_default'} | ||
assert host_audit_entry.facts == {'device_type': 'Fake Host'} |