--- Begin Message ---
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
--- End Message ---
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel