diff --git a/nornir/plugins/inventory/netbox.py b/nornir/plugins/inventory/netbox.py index d23d3dc1..96d0decf 100644 --- a/nornir/plugins/inventory/netbox.py +++ b/nornir/plugins/inventory/netbox.py @@ -37,6 +37,19 @@ def __init__( if d.get("primary_ip", {}): temp["hostname"] = d["primary_ip"]["address"].split("/")[0] + # Find interface name using additional API call + i = requests.get( + "{}/api/ipam/ip-addresses/?address={}".format( + nb_url, temp["nornir_host"] + ), + headers=headers, + ) + i.raise_for_status() + i = i.json() + + # Strip extraneous information and add it to the dictionary + temp["primary_interface"] = i["results"][0]["interface"]["name"] + # Add values that don't have an option for 'slug' temp["serial"] = d["serial"] temp["vendor"] = d["device_type"]["manufacturer"]["name"] diff --git a/tests/plugins/inventory/netbox/2.3.5/mocked/address_1.json b/tests/plugins/inventory/netbox/2.3.5/mocked/address_1.json new file mode 100644 index 00000000..1c861352 --- /dev/null +++ b/tests/plugins/inventory/netbox/2.3.5/mocked/address_1.json @@ -0,0 +1,37 @@ +{ + "count": 1, + "next": null, + "previous": null, + "results": [ + { + "id": 1, + "family": 4, + "address": "10.0.1.1/32", + "vrf": null, + "tenant": null, + "status": { + "value": 1, + "label": "Active" + }, + "role": null, + "interface": { + "id": 1, + "url": "http://netbox.local:8080/api/dcim/interfaces/1/", + "device": { + "id": 1, + "url": "http://netbox.local:8080/api/dcim/devices/1/", + "name": "1-Core", + "display_name": "1-Core" + }, + "virtual_machine": null, + "name": "lo0.0" + }, + "description": "", + "nat_inside": null, + "nat_outside": null, + "custom_fields": {}, + "created": "2018-07-26", + "last_updated": "2018-07-26T06:58:35.918980Z" + } + ] +} \ No newline at end of file diff --git a/tests/plugins/inventory/netbox/2.3.5/mocked/address_2.json b/tests/plugins/inventory/netbox/2.3.5/mocked/address_2.json new file mode 100644 index 00000000..146819ca --- /dev/null +++ b/tests/plugins/inventory/netbox/2.3.5/mocked/address_2.json @@ -0,0 +1,37 @@ +{ + "count": 1, + "next": null, + "previous": null, + "results": [ + { + "id": 2, + "family": 4, + "address": "172.16.2.1/32", + "vrf": null, + "tenant": null, + "status": { + "value": 1, + "label": "Active" + }, + "role": null, + "interface": { + "id": 2, + "url": "http://netbox.local:8080/api/dcim/interfaces/2/", + "device": { + "id": 2, + "url": "http://netbox.local:8080/api/dcim/devices/2/", + "name": "2-Distribution", + "display_name": "2-Distribution" + }, + "virtual_machine": null, + "name": "lo0.0" + }, + "description": "", + "nat_inside": null, + "nat_outside": null, + "custom_fields": {}, + "created": "2018-07-26", + "last_updated": "2018-07-26T07:23:11.518980Z" + } + ] +} \ No newline at end of file diff --git a/tests/plugins/inventory/netbox/2.3.5/mocked/address_3.json b/tests/plugins/inventory/netbox/2.3.5/mocked/address_3.json new file mode 100644 index 00000000..f6c502d0 --- /dev/null +++ b/tests/plugins/inventory/netbox/2.3.5/mocked/address_3.json @@ -0,0 +1,37 @@ +{ + "count": 1, + "next": null, + "previous": null, + "results": [ + { + "id": 3, + "family": 4, + "address": "192.168.3.1/32", + "vrf": null, + "tenant": null, + "status": { + "value": 1, + "label": "Active" + }, + "role": null, + "interface": { + "id": 3, + "url": "http://netbox.local:8080/api/dcim/interfaces/3/", + "device": { + "id": 3, + "url": "http://netbox.local:8080/api/dcim/devices/3/", + "name": "3-Access", + "display_name": "3-Access" + }, + "virtual_machine": null, + "name": "Loopback0" + }, + "description": "", + "nat_inside": null, + "nat_outside": null, + "custom_fields": {}, + "created": "2018-07-26", + "last_updated": "2018-07-26T07:59:42.981450Z" + } + ] +} \ No newline at end of file