gandi: update logic

- allow updating IPv6/AAAA
- allow updating A and AAAA records simultaneously
- skip updating if record already has same IP
This commit is contained in:
Joel Beckmeyer 2023-07-31 23:06:39 -04:00
parent baa7e440ed
commit 5ec8bfe141

View file

@ -7362,68 +7362,91 @@ EoEXAMPLE
######################################################################
sub nic_gandi_update {
debug("\nnic_gandi_update -------------------");
# Update each set configured host.
foreach my $h (@_) {
my $ip = delete $config{$h}{'wantip'};
(my $hostname = $h) =~ s/\.\Q$config{$h}{zone}\E$//;
foreach my $ipv ('ipv4', 'ipv6') {
my $ip = delete $config{$h}{"want$ipv"};
if(!$ip) {
next;
}
(my $hostname = $h) =~ s/\.\Q$config{$h}{zone}\E$//;
info("%s -- Setting IP address to %s.", $h, $ip);
verbose("UPDATE:", "updating %s", $h);
info("%s -- Setting IP address to %s.", $h, $ip);
verbose("UPDATE:", "updating %s", $h);
my $headers;
$headers = "Content-Type: application/json\n";
$headers .= "Authorization: Apikey $config{$h}{'password'}\n";
my $headers;
$headers = "Content-Type: application/json\n";
$headers .= "Authorization: Apikey $config{$h}{'password'}\n";
my $data = encode_json({
defined($config{$h}{'ttl'}) ? (rrset_ttl => $config{$h}{'ttl'}) : (),
rrset_values => [$ip],
});
my $rrset_type = is_ipv6($ip) ? "AAAA" : "A";
my $url;
$url = "https://$config{$h}{'server'}$config{$h}{'script'}";
$url .= "/livedns/domains/$config{$h}{'zone'}/records/$hostname/$rrset_type";
my $rrset_type = $ipv eq 'ipv6' ? 'AAAA' : 'A';
my $url;
$url = "https://$config{$h}{'server'}$config{$h}{'script'}";
$url .= "/livedns/domains/$config{$h}{'zone'}/records/$hostname/$rrset_type";
my $reply = geturl(
proxy => opt('proxy'),
url => $url,
headers => $headers,
method => 'PUT',
data => $data,
);
unless ($reply) {
failed("%s -- Could not connect to %s.", $h, $config{$h}{'server'});
next;
}
my $ok = header_ok($h, $reply);
my $reply = geturl(
proxy => opt('proxy'),
url => $url,
headers => $headers,
method => 'GET'
);
unless ($reply) {
failed("%s -- Could not connect to %s.", $h, $config{$h}{'server'});
next;
}
my $ok = header_ok($h, $reply);
$reply =~ s/^.*?\n\n//s;
my $response = eval { decode_json($reply) };
if (!defined($response)) {
$config{$h}{'status'} = "bad";
$reply =~ s/^.*?\n\n//s;
my $response = eval { decode_json($reply) };
if (!defined($response)) {
$config{$h}{"status-$ipv"} = "bad";
failed("%s -- Unexpected service response.", $h);
next;
}
if ($ok) {
$config{$h}{'ip'} = $ip;
$config{$h}{'mtime'} = $now;
$config{$h}{'status'} = "good";
success("%s -- Updated successfully to %s.", $h, $ip);
} else {
$config{$h}{'status'} = "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);
next;
}
if($response->{'rrset_values'}->[0] eq $ip && (!defined($config{$h}{'ttl'}) ||
$response->{'rrset_ttl'} eq $config{$h}{'ttl'})) {
$config{$h}{'ip'} = $ip;
$config{$h}{'mtime'} = $now;
$config{$h}{"status-$ipv"} = "good";
success("updating %s: skipped: address was already set to %s.", $h, $ip);
next;
}
my $data = encode_json({
defined($config{$h}{'ttl'}) ? (rrset_ttl => $config{$h}{'ttl'}) : (),
rrset_values => [$ip],
});
$reply = geturl(
proxy => opt('proxy'),
url => $url,
headers => $headers,
method => 'PUT',
data => $data,
);
unless ($reply) {
failed("%s -- Could not connect to %s.", $h, $config{$h}{'server'});
next;
}
$ok = header_ok($h, $reply);
if ($ok) {
$config{$h}{'ip'} = $ip;
$config{$h}{'mtime'} = $now;
$config{$h}{"status-$ipv"} = "good";
success("%s -- Updated successfully to %s.", $h, $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);
}
}
}
}