dnsexit2: Build updates array directly, not hash to array

to improve readability, and to make it easier to use
`group_hosts_by()` in the future to update multiple hosts at the same
time.
This commit is contained in:
Richard Hansen 2024-05-29 18:12:23 -04:00
parent 2bf6d348b0
commit 6c89eaf4ac

View file

@ -4175,18 +4175,20 @@ sub nic_dnsexit2_update {
# The DNSExit API does not support updating multiple hosts at a time. # The DNSExit API does not support updating multiple hosts at a time.
for my $h (@_) { for my $h (@_) {
# The IPv4 and IPv6 addresses must be updated together in a single API call. # The IPv4 and IPv6 addresses must be updated together in a single API call.
my %total_payload; my %ips;
my @updates;
for my $ipv ('4', '6') { for my $ipv ('4', '6') {
my $ip = delete($config{$h}{"wantipv$ipv"}) or next; my $ip = delete($config{$h}{"wantipv$ipv"}) or next;
$ips{$ipv} = $ip;
my $type = ($ipv eq '6') ? 'AAAA' : 'A'; my $type = ($ipv eq '6') ? 'AAAA' : 'A';
info("Going to update IPv%s address to %s for %s.", $ipv, $ip, $h); info("Going to update IPv%s address to %s for %s.", $ipv, $ip, $h);
$config{$h}{'status-ipv$ipv'} = 'failed'; $config{$h}{'status-ipv$ipv'} = 'failed';
$total_payload{$ipv} = { push(@updates, {
name => $h, name => $h,
type => $type, type => $type,
content => $ip, content => $ip,
ttl => $config{$h}{'ttl'}, ttl => $config{$h}{'ttl'},
}; });
}; };
my $url = "https://$config{$h}{'server'}$config{$h}{'path'}"; my $url = "https://$config{$h}{'server'}$config{$h}{'path'}";
my $header = "Content-Type: application/json\nAccept: application/json"; my $header = "Content-Type: application/json\nAccept: application/json";
@ -4196,11 +4198,10 @@ sub nic_dnsexit2_update {
} else { } else {
debug("Zone is: %s", $config{$h}{'zone'}); debug("Zone is: %s", $config{$h}{'zone'});
} }
my @payload_values = values %total_payload;
my $data = encode_json({ my $data = encode_json({
apikey => $config{$h}{'password'}, apikey => $config{$h}{'password'},
domain => $config{$h}{'zone'}, domain => $config{$h}{'zone'},
update => \@payload_values, update => \@updates,
}); });
my $reply = geturl( my $reply = geturl(
proxy => opt('proxy'), proxy => opt('proxy'),
@ -4257,8 +4258,8 @@ sub nic_dnsexit2_update {
info("Status: %s -- Message: %s", $status, $message); info("Status: %s -- Message: %s", $status, $message);
info("Server Message: %s -- Server Details: %s", $response->{'message'}, info("Server Message: %s -- Server Details: %s", $response->{'message'},
defined($response->{'details'}) ? $response->{'details'}[0] : "no details received"); defined($response->{'details'}) ? $response->{'details'}[0] : "no details received");
$config{$h}{'status-ipv4'} = $status if $total_payload{'4'}; $config{$h}{'status-ipv4'} = $status if $ips{'4'};
$config{$h}{'status-ipv6'} = $status if $total_payload{'6'}; $config{$h}{'status-ipv6'} = $status if $ips{'6'};
if ($status ne 'good') { if ($status ne 'good') {
if ($status eq 'warning') { if ($status eq 'warning') {
warning("%s", $message); warning("%s", $message);
@ -4273,10 +4274,11 @@ sub nic_dnsexit2_update {
} }
success("%s", $message); success("%s", $message);
$config{$h}{'mtime'} = $now; $config{$h}{'mtime'} = $now;
for my $ipv (keys %total_payload) { keys(%ips); # Reset internal iterator.
$config{$h}{"ipv$ipv"} = $total_payload{$ipv}{content}; while (my ($ipv, $ip) = each(%ips)) {
$config{$h}{"ipv$ipv"} = $ip;
$config{$h}{"status-ipv$ipv"} = 'good'; $config{$h}{"status-ipv$ipv"} = 'good';
success("Updated %s successfully to IPv%s address %s at time %s", $h, $ipv, $total_payload{$ipv}{content}, prettytime($config{$h}{'mtime'})); success("Updated %s successfully to IPv%s address %s at time %s", $h, $ipv, $ip, prettytime($config{$h}{'mtime'}));
} }
} }
} }