Skip to content

Commit

Permalink
Merge pull request #34 from opsmill/bgi-bump-sdk-1.3
Browse files Browse the repository at this point in the history
Bump sdk to v1.4.1
  • Loading branch information
wvandeun authored Jan 9, 2025
2 parents b894ddd + 3afb81f commit 9c58266
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ jobs:
uses: "actions/checkout@v3"
- name: "Setup environment"
run: |
pipx install poetry
pip install invoke toml
pipx install poetry invoke
- name: "Install Linters"
run: "poetry install --only=dev"
- name: "Linting"
Expand All @@ -44,15 +43,17 @@ jobs:
- name: "Check out repository code"
uses: "actions/checkout@v3"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
id: python
with:
python-version: ${{ matrix.python-version }}
- name: "Setup environment"
run: |
pipx install poetry
pip install invoke toml
env:
PIPX_DEFAULT_PYTHON: ${{ steps.python.outputs.python-path }}
- name: "Install Nornir Infrahub"
run: "poetry install"
run: "poetry install --with=dev"
- name: "Pytest Tests"
run: "poetry run pytest -v tests/"

Expand Down
12 changes: 9 additions & 3 deletions examples/nornir_tasks.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,31 @@ def main():
# we only need to run this task once, per artifact definition
run_once = nr.filter(name="jfk1-edge1")
result = run_once.run(task=generate_artifacts, artifact="startup-config", timeout=20)
ocfg_result = run_once.run(task=generate_artifacts, artifact="openconfig-interfaces", timeout=20)

for _, v in result.items():
if v[0].failed:
return 1
for _, v in ocfg_result.items():
if v[0].failed:
return 1

# These artifacts are generated for Arista EOS devices in demo-edge
eos_devices = nr.filter(platform="eos")
# retrieves the artifact for all the hosts in the inventory
result = nr.run(task=get_artifact, artifact="startup-config")
result = eos_devices.run(task=get_artifact, artifact="Startup Config for Edge devices")
print_result(result)

# push the retrieved artifact to a device
# print_result(run_once.run(task=napalm_configure, configuration=result["jfk1-edge1"][0].result, replace=True))

# artifacts with content-type application/json get deserialized
result = nr.run(task=get_artifact, artifact="openconfig-interfaces")
result = eos_devices.run(task=get_artifact, artifact="Openconfig Interface for Arista devices")
print_result(result)
assert isinstance(result["den1-edge1"][0].result, dict)

# regenerate an artifact for a host
print_result(nr.run(task=regenerate_host_artifact, artifact="startup-config"))
print_result(eos_devices.run(task=regenerate_host_artifact, artifact="Startup Config for Edge devices"))

return 0

Expand Down
4 changes: 2 additions & 2 deletions nornir_infrahub/plugins/inventory/infrahub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ruamel.yaml
from infrahub_sdk import Config, InfrahubClientSync
from infrahub_sdk.node import InfrahubNodeSync
from infrahub_sdk.schema import NodeSchema
from infrahub_sdk.schema import NodeSchemaAPI
from nornir.core.inventory import (
ConnectionOptions,
Defaults,
Expand Down Expand Up @@ -115,7 +115,7 @@ def validate_include(cls, data: Any) -> Any:
return data


def get_related_nodes(node_schema: NodeSchema, attrs: Set[str]) -> Set[str]:
def get_related_nodes(node_schema: NodeSchemaAPI, attrs: Set[str]) -> Set[str]:
nodes = {"CoreStandardGroup"}
relationship_schemas = {schema.name: schema.peer for schema in node_schema.relationships}
for attr in attrs:
Expand Down
49 changes: 42 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nornir-infrahub"
version = "1.0.0"
version = "1.0.1"
description = "Nornir plugin for Infrahub"
authors = ["OpsMill <[email protected]>"]
readme = "README.md"
Expand All @@ -24,7 +24,7 @@ packages = [{ include = "nornir_infrahub" }]

[tool.poetry.dependencies]
python = "^3.9, <3.13"
infrahub-sdk = { version = "^1", extras = ["tests"] }
infrahub-sdk = { version = "^1,>=1.3.0", extras = ["tests"] }
ruamel-yaml = "^0.18.5"
nornir = "^3.4.1"
nornir-utils = "^0.2.0"
Expand All @@ -40,6 +40,7 @@ pytest-asyncio = "^0.21.1"
types-python-slugify = "*"
invoke = "2.2.0"
yamllint = "*"
setuptools = "^75.7.0"

[tool.coverage.run]
branch = true
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from infrahub_sdk import InfrahubClient, InfrahubClientSync
from infrahub_sdk.schema import NodeSchema
from infrahub_sdk.schema import NodeSchemaAPI
from nornir_infrahub.plugins.inventory.infrahub import InfrahubInventory


Expand All @@ -18,7 +18,7 @@ async def client_sync() -> InfrahubClientSync:


@pytest.fixture
async def location_schema() -> NodeSchema:
async def location_schema() -> NodeSchemaAPI:
data = {
"name": "Location",
"namespace": "Builtin",
Expand All @@ -34,11 +34,11 @@ async def location_schema() -> NodeSchema:
{"name": "member_of_groups", "peer": "CoreGroup", "optional": True, "cardinality": "many", "kind": "Group"},
],
}
return NodeSchema(**data)
return NodeSchemaAPI(**data)


