Skip to content

Commit

Permalink
Merge pull request #18 from wvandeun/develop
Browse files Browse the repository at this point in the history
release v0.2.0
  • Loading branch information
wvandeun authored Nov 24, 2020
2 parents ea0b078 + f393eef commit 4c54194
Show file tree
Hide file tree
Showing 17 changed files with 2,254 additions and 58 deletions.
42 changes: 20 additions & 22 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ jobs:
uses: actions/setup-python@v1

- name: Install Poetry
uses: dschep/[email protected]
uses: snok/[email protected]
with:
version: 1.1.4
virtualenvs-create: true
virtualenvs-in-project: true

- name: Cache Poetry virtualenv
uses: actions/cache@v1
id: cache
uses: actions/cache@v2
id: cached-poetry-dependencies
with:
path: ~/.virtualenvs
key: poetry-linters-${{ hashFiles('**/poetry.lock') }}
restore-keys: poetry-linters-${{ hashFiles('**/poetry.lock') }}

- name: Set Poetry config
run: |
poetry config virtualenvs.in-project false
poetry config virtualenvs.path ~/.virtualenvs
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

- name: Install Dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'

- name: Run pylama
run: make pylama
Expand All @@ -52,23 +51,22 @@ jobs:
architecture: x64

- name: Install Poetry
uses: dschep/[email protected]
uses: snok/[email protected]
with:
version: 1.1.4
virtualenvs-create: true
virtualenvs-in-project: true

- name: Cache Poetry virtualenv
uses: actions/cache@v1
id: cache
uses: actions/cache@v2
id: cached-poetry-dependencies
with:
path: ~/.virtualenvs
key: poetry-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
restore-keys: poetry-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Set Poetry config
run: |
poetry config virtualenvs.in-project false
poetry config virtualenvs.path ~/.virtualenvs
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

- name: Install Dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'

- name: Run pytest
run: make pytest
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## 0.2.0 - November 24 2020

