Skip to content
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

Merged
merged 4 commits into from
Apr 8, 2019
Merged

Added add_host and add_group functions to nornir.core.inventory.Inventory class #372

merged 4 commits into from
Apr 8, 2019

Conversation

brandomando
Copy link
Contributor

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

@dbarrosop
Copy link
Contributor

You need to run black to format the code.

@@ -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,
Copy link
Contributor

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.

"""
Add a host to the inventory after initialization
"""
host = {name: Host(name, defaults=self.defaults, **kwargs)}
Copy link
Contributor

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)}

"""
Add a group to the inventory after initialization
"""
group = {name: Group(name, defaults=self.defaults, **kwargs)}
Copy link
Contributor

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.

@dbarrosop
Copy link
Contributor

Nice work, just a couple of minor comments

nornir/core/inventory.py Outdated Show resolved Hide resolved
nornir/core/inventory.py Outdated Show resolved Hide resolved
@dbarrosop dbarrosop changed the title Add host add group Added add_host and add_group functions to nornir.core.inventory.Inventory class Apr 8, 2019
@dbarrosop
Copy link
Contributor

Nice work! :)

@dbarrosop dbarrosop merged commit 92790c2 into nornir-automation:develop Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants