nautobot / pynautobot

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

Add notes support for all models that support notes #159

Closed whitej6 closed 1 day ago

whitej6 commented 7 months ago

Improve notes support

Current State

API does not have a "notes" attr in the return payload and only returns "notes_url" as a link to a list view for all notes that apply to the current instance.

Proposal

Provide ".notes" on any model that provides notes and the attribute would be an pynautobot.core.endpoint.Endpoint to allow for create update and delete operations scoped to just the current model instance

Example desired outcome

dev = py_nb.dcim.devices.get(name="foo")
count(dev.notes.all()) # return N notes created on a device named foo
note = dev.notes.create(note="bar")
note.note="baz"
note.save()

count(dev.notes.all()) # return being N+1 of previous count
jvanderaa commented 7 months ago

Working on checking some things out on this. But first look at using demo.nautobot.com as a reference, there is a notes API endpoint: /extras/notes.

There is an assigned_object_id reference on the GET reference that would be used to get the notes for the object.

jvanderaa commented 7 months ago

So what we are looking at really is a convenience function to help things out, rather than anything is not usable today right?

jvanderaa commented 7 months ago

I support the convenience methods as long as it is not something that is only available to the SDK, in that it leverages the current SDK framework.

whitej6 commented 7 months ago

It's a convenience method to match API functionality. When using /api/dcim/devices/<uuid>/notes/ you can post with just {"note": "some note"} without having to account for generic foreign key etc. Also the same endpoint will return a list of all notes for just the one device.

tsm1th commented 3 days ago

Those routes that extend beyond the traditional endpoints are currently handled with the DetailEndpoint class and the routes must be explicitly defined on the model needing them. This can easily be implemented for the Device model using that method and results in the following after testing:

dev = py_nb.dcim.devices.get(name="foo")
len(dev.notes.list()) # return N notes created on a device named foo
dev.notes.create({"note": "bar"}) # pass dictionary of data to create
len(dev.notes.list()) # return being N+1 of previous count

Extending that to all the models (that support notes) would be a heavier lift and require more work.