@pytest.fixture
async def device_schema() -> NodeSchema:
async def device_schema() -> NodeSchemaAPI:
data = {
"name": "Device",
"namespace": "Infra",
Expand Down Expand Up @@ -68,11 +68,11 @@ async def device_schema() -> NodeSchema:
},
],
}
return NodeSchema(**data)
return NodeSchemaAPI(**data)


@pytest.fixture
async def ipaddress_schema() -> NodeSchema:
async def ipaddress_schema() -> NodeSchemaAPI:
data = {
"name": "IPAddress",
"namespace": "Infra",
Expand All @@ -86,7 +86,7 @@ async def ipaddress_schema() -> NodeSchema:
{"name": "interface", "peer": "InfraInterfaceL3", "optional": True, "cardinality": "one", "kind": "Parent"}
],
}
return NodeSchema(**data)
return NodeSchemaAPI(**data)


@pytest.fixture
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from infrahub_sdk import InfrahubClient
from infrahub_sdk.node import InfrahubNodeSync
from infrahub_sdk.schema import NodeSchema
from infrahub_sdk.schema import NodeSchemaAPI
from nornir.core.inventory import ConnectionOptions, Defaults # , HostOrGroup
from nornir_infrahub.plugins.inventory.infrahub import ( # _get_inventory_element,
HostNode,
Expand Down Expand Up @@ -38,21 +38,21 @@ def test_ip_interface_to_ip_string_ipv6():
# resolve_node_mapping


def test_valid_mapping(client: InfrahubClient, ipaddress_schema: NodeSchema, ipaddress_data: dict[str, Any]):
def test_valid_mapping(client: InfrahubClient, ipaddress_schema: NodeSchemaAPI, ipaddress_data: dict[str, Any]):
node = InfrahubNodeSync(client=client, schema=ipaddress_schema, data=ipaddress_data)
attrs = ["address"]
result = resolve_node_mapping(node, attrs)
assert result == "192.168.1.1"


def test_unsupported_cardinality(client: InfrahubClient, location_schema: NodeSchema):
def test_unsupported_cardinality(client: InfrahubClient, location_schema: NodeSchemaAPI):
node = InfrahubNodeSync(client=client, schema=location_schema)
attrs = ["tags"]
with pytest.raises(RuntimeError, match="Relations with many cardinality are not supported!"):
resolve_node_mapping(node, attrs)


def test_invalid_mapping(client: InfrahubClient, ipaddress_schema: NodeSchema):
def test_invalid_mapping(client: InfrahubClient, ipaddress_schema: NodeSchemaAPI):
node = InfrahubNodeSync(client=client, schema=ipaddress_schema)
attrs = ["invalid_attribute"]
with pytest.raises(RuntimeError, match="Unable to resolve mapping"):
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_get_related_nodes():
{"name": "vehicules", "peer": "TestVehicule", "cardinality": "many", "identifier": "person__vehicule"}
],
}
node_schema = NodeSchema(**schema)
node_schema = NodeSchemaAPI(**schema)
attrs = {"vehicules", "address"} # "Address" is not in the relationships
result = get_related_nodes(node_schema, attrs)
expected_result = {"CoreStandardGroup", "TestVehicule"}
Expand Down

0 comments on commit 9c58266

Please sign in to comment.