From 95f0148af94e4c654560e7ddbe049584a8e270fc Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Wed, 16 Jun 2021 17:01:01 +0100 Subject: [PATCH] Implement dry-run --- manage-dns.pl | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/manage-dns.pl b/manage-dns.pl index dbcb749..19aa768 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -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,31 +181,36 @@ sub check_and_update_record($$$$$) { # Update the record $record->{ttl} = $in->[0]->{defaults}->{ttl}->{$zone}; _debug("Update ", $url, $record, to_json($record)); - my $res = $ua->put( - $url, - "Content-Type" => "application/json", - "Content" => to_json({ records => [ $record ] }), - ); - warn "Failed to update $url: " . $res->status_line unless $res->is_success; + unless ($DRY_RUN) { + my $res = $ua->put( + $url, + "Content-Type" => "application/json", + "Content" => to_json({ records => [ $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); - my $res = $ua->post( - $url, - "Content-Type" => "application/json", - Content => to_json({ - records => [ $new ] - }) - ); - warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; + unless ($DRY_RUN) { + my $res = $ua->post( + $url, + "Content-Type" => "application/json", + Content => to_json({ + records => [ $new ] + }) + ); + 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);