dnsexit2: Move body of for
loop to a separate function
For improved readability.
This commit is contained in:
parent
11d0c84639
commit
73a67b728d
1 changed files with 101 additions and 96 deletions
197
ddclient.in
197
ddclient.in
|
@ -4179,104 +4179,109 @@ sub nic_dnsexit2_update {
|
|||
# handling update per host.
|
||||
for my $h (@_) {
|
||||
$config{$h}{'zone'} //= $h;
|
||||
my $name = $h;
|
||||
# Remove the zone suffix from $name. If the zone eq $name, $name can be left alone or
|
||||
# set to the empty string; both have identical semantics. For consistency, always
|
||||
# remove the zone even if it means $name becomes the empty string.
|
||||
if ($name =~ s/(?:^|\.)\Q$config{$h}{'zone'}\E$//) {
|
||||
# The zone was successfully trimmed from $name.
|
||||
dnsexit2_update_host($h);
|
||||
}
|
||||
}
|
||||
|
||||
sub dnsexit2_update_host {
|
||||
my ($h) = @_;
|
||||
my $name = $h;
|
||||
# Remove the zone suffix from $name. If the zone eq $name, $name can be left alone or
|
||||
# set to the empty string; both have identical semantics. For consistency, always
|
||||
# remove the zone even if it means $name becomes the empty string.
|
||||
if ($name =~ s/(?:^|\.)\Q$config{$h}{'zone'}\E$//) {
|
||||
# The zone was successfully trimmed from $name.
|
||||
} else {
|
||||
fatal("Hostname %s does not end with the zone %s", $h, $config{$h}{'zone'});
|
||||
}
|
||||
# The IPv4 and IPv6 addresses must be updated together in a single API call.
|
||||
my %ips;
|
||||
my @updates;
|
||||
for my $ipv ('4', '6') {
|
||||
my $ip = delete($config{$h}{"wantipv$ipv"}) or next;
|
||||
$ips{$ipv} = $ip;
|
||||
info("Going to update IPv%s address to %s for %s.", $ipv, $ip, $h);
|
||||
$config{$h}{"status-ipv$ipv"} = 'failed';
|
||||
push(@updates, {
|
||||
name => $name,
|
||||
type => ($ipv eq '6') ? 'AAAA' : 'A',
|
||||
content => $ip,
|
||||
ttl => $config{$h}{'ttl'},
|
||||
});
|
||||
};
|
||||
my $url = $config{$h}{'server'} . $config{$h}{'path'};
|
||||
my $reply = geturl(
|
||||
proxy => opt('proxy'),
|
||||
url => $url,
|
||||
headers => "Content-Type: application/json\nAccept: application/json",
|
||||
method => 'POST',
|
||||
data => encode_json({
|
||||
apikey => $config{$h}{'password'},
|
||||
domain => $config{$h}{'zone'},
|
||||
update => \@updates,
|
||||
}),
|
||||
);
|
||||
unless ($reply && header_ok($h, $reply)) {
|
||||
failed("updating %s: Could not connect to %s", $h, $url);
|
||||
return;
|
||||
};
|
||||
debug("%s", $reply);
|
||||
(my $http_status) = ($reply =~ m%^s*HTTP/.*\s+(\d+)%i);
|
||||
debug("HTTP response code: %s", $http_status);
|
||||
if ($http_status ne '200') {
|
||||
failed("Failed to update Host\n%s", $h);
|
||||
failed("HTTP response code\n%s", $http_status);
|
||||
failed("Full reply\n%s", $reply) unless opt('verbose');
|
||||
return;
|
||||
}
|
||||
my $body = ($reply =~ s/^.*?\r?\n\r?\n//sr);
|
||||
my $response = eval { decode_json($body); };
|
||||
if (!$response) {
|
||||
failed("failed to parse response: $@");
|
||||
return;
|
||||
}
|
||||
if (!defined($response->{'code'}) || !defined($response->{'message'})) {
|
||||
failed("Did not receive expected 'code' and 'message' keys in server response:\n%s",
|
||||
$body);
|
||||
return;
|
||||
}
|
||||
my %codemeaning = (
|
||||
'0' => ['good', 'Success! Actions got executed successfully.'],
|
||||
'1' => ['warning', 'Some execution problems. May not indicate actions failures. Some action may got executed fine and some may have problems.'],
|
||||
'2' => ['badauth', 'API Key Authentication Error. The API Key is missing or wrong.'],
|
||||
'3' => ['error', 'Missing Required Definitions. Your JSON file may missing some required definitions.'],
|
||||
'4' => ['error', 'JSON Data Syntax Error. Your JSON file has syntax error.'],
|
||||
'5' => ['error', 'JSON Defined Record Type not Supported. Your JSON may try to update some record type not supported by our system.'],
|
||||
'6' => ['error', 'System Error. Our system problem. May not be your problem. Contact our support if you got such error.'],
|
||||
'7' => ['error', 'Error getting post data. Our server has problem to receive your JSON posting.'],
|
||||
);
|
||||
if (!exists($codemeaning{$response->{'code'}})) {
|
||||
failed("Status code %s is unknown!", $response->{'code'});
|
||||
return;
|
||||
}
|
||||
my ($status, $message) = @{$codemeaning{$response->{'code'}}};
|
||||
info("Status: %s -- Message: %s", $status, $message);
|
||||
info("Server Message: %s -- Server Details: %s", $response->{'message'},
|
||||
defined($response->{'details'}) ? $response->{'details'}[0] : "no details received");
|
||||
if ($status ne 'good') {
|
||||
if ($status eq 'warning') {
|
||||
warning("%s", $message);
|
||||
warning("Server response: %s", $response->{'message'});
|
||||
} elsif ($status =~ m'^(badauth|error)$') {
|
||||
failed("%s", $message);
|
||||
failed("Server response: %s", $response->{'message'});
|
||||
} else {
|
||||
fatal("Hostname %s does not end with the zone %s", $h, $config{$h}{'zone'});
|
||||
}
|
||||
# The IPv4 and IPv6 addresses must be updated together in a single API call.
|
||||
my %ips;
|
||||
my @updates;
|
||||
for my $ipv ('4', '6') {
|
||||
my $ip = delete($config{$h}{"wantipv$ipv"}) or next;
|
||||
$ips{$ipv} = $ip;
|
||||
info("Going to update IPv%s address to %s for %s.", $ipv, $ip, $h);
|
||||
$config{$h}{"status-ipv$ipv"} = 'failed';
|
||||
push(@updates, {
|
||||
name => $name,
|
||||
type => ($ipv eq '6') ? 'AAAA' : 'A',
|
||||
content => $ip,
|
||||
ttl => $config{$h}{'ttl'},
|
||||
});
|
||||
};
|
||||
my $url = $config{$h}{'server'} . $config{$h}{'path'};
|
||||
my $reply = geturl(
|
||||
proxy => opt('proxy'),
|
||||
url => $url,
|
||||
headers => "Content-Type: application/json\nAccept: application/json",
|
||||
method => 'POST',
|
||||
data => encode_json({
|
||||
apikey => $config{$h}{'password'},
|
||||
domain => $config{$h}{'zone'},
|
||||
update => \@updates,
|
||||
}),
|
||||
);
|
||||
unless ($reply && header_ok($h, $reply)) {
|
||||
failed("updating %s: Could not connect to %s", $h, $url);
|
||||
next;
|
||||
};
|
||||
debug("%s", $reply);
|
||||
(my $http_status) = ($reply =~ m%^s*HTTP/.*\s+(\d+)%i);
|
||||
debug("HTTP response code: %s", $http_status);
|
||||
if ($http_status ne '200') {
|
||||
failed("Failed to update Host\n%s", $h);
|
||||
failed("HTTP response code\n%s", $http_status);
|
||||
failed("Full reply\n%s", $reply) unless opt('verbose');
|
||||
next;
|
||||
}
|
||||
my $body = ($reply =~ s/^.*?\r?\n\r?\n//sr);
|
||||
my $response = eval { decode_json($body); };
|
||||
if (!$response) {
|
||||
failed("failed to parse response: $@");
|
||||
next;
|
||||
}
|
||||
if (!defined($response->{'code'}) || !defined($response->{'message'})) {
|
||||
failed("Did not receive expected 'code' and 'message' keys in server response:\n%s",
|
||||
$body);
|
||||
next;
|
||||
}
|
||||
my %codemeaning = (
|
||||
'0' => ['good', 'Success! Actions got executed successfully.'],
|
||||
'1' => ['warning', 'Some execution problems. May not indicate actions failures. Some action may got executed fine and some may have problems.'],
|
||||
'2' => ['badauth', 'API Key Authentication Error. The API Key is missing or wrong.'],
|
||||
'3' => ['error', 'Missing Required Definitions. Your JSON file may missing some required definitions.'],
|
||||
'4' => ['error', 'JSON Data Syntax Error. Your JSON file has syntax error.'],
|
||||
'5' => ['error', 'JSON Defined Record Type not Supported. Your JSON may try to update some record type not supported by our system.'],
|
||||
'6' => ['error', 'System Error. Our system problem. May not be your problem. Contact our support if you got such error.'],
|
||||
'7' => ['error', 'Error getting post data. Our server has problem to receive your JSON posting.'],
|
||||
);
|
||||
if (!exists($codemeaning{$response->{'code'}})) {
|
||||
failed("Status code %s is unknown!", $response->{'code'});
|
||||
next;
|
||||
}
|
||||
my ($status, $message) = @{$codemeaning{$response->{'code'}}};
|
||||
info("Status: %s -- Message: %s", $status, $message);
|
||||
info("Server Message: %s -- Server Details: %s", $response->{'message'},
|
||||
defined($response->{'details'}) ? $response->{'details'}[0] : "no details received");
|
||||
if ($status ne 'good') {
|
||||
if ($status eq 'warning') {
|
||||
warning("%s", $message);
|
||||
warning("Server response: %s", $response->{'message'});
|
||||
} elsif ($status =~ m'^(badauth|error)$') {
|
||||
failed("%s", $message);
|
||||
failed("Server response: %s", $response->{'message'});
|
||||
} else {
|
||||
failed("Unexpected status: %s", $status);
|
||||
}
|
||||
next;
|
||||
}
|
||||
success("%s", $message);
|
||||
$config{$h}{'mtime'} = $now;
|
||||
keys(%ips); # Reset internal iterator.
|
||||
while (my ($ipv, $ip) = each(%ips)) {
|
||||
$config{$h}{"ipv$ipv"} = $ip;
|
||||
$config{$h}{"status-ipv$ipv"} = 'good';
|
||||
success("Updated %s successfully to IPv%s address %s at time %s", $h, $ipv, $ip, prettytime($config{$h}{'mtime'}));
|
||||
failed("Unexpected status: %s", $status);
|
||||
}
|
||||
return;
|
||||
}
|
||||
success("%s", $message);
|
||||
$config{$h}{'mtime'} = $now;
|
||||
keys(%ips); # Reset internal iterator.
|
||||
while (my ($ipv, $ip) = each(%ips)) {
|
||||
$config{$h}{"ipv$ipv"} = $ip;
|
||||
$config{$h}{"status-ipv$ipv"} = 'good';
|
||||
success("Updated %s successfully to IPv%s address %s at time %s", $h, $ipv, $ip, prettytime($config{$h}{'mtime'}));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue