Merge pull request #721 from rhansen/gandi

`gandi` cleanups
This commit is contained in:
Richard Hansen 2024-07-28 19:15:29 -04:00 committed by GitHub
commit 2715743ee3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 56 deletions

View file

@ -142,6 +142,8 @@ repository history](https://github.com/ddclient/ddclient/commits/master).
[#719](https://github.com/ddclient/ddclient/pull/719) [#719](https://github.com/ddclient/ddclient/pull/719)
* `yandex`: Errors are now retried. * `yandex`: Errors are now retried.
[#719](https://github.com/ddclient/ddclient/pull/719) [#719](https://github.com/ddclient/ddclient/pull/719)
* `gandi`: Fixed handling of error responses.
[#721](https://github.com/ddclient/ddclient/pull/721)
## 2023-11-23 v3.11.2 ## 2023-11-23 v3.11.2

View file

@ -6905,47 +6905,31 @@ EoEXAMPLE
###################################################################### ######################################################################
sub nic_gandi_update { sub nic_gandi_update {
debug("\nnic_gandi_update -------------------"); debug("\nnic_gandi_update -------------------");
# Update each set configured host.
for my $h (@_) { for my $h (@_) {
for my $ipv ('ipv4', 'ipv6') { for my $ipv ('ipv4', 'ipv6') {
my $ip = delete $config{$h}{"want$ipv"}; my $ip = delete $config{$h}{"want$ipv"} or next;
if(!$ip) {
next;
}
(my $hostname = $h) =~ s/\.\Q$config{$h}{zone}\E$//; (my $hostname = $h) =~ s/\.\Q$config{$h}{zone}\E$//;
info("%s -- Setting IP address to %s.", $h, $ip); info("$h: setting IP address to $ip");
my @headers = ('Content-Type: application/json');
my $headers;
$headers = "Content-Type: application/json\n";
if ($config{$h}{'use-personal-access-token'} == 1) { if ($config{$h}{'use-personal-access-token'} == 1) {
$headers .= "Authorization: Bearer $config{$h}{'password'}\n"; push(@headers, "Authorization: Bearer $config{$h}{'password'}");
} else {
push(@headers, "Authorization: Apikey $config{$h}{'password'}");
} }
else
{
$headers .= "Authorization: Apikey $config{$h}{'password'}\n";
}
my $rrset_type = $ipv eq 'ipv6' ? 'AAAA' : 'A'; my $rrset_type = $ipv eq 'ipv6' ? 'AAAA' : 'A';
my $url; my $url = "https://$config{$h}{'server'}$config{$h}{'script'}/livedns/domains/$config{$h}{'zone'}/records/$hostname/$rrset_type";
$url = "https://$config{$h}{'server'}$config{$h}{'script'}";
$url .= "/livedns/domains/$config{$h}{'zone'}/records/$hostname/$rrset_type";
my $reply = geturl( my $reply = geturl(
proxy => opt('proxy'), proxy => opt('proxy'),
url => $url, url => $url,
headers => $headers, headers => \@headers,
method => 'GET' method => 'GET',
); );
my $ok = header_ok($h, $reply); next if !header_ok($h, $reply);
$reply =~ s/^.*?\n\n//s; $reply =~ s/^.*?\n\n//s;
my $response = eval { decode_json($reply) }; my $response = eval { decode_json($reply) };
if (!defined($response)) { if (ref($response) ne 'HASH') {
$config{$h}{"status-$ipv"} = "bad"; $config{$h}{"status-$ipv"} = "bad";
failed("$h: response is not a JSON object: $reply");
failed("%s -- Unexpected service response.", $h);
next; next;
} }
if ($response->{'rrset_values'}->[0] eq $ip && (!defined($config{$h}{'ttl'}) || if ($response->{'rrset_values'}->[0] eq $ip && (!defined($config{$h}{'ttl'}) ||
@ -6953,42 +6937,38 @@ sub nic_gandi_update {
$config{$h}{'ip'} = $ip; $config{$h}{'ip'} = $ip;
$config{$h}{'mtime'} = $now; $config{$h}{'mtime'} = $now;
$config{$h}{"status-$ipv"} = "good"; $config{$h}{"status-$ipv"} = "good";
success("updating %s: skipped: address was already set to %s.", $h, $ip); success("$h: skipped: address was already set to $ip");
next; next;
} }
my $data = encode_json({
defined($config{$h}{'ttl'}) ? (rrset_ttl => $config{$h}{'ttl'}) : (),
rrset_values => [$ip],
});
$reply = geturl( $reply = geturl(
proxy => opt('proxy'), proxy => opt('proxy'),
url => $url, url => $url,
headers => $headers, headers => \@headers,
method => 'PUT', method => 'PUT',
data => $data, data => encode_json({
defined($config{$h}{'ttl'}) ? (rrset_ttl => $config{$h}{'ttl'}) : (),
rrset_values => [$ip],
}),
); );
$ok = header_ok($h, $reply); if (!header_ok($h, $reply)) {
if ($ok) { $config{$h}{"status-$ipv"} = "bad";
$reply =~ s/^.*?\n\n//s;
my $response = eval { decode_json($reply) };
if (ref($response) eq 'HASH' && ($response->{message} // '') ne '') {
failed("$h: $response->{message}");
} else {
failed("$h: unexpected error response: $reply");
}
next;
}
$config{$h}{'ip'} = $ip; $config{$h}{'ip'} = $ip;
$config{$h}{'mtime'} = $now; $config{$h}{'mtime'} = $now;
$config{$h}{"status-$ipv"} = "good"; $config{$h}{"status-$ipv"} = "good";
success("%s -- Updated successfully to %s.", $h, $ip); success("$h: updated successfully to $ip");
} else {
$config{$h}{"status-$ipv"} = "bad";
if (defined($response->{status}) && $response->{status} eq "error") {
my @errors;
for my $err (@{$response->{errors}}) {
push(@errors, $err->{description});
}
failed("%s -- %s.", $h, join(", ", @errors));
} else {
failed("%s -- Unexpected service response.", $h);
}
}
} }
} }
} }
###################################################################### ######################################################################
## nic_keysystems_examples ## nic_keysystems_examples
###################################################################### ######################################################################