Rollback the complex types, support MX

This commit is contained in:
Matthew Slowe 2021-06-12 14:45:03 +01:00
parent 0a2d2c35e1
commit 3bbcf795d9

View File

@ -11,11 +11,18 @@ use WWW::Form::UrlEncoded::PP qw/build_urlencoded/;
my $in = YAML::Tiny->read(shift);
my $ua = LWP::UserAgent->new;
my %seen;
sub _debug {
print STDERR ("=== DEBUG ===\n", Dumper(@_), "=== END ===\n") if $ENV{DEBUG} or $in->[0]->{debug};
}
sub _info {
print STDERR ("INFO: ");
printf STDERR (@_);
print STDERR "\n";
}
if($ENV{DEBUG} or $in->[0]->{debug}) {
use LWP::Debug qw(+);
$ua->add_handler(
@ -61,14 +68,15 @@ sub get_current_zone($) {
sub find_record($$$$) {
my ($data, $type, $host, $value) = @_;
foreach my $record (@{$data->{records}}) {
if($record->{host} eq $host and $record->{type} eq $type and $record->{data} eq $value) {
_debug("Comparing", $record, $host, $type, $value);
if($record->{host} eq $host and $record->{type} eq $type and reformat_data($type, $record) eq $value) {
# Mark record as seen
# $record->{_seen}++;
_debug("Found a record matching $type, $host, $value");
_debug("Found a record matching $host $type $value");
return $record;
}
}
_debug("failed to find a record matching $type, $host, $value");
_debug("failed to find a record matching: $host $type $value");
return undef;
}
@ -81,13 +89,23 @@ sub format_record($$$$) {
type => $type,
};
if ($type eq 'MX') {
$record->{mx_priority} = $value->{pri};
$record->{data} = $value->{value};
my ($pri, $data) = split(/\s+/, $value) or croak("Argh");
$record->{mx_priority} = $pri;
$record->{data} = $data;
}
return $record;
}
sub reformat_data($$) {
my ($type, $data) = @_;
if($type eq 'MX') {
return sprintf('%d %s', $data->{mx_priority}, $data->{data});
}
return $data->{data};
}
sub check_and_update_record($$$$$) {
my ($zone, $data, $type, $host, $value) = @_;
my $record = find_record($data, $type, $host, $value);
@ -110,7 +128,7 @@ sub check_and_update_record($$$$$) {
} else {
# Create new record
my $new = format_record($zone, $type, $host, $value);
_debug("Create ", $url, undef, $value);
_info("Created new record: %s %s %s", $host, $type, $value);
my $res = $ua->post(
$url,
"Content-Type" => "application/json",
@ -118,7 +136,7 @@ sub check_and_update_record($$$$$) {
records => [ $new ]
})
);
warn "Failed to create $url: " . $res->status_line unless $res->is_success;
warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success;
}
}
@ -131,10 +149,13 @@ foreach my $z (keys %{$in->[0]->{zones}}) {
# print " - $rec\n";
foreach my $type (keys %{$zone->{$rec}}) {
# print " - $type\n";
print STDERR "\n****** $type $rec on $z ******\n";
if($type eq 'aliases') {
# handle differently
print "*** Skipping aliases on $rec for $z\n";
# aliases are a set of CNAMEs to the node
# print "*** Skipping aliases on $rec for $z\n";
foreach my $alias (@{$zone->{$rec}->{aliases}}) {
# _info ("Would create alias for %s -> %s", $alias, $rec);
check_and_update_record($z, $current, "CNAME", $alias, $rec);
}
} else {
if(ref($zone->{$rec}->{$type}) eq 'ARRAY') {
# multivalue