[pve-devel] [PATCH container 0/1] fix #5496: sdn: fix netbox integration

2025-02-25 Thread jonahz via pve-devel
--- Begin Message ---
Hi,

This patch fixes several bugs in the netbox SDN plugin.
At the moment, the plugin is completely broken and doesn't work at all because 
it can't assign IPs correctly.

Forum discussion: 
https://forum.proxmox.com/threads/sdn-problems-with-netbox-as-ipam.147395/

In addition to that, I added a function to make sure the IP range from the DHCP 
configuration is created in netbox if it doesn't exist yet.
Currently, the plugin fails silently if the range doesn't exist.

jonahz (1):
  fix #5496: sdn: fix netbox integration

 src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 21 +++--
 1 file changed, 19 insertions(+), 2 deletions(-)

-- 
2.48.1


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


[pve-devel] [PATCH container 1/1] fix #5496: sdn: fix netbox integration

2025-02-25 Thread jonahz via pve-devel
--- Begin Message ---
This fixes a bug which currently prevents the netbox
plugin from assigning an IP correctly.

It also makes sure to create an IP range, if none exists
for the configured DHCP range.

Signed-off-by: jonahz 
---
 src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 21 +++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm 
b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
index d923269..c391068 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,24 @@ 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);
+# create range if it doesn't exist
+if (!$internalid) {
+my $cidr = (split(/\//, $subnet->{cidr}))[1];
+my $params = { start_address => "$range->{'start-address'}/$cidr", 
end_address => "$range->{'end-address'}/$cidr" };
+eval {
+my $result = PVE::Network::SDN::api_request("POST", 
"$url/ipam/ip-ranges/", $headers, $params);
+$internalid = $result->{id};
+};
+if ($@) {
+die "error add range to ipam: $@" if !$noerr;
+}
+}
+
 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 +199,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 {
-- 
2.48.1


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