Skip to content

Commit

Permalink
Merge pull request #12 from rickymoorhouse/discoExclude
Browse files Browse the repository at this point in the history
Support for a boolean to include or exlude
  • Loading branch information
rickymoorhouse authored Oct 9, 2018
2 parents aafba80 + 3ae7ac7 commit 8564371
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
7 changes: 6 additions & 1 deletion hemApp/drivers/discovery_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ def hosts(**kwargs):
try:
with open(kwargs['name'], 'rt') as source_file:
hosts = yaml.safe_load(source_file)
print(hosts)
for host in hosts:
if 'key' in kwargs:
results.append(host.get(kwargs['key']))
if 'enabled_key' in kwargs:
if host.get(kwargs['enabled_key'], True) == True:
results.append(host.get(kwargs['key']))
else:
results.append(host.get(kwargs['key']))
else:
results.append(host)
except FileNotFoundError:
Expand Down
5 changes: 5 additions & 0 deletions tests/hosts_key.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-
hostname: host1.example.com
type: web
check: true
-
hostname: host0.example.com
type: web
check: false
-
hostname: host2.example.com
type: web
15 changes: 14 additions & 1 deletion tests/test_driver_discovery_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ def test_check_content_key():
print(hosts)
assert 'host1.example.com' in hosts

def test_check_content_key_enable():
import hemApp
hosts = hemApp.discover_hosts({
"type":"file",
"name":"tests/hosts_key.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"})
Expand All @@ -60,4 +73,4 @@ def test_check_metrics(capsys):
print("Capsys not working in python 3.3 or 3.4 - investigating")

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 8564371

Please sign in to comment.