Merge pull request #711 from rhansen/godaddy
godaddy: readability improvements
This commit is contained in:
commit
dfef6b2e99
1 changed files with 58 additions and 75 deletions
133
ddclient.in
133
ddclient.in
|
@ -2873,7 +2873,7 @@ sub geturl {
|
|||
push(@curlopt, "url=\"".escape_curl_param("${protocol}://${server}/${url}").'"');
|
||||
|
||||
# Each header line is added individually
|
||||
@header_lines = split('\n', $headers);
|
||||
@header_lines = ref($headers) eq 'ARRAY' ? @$headers : split('\n', $headers);
|
||||
$_ = "header=\"".escape_curl_param($_).'"' for (@header_lines);
|
||||
push(@curlopt, @header_lines);
|
||||
|
||||
|
@ -5737,92 +5737,75 @@ EoEXAMPLE
|
|||
######################################################################
|
||||
sub nic_godaddy_update {
|
||||
debug("\nnic_godaddy_update --------------------");
|
||||
for my $host (@_) {
|
||||
my $ipv4 = delete $config{$host}{'wantipv4'};
|
||||
my $ipv6 = delete $config{$host}{'wantipv6'};
|
||||
|
||||
my $zone = $config{$host}{'zone'};
|
||||
(my $hostname = $host) =~ s/\.\Q$zone\E$//;
|
||||
|
||||
for my $ip ($ipv4, $ipv6) {
|
||||
next if (!$ip);
|
||||
|
||||
info("%s.%s -- Setting IP address to %s.", $hostname, $zone, $ip);
|
||||
verbose("UPDATE:", "updating %s.%s", $hostname, $zone);
|
||||
|
||||
my $ipversion = ($ip eq ($ipv6 // '')) ? '6' : '4';
|
||||
my $status = \$config{$host}{"status-ipv$ipversion"};
|
||||
my $rrset_type = ($ipversion eq '6') ? 'AAAA' : 'A';
|
||||
my $data = encode_json([ {
|
||||
data => $ip,
|
||||
defined($config{$host}{'ttl'}) ? (ttl => $config{$host}{'ttl'}) : (),
|
||||
name => $hostname,
|
||||
type => $rrset_type,
|
||||
} ]);
|
||||
|
||||
my $url = "https://$config{$host}{'server'}";
|
||||
$url .= "/${zone}/records/${rrset_type}/${hostname}";
|
||||
|
||||
my $header = "Content-Type: application/json\n";
|
||||
$header .= "Accept: application/json\n";
|
||||
$header .= "Authorization: sso-key $config{$host}{'login'}:$config{$host}{'password'}\n";
|
||||
for my $h (@_) {
|
||||
my $zone = $config{$h}{'zone'};
|
||||
(my $hostname = $h) =~ s/\.\Q$zone\E$//;
|
||||
for my $ipv ('4', '6') {
|
||||
my $ip = delete($config{$h}{"wantipv$ipv"}) or next;
|
||||
info("$h: Setting IPv$ipv address to $ip");
|
||||
my $rrset_type = ($ipv eq '6') ? 'AAAA' : 'A';
|
||||
my $url = "https://$config{$h}{'server'}/$zone/records/$rrset_type/$hostname";
|
||||
my $reply = geturl(
|
||||
proxy => opt('proxy'),
|
||||
url => $url,
|
||||
headers => $header,
|
||||
headers => [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
"Authorization: sso-key $config{$h}{'login'}:$config{$h}{'password'}",
|
||||
],
|
||||
method => 'PUT',
|
||||
data => $data,
|
||||
data => encode_json([{
|
||||
data => $ip,
|
||||
defined($config{$h}{'ttl'}) ? (ttl => $config{$h}{'ttl'}) : (),
|
||||
name => $hostname,
|
||||
type => $rrset_type,
|
||||
}]),
|
||||
);
|
||||
unless ($reply) {
|
||||
failed("%s.%s -- Could not connect to %s.", $hostname, $zone, $config{$host}{'server'});
|
||||
failed("$h: Could not connect to $config{$h}{'server'}");
|
||||
next;
|
||||
}
|
||||
|
||||
(my $code) = ($reply =~ m%^s*HTTP/.*\s+(\d+)%i);
|
||||
my $ok = header_ok($host, $reply);
|
||||
my $msg;
|
||||
$reply =~ s/^.*?\n\n//s; # extract payload
|
||||
my $ok = header_ok($h, $reply);
|
||||
$reply =~ s/^.*?\n\n//s;
|
||||
my $response = eval {decode_json($reply)};
|
||||
if (!defined($response) && $code != "200") {
|
||||
$$status = "bad";
|
||||
|
||||
failed("%s.%s -- Unexpected or empty service response, cannot parse data.", $hostname, $zone);
|
||||
} elsif (defined($response->{code})) {
|
||||
info("%s.%s -- %s - %s.", $hostname, $zone, $response->{code}, $response->{message});
|
||||
}
|
||||
if ($ok) {
|
||||
# read data
|
||||
$config{$host}{"ipv$ipversion"} = $ip;
|
||||
$config{$host}{'mtime'} = $now;
|
||||
$$status = 'good';
|
||||
|
||||
success("%s.%s -- Updated successfully to %s (status: %s).", $hostname, $zone, $ip, $code);
|
||||
if (!defined($response)) {
|
||||
failed("$h: Unexpected or empty service response, cannot parse data");
|
||||
next;
|
||||
} elsif ($code == "400") {
|
||||
$msg = 'GoDaddy API URL ($url) was malformed.';
|
||||
} elsif ($code == "401") { # authentication error
|
||||
if ($config{$host}{'login'} && $config{$host}{'login'}) {
|
||||
$msg = 'login or password option incorrect.';
|
||||
} else {
|
||||
$msg = 'login or password option missing.';
|
||||
}
|
||||
$msg .= ' Correct values can be obtained from from https://developer.godaddy.com/keys/.';
|
||||
} elsif ($code == "403") {
|
||||
$msg = 'Customer identified by login and password options denied permission.';
|
||||
} elsif ($code == "404") {
|
||||
$msg = "\"${hostname}.${zone}\" not found at GoDaddy, please check zone option and login/password.";
|
||||
} elsif ($code == "422") {
|
||||
$msg = "\"${hostname}.${zone}\" has invalid domain or lacks A/AAAA record.";
|
||||
} elsif ($code == "429") {
|
||||
$msg = 'Too many requests to GoDaddy within brief period.';
|
||||
} elsif ($code == "503") {
|
||||
$msg = "\"${hostname}.${zone}\" is unavailable.";
|
||||
} else {
|
||||
$msg = 'Unexpected service response.';
|
||||
} elsif (defined($response->{code})) {
|
||||
info("$h: $response->{code} - $response->{message}");
|
||||
}
|
||||
|
||||
$$status = 'bad';
|
||||
failed("%s.%s -- %s", $hostname, $zone, $msg);
|
||||
if (!$ok) {
|
||||
my $msg;
|
||||
if ($code eq "400") {
|
||||
$msg = 'GoDaddy API URL ($url) was malformed.';
|
||||
} elsif ($code eq "401") {
|
||||
if ($config{$h}{'login'} && $config{$h}{'login'}) {
|
||||
$msg = 'login or password option incorrect.';
|
||||
} else {
|
||||
$msg = 'login or password option missing.';
|
||||
}
|
||||
$msg .= ' Correct values can be obtained from from https://developer.godaddy.com/keys/.';
|
||||
} elsif ($code eq "403") {
|
||||
$msg = 'Customer identified by login and password options denied permission.';
|
||||
} elsif ($code eq "404") {
|
||||
$msg = "\"$h\" not found at GoDaddy, please check zone option and login/password.";
|
||||
} elsif ($code eq "422") {
|
||||
$msg = "\"$h\" has invalid domain or lacks A/AAAA record.";
|
||||
} elsif ($code eq "429") {
|
||||
$msg = 'Too many requests to GoDaddy within brief period.';
|
||||
} elsif ($code eq "503") {
|
||||
$msg = "\"$h\" is unavailable.";
|
||||
} else {
|
||||
$msg = 'Unexpected service response.';
|
||||
}
|
||||
failed("$h: $msg");
|
||||
next;
|
||||
}
|
||||
$config{$h}{"ipv$ipv"} = $ip;
|
||||
$config{$h}{'mtime'} = $now;
|
||||
$config{$h}{"status-ipv$ipv"} = 'good';
|
||||
success("$h: Updated successfully to $ip (status: $code)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue