Unable to set / update primary_ip4 on virtual machine per API PATCH request #1929

Closed
opened 2025-12-29 17:20:39 +01:00 by adam · 3 comments
Owner

Originally created by @hensoko on GitHub (Aug 9, 2018).

Hi,

we're experiencing some issues with setting primary IPs on virtual machines via Netbox API (PATCH request). We're running Netbox 2.4.2 (even happened with 2.4.0 / 2.4.1) in a Debian 8 Environment with Python 3.5.4 / PostgreSQL 10.3 and Nginx 1.12.2 as reverse proxy. I found thread on the mailing list describing excatly the same issue: https://groups.google.com/forum/#!topic/netbox-discuss/bqLY8cyU2ns

Environment

  • Python version: 3.5.4
  • NetBox version: 2.4.2

Steps to Reproduce

  • Create a virtual machine
  • curl -i -sSL -H "Authorization: Token <token>" -H "Content-Type: application/json" -H "Accept: application/json" -X PATCH -d '{"primary_ip4":12341}' https://<netbox_url>/api/virtualization/virtual-machines/1231/

Expected Behavior

The primary ip address gets set / updated

Actual behaviour

HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Thu, 09 Aug 2018 10:35:02 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 1890
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
API-Version: 2.4
Vary: Cookie, Origin

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Server Error</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
    <meta charset="UTF-8">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-danger" style="margin-top: 200px">
                    <div class="panel-heading">
                        <strong>
                            <i class="fa fa-warning"></i>
                            Server Error
                        </strong>
                    </div>
                    <div class="panel-body">
                        
                            <p>
                                There was a problem with your request. Please contact an administrator.
                            </p>
                        
                        <hr />
                        <p>
                            The complete exception is provided below:
                        </p>
<pre><strong>&lt;class &#39;django.db.utils.IntegrityError&#39;&gt;</strong><br />
duplicate key value violates unique constraint &quot;virtualization_virtualmachine_primary_ip4_id_key&quot;
DETAIL:  Key (primary_ip4_id)=(12341) already exists.
</pre>
                        <p>
                            If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>.
                        </p>
                        <div class="text-right">
                            <a href="/" class="btn btn-primary">Home Page</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Originally created by @hensoko on GitHub (Aug 9, 2018). Hi, we're experiencing some issues with setting primary IPs on virtual machines via Netbox API (PATCH request). We're running Netbox 2.4.2 (even happened with 2.4.0 / 2.4.1) in a Debian 8 Environment with Python 3.5.4 / PostgreSQL 10.3 and Nginx 1.12.2 as reverse proxy. I found thread on the mailing list describing excatly the same issue: https://groups.google.com/forum/#!topic/netbox-discuss/bqLY8cyU2ns ### Environment * Python version: 3.5.4 * NetBox version: 2.4.2 ### Steps to Reproduce * Create a virtual machine * `curl -i -sSL -H "Authorization: Token <token>" -H "Content-Type: application/json" -H "Accept: application/json" -X PATCH -d '{"primary_ip4":12341}' https://<netbox_url>/api/virtualization/virtual-machines/1231/` ### Expected Behavior The primary ip address gets set / updated ### Actual behaviour ``` HTTP/1.1 500 Internal Server Error Server: nginx Date: Thu, 09 Aug 2018 10:35:02 GMT Content-Type: text/html; charset=utf-8 Content-Length: 1890 Connection: keep-alive X-Frame-Options: SAMEORIGIN API-Version: 2.4 Vary: Cookie, Origin <!DOCTYPE html> <html lang="en"> <head> <title>Server Error</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css"> <meta charset="UTF-8"> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="panel panel-danger" style="margin-top: 200px"> <div class="panel-heading"> <strong> <i class="fa fa-warning"></i> Server Error </strong> </div> <div class="panel-body"> <p> There was a problem with your request. Please contact an administrator. </p> <hr /> <p> The complete exception is provided below: </p> <pre><strong>&lt;class &#39;django.db.utils.IntegrityError&#39;&gt;</strong><br /> duplicate key value violates unique constraint &quot;virtualization_virtualmachine_primary_ip4_id_key&quot; DETAIL: Key (primary_ip4_id)=(12341) already exists. </pre> <p> If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>. </p> <div class="text-right"> <a href="/" class="btn btn-primary">Home Page</a> </div> </div> </div> </div> </div> </div> </body> </html> ```
adam added the type: bugstatus: accepted labels 2025-12-29 17:20:39 +01:00
adam closed this issue 2025-12-29 17:20:39 +01:00
Author
Owner

@jeremystretch commented on GitHub (Aug 9, 2018):

The IntegrityError indicates that this IP address is already assigned as the primary IP for a different VM: It can't also be assigned to this one. However, this should return a validation error rather than an exception.

It seems that the serializer isn't validating the OneToOneField properly. I'll need to dig into this more.

@jeremystretch commented on GitHub (Aug 9, 2018): The IntegrityError indicates that this IP address is already assigned as the primary IP for a different VM: It can't also be assigned to this one. However, this should return a validation error rather than an exception. It seems that the serializer isn't validating the OneToOneField properly. I'll need to dig into this more.
Author
Owner

@hensoko commented on GitHub (Aug 9, 2018):

Hey,

thanks for the quick reply. I just checked it in the database. Actually the IP-Address is set as primary on a different virtual machine where it is not assigend to an interface. So this seems to be the reason for the database error but I think it shouldn't be possible to set an ip address as primary when this ip address isn't actually assigned to an interface of this device / virtual machine, right?

Besides that: thanks for the great work so far, we're first-time users of netbox and very happy about it and the progress it made until today :).

Regards

Hendrik

@hensoko commented on GitHub (Aug 9, 2018): Hey, thanks for the quick reply. I just checked it in the database. Actually the IP-Address is set as primary on a different virtual machine where it is not assigend to an interface. So this seems to be the reason for the database error but I think it shouldn't be possible to set an ip address as primary when this ip address isn't actually assigned to an interface of this device / virtual machine, right? Besides that: thanks for the great work so far, we're first-time users of netbox and very happy about it and the progress it made until today :). Regards Hendrik
Author
Owner

@hensoko commented on GitHub (Aug 12, 2018):

Hey @jeremystretch,

thanks for fixing this. But I still have the occurence that if an IP-Address is reassigned to another VM the primay IP configuration of the former VM does not get removed.

I guess the primary IP assignment should be removed from the VM in the moment the actual IP-Address is no longer assigned to this perticular VM.

Do you agree?

@hensoko commented on GitHub (Aug 12, 2018): Hey @jeremystretch, thanks for fixing this. But I still have the occurence that if an IP-Address is reassigned to another VM the primay IP configuration of the former VM does not get removed. I guess the primary IP assignment should be removed from the VM in the moment the actual IP-Address is no longer assigned to this perticular VM. Do you agree?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1929