netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.16k stars 2.59k forks source link

Can not rename device interface templates through API - module_type, name must make a unique set #16466

Closed rozgonik closed 4 months ago

rozgonik commented 5 months ago

Deployment Type

Self-hosted

NetBox Version

v4.0.5 - netbox-docker

Python Version

3.11

Steps to Reproduce

Netbox is running as a docker container with postgres12. The behaviour can be reproduced in the demo environment too.

Preparation:

  1. Create 2 arbitrary device types. Eg: asd and qwe
  2. Create an interface template ge1 for the first device type with arbitrary parameters
  3. Create an interface template ge2 for the second device type with arbitrary parameters
  4. Get the id of the second interface template

Try to rename the second interface template to ge1 through the API (I have used the swagger "Try it out" feature): curl -X 'PATCH' 'https://demo.netbox.dev/api/dcim/interface-templates/305/' [...] -d '{"name": "ge1"}'

Expected Behavior

The interface template is successfully renamed, the PATCH call returning HTTP 200.

Observed Behavior

HTTP 400 returned with body:

{
  "non_field_errors": [
    "The fields module_type, name must make a unique set."
  ]
}
arthanson commented 4 months ago

@rozgonik can you please re-check the repo steps as it looks like it is working for me using the steps your provided.

rozgonik commented 4 months ago

@arthanson I can repro it just fine, but here are the repro steps 100% through API (repro steps above are a mixed gui-api approach, maybe that was misleading, sorry for the confusion this may have caused): (FTR: Also truncated the curl calls for the sake of clarity. The missing bits are the accept, Content-Type and X-CSRFTOKEN headers.)

  1. Make sure we have a known manufacturer (with slug idk). (This step was absent above because I have used a manufacturer already available): curl -X 'POST' 'https://demo.netbox.dev/api/dcim/manufacturers/' [...] -d '{"name": "idk", "slug": "idk"}'

  2. Create 2 arbitrary device types with name asd and qwe: curl -X 'POST' 'https://demo.netbox.dev/api/dcim/device-types/' [...] -d '{"manufacturer": {"slug": "idk"}, "model": "asd", "slug": "asd"}' curl -X 'POST' 'https://demo.netbox.dev/api/dcim/device-types/' [...] -d '{"manufacturer": {"slug": "idk"}, "model": "qwe", "slug": "qwe"}'

  3. Create an interface template ge1 for the first device type (asd) with arbitrary parameters curl -X 'POST' 'https://demo.netbox.dev/api/dcim/interface-templates/' [...] -d '{"device_type": {"slug": "asd"}, "name": "ge1", "type": "virtual"}'

  4. Create an interface template ge2 for the second device type (qwe) with arbitrary parameters curl -X 'POST' 'https://demo.netbox.dev/api/dcim/interface-templates/' [...] -d '{"device_type": {"slug": "qwe"}, "name": "ge2", "type": "virtual"}'

  5. Get the id of the second interface template (ge2) Get it from the last curl call's output. In my case its 304.

Try to rename the second interface template (ge2) to ge1 through the API using the id from step 4 (please note the id usage on the end of the url): curl -X 'PATCH' 'https://demo.netbox.dev/api/dcim/interface-templates/304/' [...] -d '{"name": "ge1"}'

arthanson commented 4 months ago

@rozgonik I followed your steps but it is still working fine for me (tried against my local install) See commands / output below using your curl commands. For the step-4 my ID returned was 302 which I used.

╭─    ~/dev/work/netbox    15348-quick_access_saved_filters ?1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ✔  3.11.9   11:25:09 AM 
╰─ curl -X 'POST' 'http://127.0.0.1:8000/api/dcim/device-types/' -H "authorization: Token 58c19c4dc7918300483c6677f242cdcfe7344148" -H 'accept: application/json' -H 'Content-Type: application/json'  -d '{"manufacturer": {"slug": "idk"}, "model": "asd", "slug": "asd"}'
{"id":15,"url":"http://127.0.0.1:8000/api/dcim/device-types/15/","display":"asd","manufacturer":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/manufacturers/15/","display":"idk","name":"idk","slug":"idk","description":""},"default_platform":null,"model":"asd","slug":"asd","part_number":"","u_height":1.0,"exclude_from_utilization":false,"is_full_depth":true,"subdevice_role":null,"airflow":null,"weight":null,"weight_unit":null,"front_image":null,"rear_image":null,"description":"","comments":"","tags":[],"custom_fields":{},"created":"2024-06-18T18:25:23.435861Z","last_updated":"2024-06-18T18:25:23.435880Z","console_port_template_count":0,"console_server_port_template_count":0,"power_port_template_count":0,"power_outlet_template_count":0,"interface_template_count":0,"front_port_template_count":0,"rear_port_template_count":0,"device_bay_template_count":0,"module_bay_template_count":0,"inventory_item_template_count":0}%

╭─    ~/dev/work/netbox    15348-quick_access_saved_filters ?1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ✔  3.11.9   11:25:23 AM 
╰─ curl -X 'POST' 'http://127.0.0.1:8000/api/dcim/device-types/' -H "authorization: Token 58c19c4dc7918300483c6677f242cdcfe7344148" -H 'accept: application/json' -H 'Content-Type: application/json'  -d '{"manufacturer": {"slug": "idk"}, "model": "qwe", "slug": "qwe"}'
{"id":16,"url":"http://127.0.0.1:8000/api/dcim/device-types/16/","display":"qwe","manufacturer":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/manufacturers/15/","display":"idk","name":"idk","slug":"idk","description":""},"default_platform":null,"model":"qwe","slug":"qwe","part_number":"","u_height":1.0,"exclude_from_utilization":false,"is_full_depth":true,"subdevice_role":null,"airflow":null,"weight":null,"weight_unit":null,"front_image":null,"rear_image":null,"description":"","comments":"","tags":[],"custom_fields":{},"created":"2024-06-18T18:25:34.905452Z","last_updated":"2024-06-18T18:25:34.905468Z","console_port_template_count":0,"console_server_port_template_count":0,"power_port_template_count":0,"power_outlet_template_count":0,"interface_template_count":0,"front_port_template_count":0,"rear_port_template_count":0,"device_bay_template_count":0,"module_bay_template_count":0,"inventory_item_template_count":0}%

╭─    ~/dev/work/netbox    15348-quick_access_saved_filters ?1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ✔  3.11.9   11:25:35 AM 
╰─ curl -X 'POST' 'http://127.0.0.1:8000/api/dcim/interface-templates/' -H "authorization: Token 58c19c4dc7918300483c6677f242cdcfe7344148" -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"device_type": {"slug": "asd"}, "name": "ge1", "type": "virtual"}'
{"id":301,"url":"http://127.0.0.1:8000/api/dcim/interface-templates/301/","display":"ge1","device_type":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/device-types/15/","display":"asd","manufacturer":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/manufacturers/15/","display":"idk","name":"idk","slug":"idk","description":""},"model":"asd","slug":"asd","description":""},"module_type":null,"name":"ge1","label":"","type":{"value":"virtual","label":"Virtual"},"enabled":true,"mgmt_only":false,"description":"","bridge":null,"poe_mode":null,"poe_type":null,"rf_role":null,"created":"2024-06-18T18:25:48.970748Z","last_updated":"2024-06-18T18:25:48.970761Z"}%

