all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: "Trygve Laugstøl via pve-devel" <pve-devel@lists.proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: "Trygve Laugstøl" <trygvis@inamo.no>
Subject: [pve-devel] [PATCH pve-network 1/1] fix #6569: ipam: netbox: better prefix lookup
Date: Wed, 30 Jul 2025 23:26:14 +0200	[thread overview]
Message-ID: <mailman.418.1753911405.367.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20250730212614.1264010-1-trygvis@inamo.no>

[-- Attachment #1: Type: message/rfc822, Size: 8295 bytes --]

From: "Trygve Laugstøl" <trygvis@inamo.no>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-network 1/1] fix #6569: ipam: netbox: better prefix lookup
Date: Wed, 30 Jul 2025 23:26:14 +0200
Message-ID: <20250730212614.1264010-2-trygvis@inamo.no>

The problem description in #6569 is correct, but instead of depending on the
freetext query parameter "q", this uses the "prefix" parameter for an explicit
lookup.

This also checks if there are multiple prefixes that matched. This will happen
if the same prefix is registered in multiple VRFs.

Signed-off-by: Trygve Laugstøl <trygvis@inamo.no>
---
 src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index e118d03..3799e47 100644
--- a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
@@ -423,18 +423,25 @@ sub on_update_hook {
 sub get_prefix_id {
     my ($config, $cidr, $noerr) = @_;
 
-    # we need to supply any IP inside the prefix, without supplying the mask, so
-    # just take the one from the cidr
-    my ($ip, undef) = split(/\//, $cidr);
-
-    my $result = eval { netbox_api_request($config, "GET", "/ipam/prefixes/?q=$ip") };
+    # look up the prefix by matching the prefix exactly.
+    my $result = eval { netbox_api_request($config, "GET", "/ipam/prefixes/?prefix=$cidr") };
     if ($@) {
         return if $noerr;
         die "could not obtain ID for prefix $cidr: $@";
     }
 
-    my $data = @{ $result->{results} }[0];
-    return $data->{id};
+    # we can get multiple prefixes returned if the netbox configuration allows
+    # it, or if the prefix is registered in different VRFs.
+    my $count = $result->{count} || 0;
+    if ($count > 1) {
+        die "ambiguous prefix lookup for $cidr: found $count matches";
+    }
+
+    if ($count == 0) {
+        return;
+    }
+
+    return $result->{results}[0]{id};
 }
 
 sub get_iprange_id {
-- 
2.47.2



[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

       reply	other threads:[~2025-07-30 21:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250730212614.1264010-1-trygvis@inamo.no>
2025-07-30 21:26 ` Trygve Laugstøl via pve-devel [this message]
2025-09-17 13:15   ` Fiona Ebner
2025-09-17 18:03     ` Trygve Laugstøl via pve-devel
     [not found]     ` <b77fd4bf-7045-47c4-bed8-5d450cb61187@app.fastmail.com>
2025-09-18 10:07       ` Fiona Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mailman.418.1753911405.367.pve-devel@lists.proxmox.com \
    --to=pve-devel@lists.proxmox.com \
    --cc=trygvis@inamo.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal