-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added add_host and add_group functions to nornir.core.inventory.Inventory class #372
Added add_host and add_group functions to nornir.core.inventory.Inventory class #372
Conversation
You need to run black to format the code. |
nornir/core/inventory.py
Outdated
@@ -438,3 +438,19 @@ def children_of_group(self, group: Union[str, Group]) -> Set[Host]: | |||
if host.has_parent_group(group): | |||
hosts.add(host) | |||
return hosts | |||
|
|||
def add_host(self, name: str, defaults: Optional[Defaults] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are accepting defaults
as an argument but you are not really using it :) In any case, it's not really necessary so removing it should be ok.
nornir/core/inventory.py
Outdated
""" | ||
Add a host to the inventory after initialization | ||
""" | ||
host = {name: Host(name, defaults=self.defaults, **kwargs)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really good start, however, in order to properly deserialize connection options you should use this method:
https://github.com/nornir-automation/nornir/blob/develop/nornir/core/deserializer/inventory.py#L78
So your code will be something like host = {name: InventoryElement.deserialize_host(name=name, defaults=self.defaults, **kwargs)}
nornir/core/inventory.py
Outdated
""" | ||
Add a group to the inventory after initialization | ||
""" | ||
group = {name: Group(name, defaults=self.defaults, **kwargs)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments as with add_host
but using deserialize_group
instead.
Nice work, just a couple of minor comments |
Nice work! :) |
Added add_host and add_group functions to nornir.core.inventory.Inventory class. Automatically maps the existing inventory defaults to the new host or group as they are dynamically created after initialization. Added unit tests to test_inventory module.
Ref: nornir-automation/nornir Issue# 370