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

feat(anta): NetBox as an inventory source #968

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

JulioPDX
Copy link

@JulioPDX JulioPDX commented Dec 17, 2024

Description

Initial implementation to get NetBox as an inventory source. This has a lot of areas where it could be improved (logging, syntax, naming) but I hope this initial PR can get the idea implemented.

Example using the public NetBox demo instance:

> anta get from-netbox -nbi https://demo.netbox.dev -nbt 1092017ee4dba8515d37a6b0ab104cc5978331c7 -o test.yml
[16:20:14] INFO     Building inventory from netbox instance file          commands.py:119
                    'https://demo.netbox.dev'                                            
[16:20:15] INFO     ANTA inventory file has been created: 'test.yml'         utils.py:130
 ~/git/anta  netbox-inv !2 ?1 ............................................... 3.11.6 py 
> 
anta_inventory:
  hosts:
  - host: 192.168.100.50
    name: dc1-leaf1
    tags:
    - Quebec
    - Bravo
    - Echo
  - host: 192.168.100.51
    name: dc1-leaf2
    tags:
    - Bravo

Still TODO:

  • Add tests
  • How to set verify to False as a parameter and pass pre-checks
  • Documentation
  • Improved Typing
  • Add pynetbox as an optional install

Fixes #257

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have run pre-commit for code linting and typing (pre-commit run)
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (tox -e testenv)

@JulioPDX
Copy link
Author

Also need to look at making the filter or search more dynamic. For example, maybe someone only wants the inventory from site=ny-dc-1.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
34.2% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Collaborator

@gmuloc gmuloc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start! Thanks for taking this over! will need to add tests and documentation for this

Comment on lines +112 to +116
@click.option("--nb-instance", "-nbi", help="Name of the NetBox instance", type=str, required=True)
@click.option("--nb-token", "-nbt", help="NetBox token", type=str, required=True)
@click.option("--nb-platform", "-nbp", help="NetBox device platform", type=str, default="Arista EOS", required=False)
@click.option("--nb-site", "-nbs", help="NetBox site (case sensitive)", type=str, default=None, required=False)
@click.option("--nb-verify", "-nbf", help="NetBox verify SSL", type=bool, default=False, required=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1/ Given that you are in the from-netbox command context I dont think you need to add nb prefix to the options (except if it makes a lot of sense)

2/ the default for security should always be True ;)

3/ I think (and we may not be doing this everywhere) that you don't need required=False as it is the default in click. Same for default=None

Comment on lines +239 to +242
try:
import pynetbox
except ImportError as e:
logging.error(e)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make a better error message telling what to instal pip install anta[netbox]?

Comment on lines +264 to +265
except Exception as e:
raise ValueError(e) from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to be more specific on which Exception you can get

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.

feature request: get inventory from netbox
2 participants