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 $in = YAML::Tiny->read(shift);
my $ua = LWP::UserAgent->new; my $ua = LWP::UserAgent->new;
my %seen;
sub _debug { sub _debug {
print STDERR ("=== DEBUG ===\n", Dumper(@_), "=== END ===\n") if $ENV{DEBUG} or $in->[0]->{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}) { if($ENV{DEBUG} or $in->[0]->{debug}) {
use LWP::Debug qw(+); use LWP::Debug qw(+);
$ua->add_handler( $ua->add_handler(
@ -61,14 +68,15 @@ sub get_current_zone($) {
sub find_record($$$$) { sub find_record($$$$) {
my ($data, $type, $host, $value) = @_; my ($data, $type, $host, $value) = @_;
foreach my $record (@{$data->{records}}) { 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 # Mark record as seen
# $record->{_seen}++; # $record->{_seen}++;
_debug("Found a record matching $type, $host, $value"); _debug("Found a record matching $host $type $value");
return $record; return $record;
} }
} }
_debug("failed to find a record matching $type, $host, $value"); _debug("failed to find a record matching: $host $type $value");
return undef; return undef;
} }
@ -81,13 +89,23 @@ sub format_record($$$$) {
type => $type, type => $type,
}; };
if ($type eq 'MX') { if ($type eq 'MX') {
$record->{mx_priority} = $value->{pri}; my ($pri, $data) = split(/\s+/, $value) or croak("Argh");
$record->{data} = $value->{value}; $record->{mx_priority} = $pri;
$record->{data} = $data;
} }
return $record; 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($$$$$) { sub check_and_update_record($$$$$) {
my ($zone, $data, $type, $host, $value) = @_; my ($zone, $data, $type, $host, $value) = @_;
my $record = find_record($data, $type, $host, $value); my $record = find_record($data, $type, $host, $value);
@ -110,7 +128,7 @@ sub check_and_update_record($$$$$) {
} else { } else {
# Create new record # Create new record
my $new = format_record($zone, $type, $host, $value); 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( my $res = $ua->post(
$url, $url,
"Content-Type" => "application/json", "Content-Type" => "application/json",
@ -118,7 +136,7 @@ sub check_and_update_record($$$$$) {
records => [ $new ] 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"; # print " - $rec\n";
foreach my $type (keys %{$zone->{$rec}}) { foreach my $type (keys %{$zone->{$rec}}) {
# print " - $type\n"; # print " - $type\n";
print STDERR "\n****** $type $rec on $z ******\n";
if($type eq 'aliases') { if($type eq 'aliases') {
# handle differently # aliases are a set of CNAMEs to the node
print "*** Skipping aliases on $rec for $z\n"; # 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 { } else {
if(ref($zone->{$rec}->{$type}) eq 'ARRAY') { if(ref($zone->{$rec}->{$type}) eq 'ARRAY') {
# multivalue # multivalue