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

feat: OBS-398 - netbox-diode-plugin: create api endpoint for applying change sets #49

Conversation

Julio-Oliveira-Encora
Copy link
Contributor

Add an endpoint to apply changes (create and update) objects in the Netbox.

Copy link

linear bot commented Feb 29, 2024

OBS-398 Create API endpoint for applying change sets

In the NetBox diode plugin, we need to create an API endpoint for change sets to process by the plugin.

POST /api/plugins/netbox_diode_plugin/apply-change-set

Diode's reconciler service will make change sets based on ingested data and existing matching NetBox object states and send a request to this API endpoint.

Payload will contain a list of changes to action with following fields:

  • change_id - [required] string containing change ID generated by diode backend during data ingestion
  • change_type - [required] string containing operation type: create, update
  • object_type - [required] string containing NetBox object type, i.e. device
  • object_id - integer containing NetBox object ID for change types other than create (i.e. we need object ID to update or delete [in the future] existing objects)
  • object_version - integer containing NetBox object's objectchange ID (see https://docs.netbox.dev/en/stable/features/change-logging/), similarly to object_id required for change types other than create
  • data - [required] an object/dict containing properties corresponding to object_type NetBox model

Example payload:

{
    "change_set_id": "<UUID-0>",
    "change_set": [
        {"change_id": "<UUID-1>", "change_type": "create", "object_version": null, "object_type": "dcim.device", "object_id": null, "data": {"name": "router01", "device_type": "ISR4321", "device_role": "undefined", "site": "undefined", "platform": "IOS 15.6", "status": "Active", ...}},
    	{"change_id": "<UUID-2>", "change_type": "update", "object_version": 1, "object_type": "dcim.device", "object_id": 123, "data": {"name": "router01", "primary_ipv4": "mgmt", ...}},
	    {"change_id": "<UUID-3>", "change_type": "create", "object_version": null, "object_type": "dcim.site", "object_id": null, "data": {"name": "undefined", ...}},
    	{"change_id": "<UUID-4>", "change_type": "create", "object_version": null, "object_type": "dcim.device_role", "object_id": null, "data": {"name": "undefined", ...}},
	    {"change_id": "<UUID-5>", "change_type": "create", "object_version": null, "object_type": "dcim.platform", "object_id": null, "data": {"name": "IOS 15.6", ...}},
     	{"change_id": "<UUID-6>", "change_type": "create", "object_version": null, "object_type": "dcim.manufacturer", "object_id": null, "data": {"name": "Cisco", ...}},
	    {"change_id": "<UUID-7>", "change_type": "create", "object_version": null, "object_type": "dcim.device_type", "object_id": null, "data": {"model": "ISR4321", "manufacturer": "Cisco", ...}}
    ]
}

Once we receive the request, we should action given change sets based on the change_type, i.e:

{"change_id": "<UUID-7>", "change_type": "create", "object_version": null, "object_type": "dcim.device", "object_id": null, "data": {"name": "router01", "device_type": "ISR4321", "device_role": "undefined", "site": "undefined", "platform": "IOS 15.6", "status": "Active", ...}}

should create a new Device NetBox object using provided data.

We should use atomic transactions and attempt to commit all changes in a set or rollback on any failure. Example use of Django's built-in transaction.atomic functionality (docs):

These change sets should be applied utilising existing corresponding NetBox models.

Note that each NetBox object type (model) might have some requirements, i.e. Device requires Site, DeviceTypeID and RoleID which we should create first as a placeholders prior to creation of Device object itself. It's not specified yet, but might provide these placeholders to create in the change set's data while being generated by reconciler service (TBC).

API endpoint should respond with with a success response or return an error.

Success:

{"change_set_id": "<UUID-0>", "result": "success"}

Error:

{"change_set_id": "<UUID-0>", "result": "failed", "errors": ["failed to create device..."]}

(response structure may change as we find it appropriate during development and testing).

This API should be unit tested appropriately.

@mfiedorowicz mfiedorowicz changed the title feat: Obs 398 [Netbox Diode Plugin] create api endpoint for applying change sets feat: OBS-398 - netbox-diode-plugin: create api endpoint for applying change sets Mar 1, 2024
Copy link
Member

@mfiedorowicz mfiedorowicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Julio-Oliveira-Encora, a few comments to think about.

Copy link
Member

@mfiedorowicz mfiedorowicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atomic transactions are working 🎉

A couple of comments on error handling/responses.

Adjusts in the error response.
Added ApplyChangeSetRequestSerializer to validate the request.
Changed "post" method in ApplyChangeSetView to use the serializer to validate the request.
Copy link
Member

@mfiedorowicz mfiedorowicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Julio-Oliveira-Encora, a few more suggestions/points to think and discuss if needed.

Replaced ChangeSetSerialiazer to ChangeSerialiazer.
Updated some variables names.
@Julio-Oliveira-Encora Julio-Oliveira-Encora merged commit b3e2be2 into develop Mar 6, 2024
14 checks passed
@Julio-Oliveira-Encora Julio-Oliveira-Encora deleted the obs-398-create-api-endpoint-for-applying-change-sets branch March 6, 2024 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants