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

Extend the capabilities of dynamic REST API serialization to incorporate downstream relations #17971

Open
jeremystretch opened this issue Nov 8, 2024 · 1 comment
Labels
complexity: high Expected to require a large amont of time and effort to implement relative to other tasks status: under review Further discussion is needed to determine this issue's scope and/or implementation type: feature Introduction of new functionality to the application

Comments

@jeremystretch
Copy link
Member

NetBox version

v4.1.6

Feature type

New functionality

Triage priority

I volunteer to perform this work (if approved)

Proposed functionality

NetBox v4.0 introduced support for dynamic serializer fields in the REST API. For example, a request for GET /api/dcim/sites/?fields=id,name,status will return only the listed fields for each site:

{
    "count": 24,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 24,
            "name": "Butler Communications",
            "status": {
                "value": "active",
                "label": "Active"
            }
        },
        {
            "id": 2,
            "name": "DM-Akron",
            "status": {
                "value": "active",
                "label": "Active"
            }
        },
        ...
    ]
}

Now that this mechanism is in place, we can extend REST API serializers to also support returning downstream-related objects (e.g. racks or devices within a site). These objects will not be part of the object representation by default, but can be enabled by inclusion in the fields query parameter as above.

For example, retrieving a list of device interfaces along with their assigned IP addresses could be achieved by requesting GET /api/dcim/interfaces/?device=123&fields=name,type,enabled,ip_addresses, which return something like the following rough mock-up:

{
    "count": 14,
    "next": null,
    "previous": null,
    "results": [
        {
            "name": "GigabitEthernet0/0/0",
            "type": {
                "value": "1000base-x-sfp",
                "label": "SFP (1GE)"
            },
            "enabled": true,
            "ip_addresses": [
                {
                    "id": 31,
                    "display": "172.16.0.1/24",
                    "family": {
                        "value": 4,
                        "label": "IPv4"
                    },
                    "address": "172.16.0.1/24",
                }
            ]
        },
    ...
    ]
}

Use case

This would provide more flexibility when consuming the REST API, affording the API client a limited ability to include immediately related objects and obviate the need (in some cases) to make additional requests.

While it should be pointed out that this FR does not seek to effectively replicate the full GraphQL API in REST form, it would likely serve many of the simpler use cases for GraphQL.

Database changes

N/A

External dependencies

N/A

@jeremystretch jeremystretch added type: feature Introduction of new functionality to the application status: under review Further discussion is needed to determine this issue's scope and/or implementation complexity: high Expected to require a large amont of time and effort to implement relative to other tasks labels Nov 8, 2024
@julianstolp
Copy link

Hi Jeremy, I really like this idea. Just a quick question, in your example would the full IP address object be returned in a similar way to /api/ipam/ip-addresses/1/ or just what would be visible in the UI? This is interesting to me as we use a lot of object-based permissions for other users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity: high Expected to require a large amont of time and effort to implement relative to other tasks status: under review Further discussion is needed to determine this issue's scope and/or implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

2 participants