* implements support for virtual-machines in NetBoxInventory2 [@patrickdaj](https://github.com/patrickdaj)
* fix github workflow deprecating set-env, add-path commands
* implement `use_platform_slug` for NetBoxInventory2
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ nr = InitNornir(
)
```

### NBInventory arguments

```
Arguments:
nb_url: NetBox url, defaults to http://localhost:8080.
You can also use env variable NB_URL
nb_token: NetBox token. You can also use env variable NB_TOKEN
use_slugs: Whether to use slugs or not
ssl_verify: Enable/disable certificate validation or provide path to CA bundle file
flatten_custom_fields: Whether to assign custom fields directly to the host or not
filter_parameters: Key-value pairs to filter down hosts
```

### NetBoxInventory2 arguments

```
Environment Variables:
* ``NB_URL``: Corresponds to nb_url argument
* ``NB_TOKEN``: Corresponds to nb_token argument
Arguments:
nb_url: NetBox url (defaults to ``http://localhost:8080``)
nb_token: NetBox API token
ssl_verify: Enable/disable certificate validation or provide path to CA bundle file
(defaults to True)
flatten_custom_fields: Assign custom fields directly to the host's data attribute
(defaults to False)
filter_parameters: Key-value pairs that allow you to filter the NetBox inventory.
include_vms: Get virtual machines from NetBox as well as devices.
(defaults to False)
use_platform_slug: Use the NetBox platform slug for the platform attribute of a Host
(defaults to False)
```

# Useful Links

- [Nornir](https://github.com/nornir-automation/nornir)
Expand Down
60 changes: 44 additions & 16 deletions nornir_netbox/plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ class NetBoxInventory2:
flatten_custom_fields: Assign custom fields directly to the host's data attribute
(defaults to False)
filter_parameters: Key-value pairs that allow you to filter the NetBox inventory.
include_vms: Get virtual machines from NetBox as well as devices.
(defaults to False)
use_platform_slug: Use the NetBox platform slug for the platform attribute of a Host
(defaults to False)
"""

def __init__(
Expand All @@ -192,6 +196,8 @@ def __init__(
ssl_verify: Union[bool, str] = True,
flatten_custom_fields: bool = False,
filter_parameters: Optional[Dict[str, Any]] = None,
include_vms: bool = False,
use_platform_slug: bool = False,
**kwargs: Any,
) -> None:
filter_parameters = filter_parameters or {}
Expand All @@ -203,28 +209,29 @@ def __init__(
self.nb_url = nb_url
self.flatten_custom_fields = flatten_custom_fields
self.filter_parameters = filter_parameters
self.include_vms = include_vms
self.use_platform_slug = use_platform_slug

self.session = requests.Session()
self.session.headers.update({"Authorization": f"Token {nb_token}"})
self.session.verify = ssl_verify

def load(self) -> Inventory:

url = f"{self.nb_url}/api/dcim/devices/?limit=0"
nb_devices: List[Dict[str, Any]] = []

while url:
r = self.session.get(url, params=self.filter_parameters)
nb_devices = self._get_resources(
url=f"{self.nb_url}/api/dcim/devices/?limit=0",
params=self.filter_parameters,
)

if not r.status_code == 200:
raise ValueError(
f"Failed to get devices from NetBox instance {self.nb_url}"
if self.include_vms:
nb_devices.extend(
self._get_resources(
url=f"{self.nb_url}/api/virtualization/virtual-machines/?limit=0",
params=self.filter_parameters,
)

resp = r.json()
nb_devices.extend(resp.get("results"))

url = resp.get("next")
)

hosts = Hosts()
groups = Groups()
Expand All @@ -247,11 +254,13 @@ def load(self) -> Inventory:
hostname = device["name"]
serialized_device["hostname"] = hostname

platform = (
device["platform"]["name"]
if isinstance(device["platform"], dict)
else device["platform"]
)
if isinstance(device["platform"], dict) and self.use_platform_slug:
platform = device["platform"].get("slug")
elif isinstance(device["platform"], dict):
platform = device["platform"].get("name")
else:
platform = device["platform"]

serialized_device["platform"] = platform

name = serialized_device["data"].get("name") or str(
Expand All @@ -263,3 +272,22 @@ def load(self) -> Inventory:
)

return Inventory(hosts=hosts, groups=groups, defaults=defaults)

def _get_resources(self, url: str, params: Dict[str, Any]) -> List[Dict[str, Any]]:

resources: List[Dict[str, Any]] = []

while url:
r = self.session.get(url, params=params)

if not r.status_code == 200:
raise ValueError(
f"Failed to get data from NetBox instance {self.nb_url}"
)

resp = r.json()
resources.extend(resp.get("results"))

url = resp.get("next")

return resources
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nornir_netbox"
version = "0.1.1"
version = "0.2.0"
description = "Netbox plugin for Nornir"
authors = ["Wim Van Deun <[email protected]>", "Clay Curtis <[email protected]>"]
license = "Apache-2.0"
Expand Down
12 changes: 6 additions & 6 deletions tests/NetBoxInventory2/2.8.9/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"platform": {
"id": 1,
"url": "http://localhost:8080/api/dcim/platforms/1/",
"name": "junos",
"name": "juniper_junos",
"slug": "junos"
},
"serial": "",
Expand Down Expand Up @@ -70,7 +70,7 @@
},
"name":"1-Core",
"hostname": "10.0.1.1",
"platform": "junos",
"platform": "juniper_junos",
"port": null,
"username": null,
"password": null,
Expand Down Expand Up @@ -176,7 +176,7 @@
"platform": {
"id": 2,
"url": "http://localhost:8080/api/dcim/platforms/2/",
"name": "ios",
"name": "cisco_ios",
"slug": "ios"
},
"serial": "",
Expand Down Expand Up @@ -221,7 +221,7 @@
},
"name":"3-Access",
"hostname": "192.168.3.1",
"platform": "ios",
"platform": "cisco_ios",
"port": null,
"username": null,
"password": null,
Expand Down Expand Up @@ -255,7 +255,7 @@
"platform": {
"id": 1,
"url": "http://localhost:8080/api/dcim/platforms/1/",
"name": "junos",
"name": "juniper_junos",
"slug": "junos"
},
"serial": "",
Expand Down Expand Up @@ -298,7 +298,7 @@
},
"name":"4",
"hostname": "10.0.1.4",
"platform": "junos",
"platform": "juniper_junos",
"port": null,
"username": null,
"password": null,
Expand Down
Loading

0 comments on commit 4c54194

Please sign in to comment.