╭─    ~/dev/work/netbox    15348-quick_access_saved_filters ?1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ✔  3.11.9   11:25:49 AM 
╰─ curl -X 'POST' 'http://127.0.0.1:8000/api/dcim/interface-templates/' -H "authorization: Token 58c19c4dc7918300483c6677f242cdcfe7344148" -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"device_type": {"slug": "qwe"}, "name": "ge2", "type": "virtual"}'
{"id":302,"url":"http://127.0.0.1:8000/api/dcim/interface-templates/302/","display":"ge2","device_type":{"id":16,"url":"http://127.0.0.1:8000/api/dcim/device-types/16/","display":"qwe","manufacturer":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/manufacturers/15/","display":"idk","name":"idk","slug":"idk","description":""},"model":"qwe","slug":"qwe","description":""},"module_type":null,"name":"ge2","label":"","type":{"value":"virtual","label":"Virtual"},"enabled":true,"mgmt_only":false,"description":"","bridge":null,"poe_mode":null,"poe_type":null,"rf_role":null,"created":"2024-06-18T18:26:04.096390Z","last_updated":"2024-06-18T18:26:04.096404Z"}%

╭─    ~/dev/work/netbox    15348-quick_access_saved_filters ?1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ✔  3.11.9   11:26:04 AM 
╰─ curl -X 'PATCH' 'http://127.0.0.1:8000/api/dcim/interface-templates/302/' -H "authorization: Token 58c19c4dc7918300483c6677f242cdcfe7344148" -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"name": "ge1"}'
{"id":302,"url":"http://127.0.0.1:8000/api/dcim/interface-templates/302/","display":"ge1","device_type":{"id":16,"url":"http://127.0.0.1:8000/api/dcim/device-types/16/","display":"qwe","manufacturer":{"id":15,"url":"http://127.0.0.1:8000/api/dcim/manufacturers/15/","display":"idk","name":"idk","slug":"idk","description":""},"model":"qwe","slug":"qwe","description":""},"module_type":null,"name":"ge1","label":"","type":{"value":"virtual","label":"Virtual"},"enabled":true,"mgmt_only":false,"description":"","bridge":null,"poe_mode":null,"poe_type":null,"rf_role":null,"created":"2024-06-18T18:26:04.096390Z","last_updated":"2024-06-18T18:28:14.266486Z"}%
rozgonik commented 4 months ago

@arthanson I can still reproduce it on a fresh source install from the develop branch (commit 7a5e8a8).

curl -X 'PATCH' 'http://127.0.0.1:8000/api/dcim/interface-templates/2/' -H "authorization: Token 426b45f17bd999bfe0abba46280c0bc195b1b353" -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"name": "ge1"}' {"non_field_errors":["The fields module_type, name must make a unique set."]}

Please make sure your dependencies are correctly installed as I found out that downgrading djangorestframework to 3.14.0 from 3.15.1 makes the error go away.

Update: It seems like djangorestframework update happened in commit 3be3bbe

rozgonik commented 4 months ago

I think this drf issue may be related: https://github.com/encode/django-rest-framework/issues/9410

github-actions[bot] commented 4 months ago

This is a reminder that additional information is needed in order to further triage this issue. If the requested details are not provided, the issue will soon be closed automatically.

github-actions[bot] commented 4 months ago

This issue is being closed as no further information has been provided. If you would like to revisit this topic, please first modify your original post to include all the requested detail, and then ask that the issue be reopened.

rozgonik commented 4 months ago

Can someone please reopen it?

What details can I provide to help on reproducing this issue? One clue, its important to use a Netbox installation corresponding to the official docs with all the correct requirement versions installed as stated above. Also v4.0.6 is affected too.

FrondorM commented 3 months ago

I can confirm that the issue is also still present in v4.0.7.

And it doesn't only apply to device interface templates, but also to device console port templates.

It seems the unique name check isn't done on the device itself as it should, but on the entire template database.

Edit: to be clear: I use the swagger "Try it out" feature