Implement dry-run

This commit is contained in:
Matthew Slowe 2021-06-16 17:01:01 +01:00
parent 405903f0f5
commit 95f0148af9

View File

@ -13,6 +13,7 @@ use WWW::Form::UrlEncoded::PP qw/build_urlencoded/;
my $in = YAML::Tiny->read(shift);
my $ua = LWP::UserAgent->new;
my %seen;
my $DRY_RUN = $ENV{DRY_RUN};
sub _debug {
print STDERR ("=== DEBUG ===\n", Dumper(@_), "=== END ===\n") if $ENV{DEBUG} or $in->[0]->{debug};
@ -51,6 +52,8 @@ sub is_unsupported($) {
return $supported_types{$type};
}
_notice("Dry run - no changes will be applied") if ${DRY_RUN};
if($ENV{DEBUG} or $in->[0]->{debug}) {
use LWP::Debug qw(+);
$ua->add_handler(
@ -178,6 +181,7 @@ sub check_and_update_record($$$$$) {
# Update the record
$record->{ttl} = $in->[0]->{defaults}->{ttl}->{$zone};
_debug("Update ", $url, $record, to_json($record));
unless ($DRY_RUN) {
my $res = $ua->put(
$url,
"Content-Type" => "application/json",
@ -185,11 +189,13 @@ sub check_and_update_record($$$$$) {
);
warn "Failed to update $url: " . $res->status_line unless $res->is_success;
}
}
} else {
# Create new record
my $new = format_record($zone, $type, $host, $value);
_notice("Created new record: %s %s %s", $host, $type, $value);
_debug($new);
unless ($DRY_RUN) {
my $res = $ua->post(
$url,
"Content-Type" => "application/json",
@ -200,9 +206,11 @@ sub check_and_update_record($$$$$) {
warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success;
}
}
}
sub delete_record($$) {
my ($zone, $record) = @_;
return if $DRY_RUN;
my $url = $in->[0]->{defaults}->{api} . "/$zone/records/$record->{host}/$record->{type}?host=$record->{host}&data=$record->{data}";
my $res = $ua->delete($url);