ipam: netbox: fix adding vms without a name set in the options

The hostname provided to the methods in the IPAM plugins is empty for
VMs that do not have a name set. We always included the dns_name key
in the request, even if it was undefined, which leads Netbox to return
a 400 Bad Request error. With this patch we omit the dns_name
parameter if it isn't set, so the call succeeds.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
Link: https://lore.proxmox.com/20250408132517.225240-1-s.hanreich@proxmox.com
This commit is contained in:
Stefan Hanreich 2025-04-08 15:25:17 +02:00 committed by Thomas Lamprecht
parent 1fd6496b5e
commit 3ede261b25

View file

@ -206,11 +206,14 @@ sub add_ip {
} }
eval { eval {
netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", { my $params = {
address => "$ip/$mask", address => "$ip/$mask",
dns_name => $hostname,
description => $description, description => $description,
}); };
$params->{dns_name} = $hostname if $hostname;
netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", $params);
}; };
if ($@) { if ($@) {
@ -244,11 +247,14 @@ sub update_ip {
} }
eval { eval {
netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", { my $params = {
address => "$ip/$mask", address => "$ip/$mask",
dns_name => $hostname,
description => $description, description => $description,
}); };
$params->{dns_name} = $hostname if $hostname;
netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", $params);
}; };
if ($@) { if ($@) {
die "error update ip $ip : $@" if !$noerr; die "error update ip $ip : $@" if !$noerr;
@ -272,10 +278,13 @@ sub add_next_freeip {
$description = "mac:$mac" if $mac; $description = "mac:$mac" if $mac;
my $ip = eval { my $ip = eval {
my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", { my $params = {
dns_name => $hostname,
description => $description, description => $description,
}); };
$params->{dns_name} = $hostname if $hostname;
my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", $params);
my ($ip, undef) = split(/\//, $result->{address}); my ($ip, undef) = split(/\//, $result->{address});
return $ip; return $ip;
@ -303,10 +312,13 @@ sub add_range_next_freeip {
$description = "mac:$data->{mac}" if $data->{mac}; $description = "mac:$data->{mac}" if $data->{mac};
my $ip = eval { my $ip = eval {
my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", { my $params = {
dns_name => $data->{hostname},
description => $description, description => $description,
}); };
$params->{dns_name} = $data->{hostname} if $data->{hostname};
my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", $params);
my ($ip, undef) = split(/\//, $result->{address}); my ($ip, undef) = split(/\//, $result->{address});
print "found ip free $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip; print "found ip free $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip;