Merge pull request #711 from rhansen/godaddy

godaddy: readability improvements
This commit is contained in:
Richard Hansen 2024-07-19 16:36:24 -04:00 committed by GitHub
commit dfef6b2e99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2873,7 +2873,7 @@ sub geturl {
push(@curlopt, "url=\"".escape_curl_param("${protocol}://${server}/${url}").'"'); push(@curlopt, "url=\"".escape_curl_param("${protocol}://${server}/${url}").'"');
# Each header line is added individually # 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); $_ = "header=\"".escape_curl_param($_).'"' for (@header_lines);
push(@curlopt, @header_lines); push(@curlopt, @header_lines);
@ -5737,92 +5737,75 @@ EoEXAMPLE
###################################################################### ######################################################################
sub nic_godaddy_update { sub nic_godaddy_update {
debug("\nnic_godaddy_update --------------------"); debug("\nnic_godaddy_update --------------------");
for my $host (@_) { for my $h (@_) {
my $ipv4 = delete $config{$host}{'wantipv4'}; my $zone = $config{$h}{'zone'};
my $ipv6 = delete $config{$host}{'wantipv6'}; (my $hostname = $h) =~ s/\.\Q$zone\E$//;
for my $ipv ('4', '6') {
my $zone = $config{$host}{'zone'}; my $ip = delete($config{$h}{"wantipv$ipv"}) or next;
(my $hostname = $host) =~ s/\.\Q$zone\E$//; info("$h: Setting IPv$ipv address to $ip");
my $rrset_type = ($ipv eq '6') ? 'AAAA' : 'A';
for my $ip ($ipv4, $ipv6) { my $url = "https://$config{$h}{'server'}/$zone/records/$rrset_type/$hostname";
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";
my $reply = geturl( my $reply = geturl(
proxy => opt('proxy'), proxy => opt('proxy'),
url => $url, url => $url,
headers => $header, headers => [
'Content-Type: application/json',
'Accept: application/json',
"Authorization: sso-key $config{$h}{'login'}:$config{$h}{'password'}",
],
method => 'PUT', method => 'PUT',
data => $data, data => encode_json([{
data => $ip,
defined($config{$h}{'ttl'}) ? (ttl => $config{$h}{'ttl'}) : (),
name => $hostname,
type => $rrset_type,
}]),
); );
unless ($reply) { 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; next;
} }
(my $code) = ($reply =~ m%^s*HTTP/.*\s+(\d+)%i); (my $code) = ($reply =~ m%^s*HTTP/.*\s+(\d+)%i);
my $ok = header_ok($host, $reply); my $ok = header_ok($h, $reply);
my $msg; $reply =~ s/^.*?\n\n//s;
$reply =~ s/^.*?\n\n//s; # extract payload
my $response = eval {decode_json($reply)}; my $response = eval {decode_json($reply)};
if (!defined($response) && $code != "200") { if (!defined($response)) {
$$status = "bad"; failed("$h: Unexpected or empty service response, cannot parse data");
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);
next; next;
} elsif ($code == "400") { } elsif (defined($response->{code})) {
$msg = 'GoDaddy API URL ($url) was malformed.'; info("$h: $response->{code} - $response->{message}");
} 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.';
} }
if (!$ok) {
$$status = 'bad'; my $msg;
failed("%s.%s -- %s", $hostname, $zone, $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)");
} }
} }
} }