public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: jonahz via pve-devel <pve-devel@lists.proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: jonahz <jonah.zuercher@adfinis.com>
Subject: [pve-devel] [PATCH container 1/1] fix #5496: sdn: fix netbox integration
Date: Mon, 24 Feb 2025 17:58:25 +0100	[thread overview]
Message-ID: <mailman.525.1740485791.293.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20250224165825.82940-1-jonah.zuercher@adfinis.com>

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

From: jonahz <jonah.zuercher@adfinis.com>
To: pve-devel@lists.proxmox.com
Cc: jonahz <jonah.zuercher@adfinis.com>
Subject: [PATCH container 1/1] fix #5496: sdn: fix netbox integration
Date: Mon, 24 Feb 2025 17:58:25 +0100
Message-ID: <20250224165825.82940-2-jonah.zuercher@adfinis.com>

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 <jonah.zuercher@adfinis.com>
---
 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



[-- 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-02-25 12:16 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20250224165825.82940-1-jonah.zuercher@adfinis.com>]

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.525.1740485791.293.pve-devel@lists.proxmox.com \
    --to=pve-devel@lists.proxmox.com \
    --cc=jonah.zuercher@adfinis.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal