8000 Add tenant filter to Netbox inventory call · Issue #273 · nornir-automation/nornir · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add tenant filter to Netbox inventory call #273

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

Closed
walidamer711 opened this issue Nov 11, 2018 · 5 comments
Closed

Add tenant filter to Netbox inventory call #273

walidamer711 opened this issue Nov 11, 2018 · 5 comments

Comments

@walidamer711
Copy link
Contributor

Forgive my basic python. I have tested the below and it is working, shall I submit PR.

` class NBInventory(Inventory):
def init(
self,
nb_url=None,
nb_token=None,
nb_tenant=None,
use_slugs=True,
flatten_custom_fields=True,
**kwargs
) -> None:

    nb_url = nb_url or os.environ.get("NB_URL", "http://localhost:8080")
    nb_token = nb_token or os.environ.get(
        "NB_TOKEN", "0123456789abcdef0123456789abcdef01234567"
    )
    headers = {"Authorization": "Token {}".format(nb_token)}
    if nb_tenant:
        query_params = {"tenant": nb_tenant}
        # Create dict of hosts using 'devices' from NetBox filtered by tenant name
        r = requests.get("{}/api/dcim/devices/?limit=0".format(nb_url),
                         params=query_params, headers=headers)
    else:
        # Create dict of hosts using 'devices' from NetBox
        r = requests.get("{}/api/dcim/devices/?limit=0".format(nb_url), headers=headers)
    r.raise_for_status()
    nb_devices = r.json()`

using the below call:

nr = InitNornir(core={"num_workers":100}, inventory={"plugin": "nornir.plugins.inventory.netbox.NBInventory", "options": {"nb_url": "http://x.x.x.x", "nb_token": "0123456789abcdef0123456789abcdef01234567", "nb_tenant": "prod"}})

@dbarrosop
Copy link
Contributor

Are there any other parameter besides nb_tenant that can be used to filter down the hosts? I was thinking we could slightly generalize your solution and allow any key-value pair but I am not sure netbox allows that.

@walidamer711
Copy link
Contributor Author

That will provide a better flexibility for filtering.
Netbox can accept fixed keys and values could be 'str or 'int' and accept multiple parameters. For devices, we can filter (tenant, site, role, status(int), ....) and we can provide multiple parameters to perform AND logic among them.
for example: query_params = {"tenant": "prod", "status": 2}

< 8000 /div>

@dbarrosop
Copy link
Contributor

Can you test PR #275? It also includes the fix in #274

@walidamer711
Copy link
Contributor Author

I have tested with the below and it is working fine.

nr = InitNornir(core={"num_workers":100},
inventory={"plugin": "nornir.plugins.inventory.netbox.NBInventory",
"options": {"nb_url": "http://x.x.x.x",
"nb_token": "0123456789abcdef0123456789abcdef01234567",
"filter_parameters": {"tenant": "prod"}}})

Thanks

@dbarrosop
Copy link
Contributor

Second beta with support for this has been released.

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

No branches or pull requests

2 participants
0