Skip to content

Commit

Permalink
Fix #166 (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfigol authored and dbarrosop committed Jul 22, 2018
1 parent 562ce4e commit 19d05bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
22 changes: 17 additions & 5 deletions nornir/core/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,23 @@ def __init__(

self.defaults = defaults or {}

self.groups = groups or {}
for n, g in self.groups.items():
if isinstance(g, dict):
g = Group(name=n, nornir=nornir, **g)
self.groups[n] = g
self.groups: Dict[str, Group] = {}
if groups is not None:
for group_name, group_details in groups.items():
if group_details is None:
group = Group(name=group_name, nornir=nornir)
elif isinstance(group_details, dict):
group = Group(name=group_name, nornir=nornir, **group_details)
elif isinstance(group_details, Group):
group = group_details
else:
raise ValueError(
f"Parsing group {group_name}: "
f"expected dict or Group object, "
f"got {type(group_details)} instead"
)

self.groups[group_name] = group

for group in self.groups.values():
group.groups = self._resolve_groups(group.groups)
Expand Down
1 change: 1 addition & 0 deletions tests/core/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def test_to_dict(self):
"groups": ["parent_group"],
},
"group_2": {"name": "group_2", "site": "site2"},
"empty_group": {"name": "empty_group"},
},
}
assert inventory.filter(role="www").to_dict() == expected
2 changes: 2 additions & 0 deletions tests/inventory_data/groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ group_1:

group_2:
site: site2

empty_group:

0 comments on commit 19d05bf

Please sign in to comment.