SPAM: [PATCH pve-network 15/16] ipam: nautobot: implement plain prefix allocation (without ranges)

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


+ bugfix (return from eval was garbage-collected, callers got undef)

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

diff --git a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
index 95e749c..e3ba57c 100644
--- a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
@@ -96,6 +96,33 @@ sub add_ip {
     }
 }
 
+sub add_next_freeip {
+    my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $vmid, $noerr) = @_;
+
+    my $cidr = $subnet->{cidr};
+
+    my $url = $plugin_config->{url};
+    my $namespace = $plugin_config->{namespace};
+    my $headers = default_headers($plugin_config);
+
+    my $internalid = PVE::Network::SDN::Ipams::NetboxPlugin::get_prefix_id($url, $cidr, $headers);
+
+    my $description = "mac:$mac" if $mac;
+
+    my $params = { type => "dhcp", dns_name => $hostname, description => $description, namespace => $namespace, status => default_ip_status() };
+
+    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;
+    };
+
+    if ($@) {
+	die "can't find free ip in subnet $cidr: $@" if !$noerr;
+    }
+    return $ip;
+}
+
 sub add_range_next_freeip {
     my ($class, $plugin_config, $subnet, $range, $data, $noerr) = @_;
 
@@ -107,7 +134,7 @@ sub add_range_next_freeip {
     my $minimal_size = NetAddr::IP->new($range->{'start-address'}) - NetAddr::IP->new($cidr);
     my $internalid = PVE::Network::SDN::Ipams::NetboxPlugin::get_prefix_id($url, $cidr, $headers);
 
-    eval {
+    my $ip = eval {
 	my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/prefixes/$internalid/available-ips/?limit=$minimal_size", $headers);
 	# v important for NetAddr::IP comparison!
 	my @ips = map((split(/\//,$_->{address}))[0], @{$result});
@@ -125,6 +152,7 @@ sub add_range_next_freeip {
     if ($@) {
 	die "can't find free ip in range $range->{'start-address'}-$range->{'end-address'}: $@" if !$noerr;
     }
+    return $ip;
 }
 
 
-- 
2.39.5




More information about the pve-devel mailing list