nautobot / pynautobot

Nautobot Python SDK
https://pynautobot.readthedocs.io/en/latest/index.html
Apache License 2.0
36 stars 32 forks source link

Add method to Endpoint that can directly update an object #125

Closed jamesharr closed 1 year ago

jamesharr commented 1 year ago

I'm working with Nautobot 2.0 and due to the fact that the REST API doesn't resolve related objects has pushed me into the GraphQL world. The code I'm working on needs to review a set of objects and possibly update them. Since I'm already getting all the data back from GraphQL I need, and I know exactly which fields need updating (thanks to diffsync), I'd like to update the objects without adding the unnecessary HTTP GET into the mix.

For example, the way to do it today:

  1. Query data from GraphQL
  2. Determine updates
  3. Load object using o = nb.dcim.devices.get(device_id) which results in another GET
  4. Make a o.update(...) call to call PATCH

This results in an unnecessary GET of every object I want to update. A work-around today is to manually construct a Request to PATCH the object, but this is likely error-prone. I think pynautobot can provide a better API.

Example use of such an API:

# Add a method to class Endpoint.update() that resembles Record.update()
nb.dcim.devices.update(id=..., data={"field1": "value1"})

The call would return True if the PATCH was successful and False if unsuccessful.

References: