Skip to content

Commit

Permalink
Merge pull request #20 from rickymoorhouse/keyedObjects
Browse files Browse the repository at this point in the history
Keyed objects
  • Loading branch information
rickymoorhouse authored Jan 11, 2021
2 parents edfb33d + b6ff12b commit 1f7d5e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist/
*.egg-info/
build/
.eggs/
hemdev/
12 changes: 9 additions & 3 deletions hemApp/drivers/discovery_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions tests/hosts_keyed_objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"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
}
}
14 changes: 14 additions & 0 deletions tests/test_driver_discovery_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit 1f7d5e4

Please sign in to comment.