From 213e47962378b2024bf522f2a9673220a24a503d Mon Sep 17 00:00:00 2001 From: Ricky Moorhouse Date: Thu, 22 Oct 2020 13:27:29 +0100 Subject: [PATCH 1/2] Allow for keyed objects - Tests first --- tests/hosts_keyed_objects.yaml | 16 ++++++++++++++++ tests/test_driver_discovery_file.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/hosts_keyed_objects.yaml diff --git a/tests/hosts_keyed_objects.yaml b/tests/hosts_keyed_objects.yaml new file mode 100644 index 0000000..1534600 --- /dev/null +++ b/tests/hosts_keyed_objects.yaml @@ -0,0 +1,16 @@ +{ + "cluster_a": { + "hostname": "host1.example.com", + "type": "web" + "check": true + }, + "cluster_b": { + "hostname": "host2.example.com", + "type": "web" + "check": true + }, + "cluster_c": { + "hostname": "host0.example.com", + "type": "web" + "check": false + }} \ No newline at end of file diff --git a/tests/test_driver_discovery_file.py b/tests/test_driver_discovery_file.py index ff5bf32..ea921d2 100644 --- a/tests/test_driver_discovery_file.py +++ b/tests/test_driver_discovery_file.py @@ -58,6 +58,20 @@ def test_check_content_key_enable(): assert 'host1.example.com' in hosts assert 'host2.example.com' in hosts +def test_check_content_keyed_objects_enable(): + import hemApp + hosts = hemApp.discover_hosts({ + "type":"file", + "name":"tests/hosts_keyed_objects.yaml", + "enabled_key":"check", + "key":"hostname"}) + assert type(hosts) == list + print(hosts) + assert 'host0.example.com' not in hosts + assert 'host1.example.com' in hosts + assert 'host2.example.com' in hosts + + def test_check_metrics(capsys): import hemApp metrics = hemApp.initialise_metrics({"type":"console"}) From b6ff12bda53fd0dc667c12110f85cba1312389d3 Mon Sep 17 00:00:00 2001 From: Ricky Moorhouse Date: Thu, 22 Oct 2020 13:36:33 +0100 Subject: [PATCH 2/2] Allow for keyed objects in file discovery --- .gitignore | 1 + hemApp/drivers/discovery_file.py | 12 +++++++++--- tests/hosts_keyed_objects.yaml | 9 +++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 984b83b..feb59ff 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist/ *.egg-info/ build/ .eggs/ +hemdev/ \ No newline at end of file diff --git a/hemApp/drivers/discovery_file.py b/hemApp/drivers/discovery_file.py index 05552e4..1b07cc2 100644 --- a/hemApp/drivers/discovery_file.py +++ b/hemApp/drivers/discovery_file.py @@ -14,12 +14,18 @@ def hosts(**kwargs): with open(kwargs['name'], 'rt') as source_file: hosts = yaml.safe_load(source_file) for host in hosts: + # if this is just the key then we need the object + if isinstance(host, str) and isinstance(hosts, dict): + host_object = hosts[host] + else: + host_object = host + print(host_object) if 'key' in kwargs: if 'enabled_key' in kwargs: - if host.get(kwargs['enabled_key'], True) == True: - results.append(host.get(kwargs['key'])) + if host_object.get(kwargs['enabled_key'], True) == True: + results.append(host_object.get(kwargs['key'])) else: - results.append(host.get(kwargs['key'])) + results.append(host_object.get(kwargs['key'])) else: results.append(host) except FileNotFoundError: diff --git a/tests/hosts_keyed_objects.yaml b/tests/hosts_keyed_objects.yaml index 1534600..d5d32a5 100644 --- a/tests/hosts_keyed_objects.yaml +++ b/tests/hosts_keyed_objects.yaml @@ -1,16 +1,17 @@ { "cluster_a": { "hostname": "host1.example.com", - "type": "web" + "type": "web", "check": true }, "cluster_b": { "hostname": "host2.example.com", - "type": "web" + "type": "web", "check": true }, "cluster_c": { "hostname": "host0.example.com", - "type": "web" + "type": "web", "check": false - }} \ No newline at end of file + } +} \ No newline at end of file