SPAM: [PATCH pve-network 13/16] ipam: nautobot: 1st draft for allocating ip in dhcp range

Lou Lecrivain lou.lecrivain at wdz.de
Wed Nov 27 17:18:00 CET 2024


offset has to be fixed - we can't guarantee correct start
if IPs outside range have been boked

Signed-off-by: lou lecrivain <lou.lecrivain at wdz.de>
---
 src/PVE/Network/SDN/Ipams/NautobotPlugin.pm | 25 +++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
index 69e7897..bf9d34a 100644
--- a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
@@ -5,6 +5,7 @@ use warnings;
 use PVE::INotify;
 use PVE::Cluster;
 use PVE::Tools;
+use NetAddr::IP;
 
 use base('PVE::Network::SDN::Ipams::NetboxPlugin');
 
@@ -95,6 +96,30 @@ sub add_ip {
     }
 }
 
+sub add_range_next_freeip {
+    my ($class, $plugin_config, $subnet, $range, $data, $noerr) = @_;
+
+    my $url = $plugin_config->{url};
+    my $namespace = $plugin_config->{namespace};
+    my $headers = default_headers($plugin_config);
+    my $cidr = $subnet->{cidr};
+
+    my $range_offset = NetAddr::IP->new($range->{'start-address'}) - NetAddr::IP->new($cidr);
+    my $internalid = PVE::Network::SDN::Ipams::NetboxPlugin::get_prefix_id($url, $cidr, $headers);
+    my $params = { offset => $range_offset };
+
+    eval {
+	my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
+	my ($ip, undef) = split(/\//, @{$result}[0]->{address});
+	print "found free ip $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip;
+	return $ip
+    };
+
+    if ($@) {
+	die "can't find free ip in range $range->{'start-address'}-$range->{'end-address'}: $@" if !$noerr;
+    }
+}
+
 
 sub update_ip {
     my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway, $noerr) = @_;
-- 
2.39.5




More information about the pve-devel mailing list