From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 242291FF15E
	for <inbox@lore.proxmox.com>; Tue,  8 Apr 2025 15:25:56 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id A7F0A1E396;
	Tue,  8 Apr 2025 15:25:52 +0200 (CEST)
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue,  8 Apr 2025 15:25:17 +0200
Message-Id: <20250408132517.225240-1-s.hanreich@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.232 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery
 methods
 RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_NONE                0.001 SPF: sender does not publish an SPF Record
Subject: [pve-devel] [PATCH pve-network 1/1] ipam: netbox: fix adding vms
 without a name set in the options
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

The hostname provided to the methods in the IPAM plugins is empty for
VMs that do not have a name set. We always included the dns_name key
in the request, even if it was undefined, which leads Netbox to return
a 400 Bad Request error. With this patch we omit the dns_name
parameter if it isn't set, so the call succeeds.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 36 +++++++++++++++--------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index a691323..97a0c3f 100644
--- a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
@@ -206,11 +206,14 @@ sub add_ip {
     }
 
     eval {
-	netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", {
+	my $params = {
 	    address => "$ip/$mask",
-	    dns_name => $hostname,
 	    description => $description,
-	});
+	};
+
+	$params->{dns_name} = $hostname if $hostname;
+
+	netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", $params);
     };
 
     if ($@) {
@@ -244,11 +247,14 @@ sub update_ip {
     }
 
     eval {
-	netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", {
+	my $params = {
 	    address => "$ip/$mask",
-	    dns_name => $hostname,
 	    description => $description,
-	});
+	};
+
+	$params->{dns_name} = $hostname if $hostname;
+
+	netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", $params);
     };
     if ($@) {
 	die "error update ip $ip : $@" if !$noerr;
@@ -272,10 +278,13 @@ sub add_next_freeip {
     $description = "mac:$mac" if $mac;
 
     my $ip = eval {
-	my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", {
-	    dns_name => $hostname,
+	my $params = {
 	    description => $description,
-	});
+	};
+
+	$params->{dns_name} = $hostname if $hostname;
+
+	my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", $params);
 
 	my ($ip, undef) = split(/\//, $result->{address});
 	return $ip;
@@ -303,10 +312,13 @@ sub add_range_next_freeip {
     $description = "mac:$data->{mac}" if $data->{mac};
 
     my $ip = eval {
-	my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", {
-	    dns_name => $data->{hostname},
+	my $params = {
 	    description => $description,
-	});
+	};
+
+	$params->{dns_name} = $data->{hostname} if $data->{hostname};
+
+	my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", $params);
 
 	my ($ip, undef) = split(/\//, $result->{address});
 	print "found ip free $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip;
-- 
2.39.5


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