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 140371FF16B
	for <inbox@lore.proxmox.com>; Thu, 12 Dec 2024 01:04:49 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3E09310F84;
	Thu, 12 Dec 2024 01:04:52 +0100 (CET)
To: pve-devel@lists.proxmox.com
Date: Thu, 12 Dec 2024 06:38:04 +0700
MIME-Version: 1.0
Message-ID: <mailman.147.1733961890.332.pve-devel@lists.proxmox.com>
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Post: <mailto:pve-devel@lists.proxmox.com>
From: nurohman via pve-devel <pve-devel@lists.proxmox.com>
Precedence: list
Cc: nurohman <me@nurohman.com>
X-Mailman-Version: 2.1.29
X-BeenThere: pve-devel@lists.proxmox.com
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
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/>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
Subject: [pve-devel] [PATCH pve-network] Fix #5496: NetBox add ip range
Content-Type: multipart/mixed; boundary="===============2850319848036294456=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============2850319848036294456==
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <bounce+bdf587.9db3643-pve-devel=lists.proxmox.com@mailsrv.nurohman.com>
X-Original-To: pve-devel@lists.proxmox.com
Delivered-To: pve-devel@lists.proxmox.com
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by lists.proxmox.com (Postfix) with UTF8SMTPS id 8860CC239B
	for <pve-devel@lists.proxmox.com>; Thu, 12 Dec 2024 01:04:50 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with UTF8SMTP id 61D1A10ED3
	for <pve-devel@lists.proxmox.com>; Thu, 12 Dec 2024 01:04:20 +0100 (CET)
Received: from m42-7.mailgun.net (m42-7.mailgun.net [69.72.42.7])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with UTF8SMTPS
	for <pve-devel@lists.proxmox.com>; Thu, 12 Dec 2024 01:04:18 +0100 (CET)
DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mailsrv.nurohman.com; q=dns/txt; s=krs; t=1733961857; x=1733969057;
 h=Content-Transfer-Encoding: MIME-Version: Message-ID: Date: Subject: Subject: Cc: To: To: From: From: Sender: Sender;
 bh=1ejmQ2Cm35QsaGre8fS9RaxBEb6unGNphBaFhHdu87k=;
 b=daXCJ1yZAQAuO01trUegfp8MLynWvEOQMTA2wbVj7ysvZi4l8n8QmR9H+P0vUgCsAYVmcfZhHzBvZIaDyb5riXcvtIb1RyogSdyZMLkFFSXFVXv5fo6Sv92ZtJqS6m6Pyx6+bdCl/P2et8j7GY920+vh2oOx5ZuUNH2kGGXVSWI=
X-Mailgun-Sending-Ip: 69.72.42.7
X-Mailgun-Sending-Ip-Pool-Name: 
X-Mailgun-Sending-Ip-Pool: 
X-Mailgun-Sid: WyIwZTU2ZCIsInB2ZS1kZXZlbEBsaXN0cy5wcm94bW94LmNvbSIsIjlkYjM2NDMiXQ==
Received: from eva.nurohman.com (unknown [157.15.83.42]) by 4520c402cadd with SMTP id
 675a22963b3f1281d70f6e8b (version=TLS1.3, cipher=TLS_AES_128_GCM_SHA256);
 Wed, 11 Dec 2024 23:39:02 GMT
Sender: me=nurohman.com@mailsrv.nurohman.com
From: nurohman <me@nurohman.com>
To: pve-devel@lists.proxmox.com
Cc: nurohman <me@nurohman.com>
Subject: [PATCH pve-network] Fix #5496: NetBox add ip range
Date: Thu, 12 Dec 2024 06:38:04 +0700
Message-ID: <20241211233832.806187-1-me@nurohman.com>
X-Mailer: git-send-email 2.47.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	DKIM_SIGNED               0.1 Message has a DKIM or DK signature, not necessarily valid
	DKIM_VALID               -0.1 Message has at least one valid DKIM or DK signature
	DKIM_VALID_EF            -0.1 Message has a valid DKIM or DK signature from envelope-from domain
	DMARC_PASS               -0.1 DMARC pass policy
	RCVD_IN_DNSWL_NONE     -0.0001 Sender listed at https://www.dnswl.org/, no trust
	RCVD_IN_MSPIKE_H2      -0.001 Average reputation (+2)
	RCVD_IN_VALIDITY_CERTIFIED_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	RCVD_IN_VALIDITY_RPBL_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	RCVD_IN_VALIDITY_SAFE_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
	SPF_PASS               -0.001 SPF: sender matches SPF record

when ip range not available in the NetBox, function response "can't find free ip in range". 
Add new ip range if can't find ip range ID and response new ip range ID.  

Co-authored-by: Jacob Green <jacobgreen at corporatetools.com>
Co-authored-by: Lou Lecrivain <lou.lecrivain at wdz.de>
Signed-off-by: Nurohman <me at nurohman.com>
---
 src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 34 +++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index d923269..5591b0b 100644
--- a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
@@ -151,7 +151,7 @@ sub add_next_freeip {
 
     my $params = { dns_name => $hostname, description => $description };
 
-    eval {
+    my $ip = eval {
 	my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
 	my ($ip, undef) = split(/\//, $result->{address});
 	return $ip;
@@ -160,6 +160,8 @@ sub add_next_freeip {
     if ($@) {
 	die "can't find free ip in subnet $cidr: $@" if !$noerr;
     }
+
+    return $ip;
 }
 
 sub add_range_next_freeip {
@@ -170,11 +172,14 @@ sub add_range_next_freeip {
     my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
 
     my $internalid = get_iprange_id($url, $range, $headers);
+    if ($internalid eq "") {
+	$internalid = add_iprange($url, $range, $subnet, $headers);
+    }
     my $description = "mac:$data->{mac}" if $data->{mac};
 
     my $params = { dns_name => $data->{hostname}, description => $description };
 
-    eval {
+    my $ip = eval {
 	my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/ip-ranges/$internalid/available-ips/", $headers, $params);
 	my ($ip, undef) = split(/\//, $result->{address});
 	print "found ip free $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip;
@@ -184,6 +189,8 @@ sub add_range_next_freeip {
     if ($@) {
 	die "can't find free ip in range $range->{'start-address'}-$range->{'end-address'}: $@" if !$noerr;
     }
+
+    return $ip;
 }
 
 sub del_ip {
@@ -290,6 +297,29 @@ sub is_ip_gateway {
     return $is_gateway;
 }
 
+sub add_iprange {
+    my ($url, $range, $subnet, $headers) = @_;
+
+    my ($start, $smask)= split('/', $range->{'start-address'});
+    my ($end, $emask)= split('/', $range->{'end-address'});
+    my $params = {
+	start_address => "$start/$subnet->{mask}",
+	end_address => "$end/$subnet->{mask}",
+	description => "prefix:$subnet->{cidr}"
+    };
+
+    my $data = eval {
+        my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/ip-ranges/", $headers, $params);
+	return $result;
+    };
+
+    if ($@) {
+        die "Can't add ip range $start/$subnet->{mask} -> $end/$subnet->{mask} $@";
+    }
+
+    return $data->{id};
+}
+
 1;
 
 
-- 
2.47.1



--===============2850319848036294456==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

--===============2850319848036294456==--