netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.93k stars 2.56k forks source link

Expose device role in /dcim/interfaces/ connected_endpoints #3413

Closed n3wtype closed 5 years ago

n3wtype commented 5 years ago

Environment

Proposed Functionality

Expose device role in /dcim/interfaces/ connected_endpoints

now it looks like

{
  "count": 2982,
  "next": "https://<url>/api/dcim/interfaces/?limit=1000&offset=1000",
  "previous": null,
  "results": [
    {
      "id": 1352,
      "device": {
        "id": 60,
        "url": "https://<url>/api/dcim/devices/60/",
        "name": "sw1",
        "display_name": "sw1"
      },
      "name": "et-0/0/0",
      "type": {
        "value": 1600,
        "label": "QSFP28 (100GE)"
      },
      "form_factor": {
        "value": 1600,
        "label": "QSFP28 (100GE)"
      },
      "enabled": true,
      "lag": null,
      "mtu": null,
      "mac_address": null,
      "mgmt_only": false,
      "description": "",
      "connected_endpoint_type": "dcim.interface",
      "connected_endpoint": {
        "id": 1045,
        "url": "https://<url>/api/dcim/interfaces/1045/",
        "device": {
          "id": 52,
          "url": "https://<url>/api/dcim/devices/52/",
          "name": "vps1",
          "display_name": "vps1"
        },
        "name": "et-0/0/48",
        "cable": 250,
        "connection_status": {
          "value": true,
          "label": "Connected"
        }
      },
      "connection_status": {
        "value": true,
        "label": "Connected"
      },
      "cable": {
        "id": 250,
        "url": "https://<url>/api/dcim/cables/250/",
        "label": ""
      },
      "mode": null,
      "untagged_vlan": null,
      "tagged_vlans": [],
      "tags": [],
      "count_ipaddresses": 0
    }
}

after a change

{
  "count": 2982,
  "next": "https://<url>/api/dcim/interfaces/?limit=1000&offset=1000",
  "previous": null,
  "results": [
    {
      "id": 1352,
      "device": {
        "id": 60,
        "url": "https://<url>/api/dcim/devices/60/",
        "name": "sw1",
        "display_name": "sw1"
      },
      "name": "xe-0/0/0",
      "type": {
        "value": 1600,
        "label": "QSFP28 (100GE)"
      },
      "form_factor": {
        "value": 1600,
        "label": "QSFP28 (100GE)"
      },
      "enabled": true,
      "lag": null,
      "mtu": null,
      "mac_address": null,
      "mgmt_only": false,
      "description": "",
      "connected_endpoint_type": "dcim.interface",
      "connected_endpoint": {
        "id": 1045,
        "url": "https://<url>/api/dcim/interfaces/1045/",
        "device": {
          "id": 52,
          "url": "https://<url>/api/dcim/devices/52/",
          "name": "vps1",
          "display_name": "vps1"
          "device_role":  {
              "name": "vps"
          }
        },
        "name": "et-0/0/48",
        "cable": 250,
        "connection_status": {
          "value": true,
          "label": "Connected"
        }
      },
      "connection_status": {
        "value": true,
        "label": "Connected"
      },
      "cable": {
        "id": 250,
        "url": "https://<url>/api/dcim/cables/250/",
        "label": ""
      },
      "mode": null,
      "untagged_vlan": null,
      "tagged_vlans": [],
      "tags": [],
      "count_ipaddresses": 0
    }
}

Use Case

Let's say that I have an access switch and bunch of servers connected to it within netbox. Based on device_role.name of connected device I would like to do some automated configuration of access port. To do that I fetch a list of interfaces of this switch and look within connected_endpoint section. Currently device_role.name is not exposed by this api. So get this information I need to do additional API calls. Imagine I have 100+ switches with 48 ports connected to different devices. It make a lot of additional API calls. It would be lot simpler connected_endpoint section would provide this information right away. It would make automation based on data from netbox easier. Making automation decision based on data from netbox keeps netbox consistent with actual configuration and for bigger environments it could be reasonable to provide some more data right away to avoid massive amount of additional API calls.

Database Changes

As far as I'm aware no additional schema change is required. The only required change is probably additional JOIN in orm to fetch device_role.name for each device.

External Dependencies

n/a

jeremystretch commented 5 years ago

This would introduce excessive overhead for the request. It's the same reason the response doesn't include the connected device's site, rack, etc. You'll need to make a separate API request for the device if you need that information.