Ok, that makes sense, thanks for the info. Seems like a bit more work than I expected, so I'll retract my offer to contribute to this (for now, at least). This isn't a need-to-have for us but would definitely make it more seamless to integrate NAV with network platforms managing their own inventories.

--
Med vennlig hilsen
Håvard Ose Nordstrand
Overingeniør – Faggruppe Nett
NTNU IT, Drift


Fra: Morten Brekkevold <morten.brekkevold@sikt.no>
Sendt: tirsdag 8. august 2023 14:35
Til: Håvard Ose Nordstrand <havard.o.nordstrand@ntnu.no>
Kopi: nav-users@lister.sikt.no <nav-users@lister.sikt.no>
Emne: Re: [Nav-users] Setting device group for a device through API
 
On Mon, 7 Aug 2023 09:18:51 +0000 (1 day, 3 hours, 16 minutes ago) Håvard Ose Nordstrand <havard.o.nordstrand@ntnu.no> wrote:

> Hi! We're using a Python script to import devices from a Cisco DNA inventory,
> but I can't seem to find a way to set a device group for the devices through
> the API. I'm using PATCH with the "/api/1/netbox/{netbox_id}/" endpoint, but as
> far as I can understand from the source code, it only supports certain special
> attributes like "organizationid", "roomid" etc. and not arbitrary device
> attributes.

The endpoint should support modification of all Netbox attributes that
aren't reserved for internal use by NAV.

> Is this implemented?

No, as device groups aren't an attribute of the Netbox model.  Device
groups (or rather, `NetboxGroup`, as the model is named internally) have
a many-to-many relationship with the `Netbox` model, but the `netbox`
API endpoint provides the meta-attribute `groups` that is an
auto-generated/computed read-only attribute.

> If not, would it be easy for me to implement a new API endpoint
> attribute for this or would this tie into a lot of other things?

The API currently lacks an endpoint for manipulating even groups
themselves. `netboxgroup` would be the natural name for an endpoint
dealing with the groups themselves.  Implementing this should be rather
straightforward and standalone.

The question is what would be the best way to manipulate actual
memberships through the API.  IMHO, there are at least two ways to go
about it:

1. Add a new endpoint to manipulate the `NetboxCategory` model (the
   M2M intermediary table that maps memberships in groups) directly.

or

2. Alter the `netbox` endpoint to handle patch operations on the
   computed `groups` attribute.  Conversely, the `netboxgroup` endpoint
   should be implemented and have a similarly writable `netboxes` field
   in its API to list the members of each group.

None of this should have any impact on the API version as long as
existing formats and operations are changed in a non-compatible way.


--
Best regards
Morten Brekkevold
Senior engineer, The Data and Infrastructure Division

Phone: (+47) 735 57 802

Sikt – Norwegian Agency for Shared Services in Education and Research
www.sikt.no