From a7031753d26742913a58f88c44b10bc1278c7930 Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Sat, 2 Sep 2023 14:48:49 +0100 Subject: [PATCH 1/5] abstract to delete, create, update queues --- manage-dns.pl | 73 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/manage-dns.pl b/manage-dns.pl index d947810..0f84df2 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -15,6 +15,10 @@ my $ua = LWP::UserAgent->new; my %seen; my $DRY_RUN = $ENV{DRY_RUN}; +my @to_create; +my @to_delete; +my @to_update; + sub _debug { print STDERR ("=== DEBUG ===\n", Dumper(@_), "=== END ===\n") if $ENV{DEBUG} or $in->[0]->{debug}; } @@ -204,41 +208,22 @@ sub check_and_update_record($$$$$) { if($record->{ttl} ne $in->[0]->{defaults}->{ttl}->{$zone}) { # 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", - "Content" => to_json({ records => [ $record ] }), - ); - warn "Failed to update $url: " . $res->status_line unless $res->is_success; - } + _debug("Will update ", $url, $record, to_json($record)); + push(@to_update, $record); } } 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", - Content => to_json({ - records => [ $new ] - }) - ); - warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; - } + _notice("Will create new record: %s %s %s", $host, $type, $value); + push(@to_create, $new); } } 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); - warn "Failed to delete $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; + _notice("Will delete: %s %s %s", $record->{host}, $record->{type}, $record->{data}); + push(@to_delete, $url); } foreach my $z (keys %{$in->[0]->{zones}}) { @@ -297,5 +282,41 @@ foreach my $z (keys %{$in->[0]->{zones}}) { } } } - _info("Finished processing %s", $z); + _info("Finished pre-processing %s", $z); +} + +unless($DRY_RUN) { + _info("Applying changes (%d creates, %d updates, %d deletes)", scalar @to_create, scalar @to_update, scalar @to_delete); + + # Delete + foreach my $url (@to_delete) { + my $res = $ua->delete($url); + warn "Failed to delete $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; + } + + # Create + foreach my $record (@to_create) { + my $res = $ua->post( + $url, + "Content-Type" => "application/json", + Content => to_json({ + records => [ $record ] + }) + ); + warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; + } + + # Update + foreach my $record (@to_update) { + 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; + } + + _info("Finished applying changes") +} else { + _info("DRY RUN: Skipped applying changes (%d creates, %d updates, %d deletes)", scalar @to_create, scalar @to_update, scalar @to_delete); } -- 2.47.1 From f1617977a95ff8f9541f434427bf5b6b93e12064 Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Sat, 2 Sep 2023 14:52:53 +0100 Subject: [PATCH 2/5] Pass url through to create and update --- manage-dns.pl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/manage-dns.pl b/manage-dns.pl index 0f84df2..57c7719 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -209,13 +209,13 @@ sub check_and_update_record($$$$$) { # Update the record $record->{ttl} = $in->[0]->{defaults}->{ttl}->{$zone}; _debug("Will update ", $url, $record, to_json($record)); - push(@to_update, $record); + push(@to_update, [$url,$record]); } } else { # Create new record my $new = format_record($zone, $type, $host, $value); _notice("Will create new record: %s %s %s", $host, $type, $value); - push(@to_create, $new); + push(@to_create, [$url,$new]); } } @@ -297,10 +297,10 @@ unless($DRY_RUN) { # Create foreach my $record (@to_create) { my $res = $ua->post( - $url, + $record[0], "Content-Type" => "application/json", Content => to_json({ - records => [ $record ] + records => [ $record[1] ] }) ); warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; @@ -309,9 +309,9 @@ unless($DRY_RUN) { # Update foreach my $record (@to_update) { my $res = $ua->put( - $url, + $record[0], "Content-Type" => "application/json", - "Content" => to_json({ records => [ $record ] }), + "Content" => to_json({ records => [ $record[1] ] }), ); warn "Failed to update $url: " . $res->status_line unless $res->is_success; } -- 2.47.1 From d43ef34abc56cec87d288d90cac1c791bc4676a5 Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Sat, 2 Sep 2023 14:54:39 +0100 Subject: [PATCH 3/5] Dereference record --- manage-dns.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/manage-dns.pl b/manage-dns.pl index 57c7719..cfe3528 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -297,10 +297,10 @@ unless($DRY_RUN) { # Create foreach my $record (@to_create) { my $res = $ua->post( - $record[0], + $record->[0], "Content-Type" => "application/json", Content => to_json({ - records => [ $record[1] ] + records => [ $record->[1] ] }) ); warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; @@ -309,9 +309,9 @@ unless($DRY_RUN) { # Update foreach my $record (@to_update) { my $res = $ua->put( - $record[0], + $record->[0], "Content-Type" => "application/json", - "Content" => to_json({ records => [ $record[1] ] }), + "Content" => to_json({ records => [ $record->[1] ] }), ); warn "Failed to update $url: " . $res->status_line unless $res->is_success; } -- 2.47.1 From cb62c68633c7ccc761c87d64404797bf124a0322 Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Sat, 2 Sep 2023 14:55:35 +0100 Subject: [PATCH 4/5] and messaging --- manage-dns.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manage-dns.pl b/manage-dns.pl index cfe3528..155afe9 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -303,7 +303,7 @@ unless($DRY_RUN) { records => [ $record->[1] ] }) ); - warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success; + warn "Failed to create $record->[0]: " . $res->status_line . "\n" . $res->content unless $res->is_success; } # Update @@ -313,7 +313,7 @@ unless($DRY_RUN) { "Content-Type" => "application/json", "Content" => to_json({ records => [ $record->[1] ] }), ); - warn "Failed to update $url: " . $res->status_line unless $res->is_success; + warn "Failed to update $record->[0]: " . $res->status_line unless $res->is_success; } _info("Finished applying changes") -- 2.47.1 From b1767bdaaec62b8d97173df351b38ae19e13aef0 Mon Sep 17 00:00:00 2001 From: Matthew Slowe Date: Sat, 2 Sep 2023 14:59:10 +0100 Subject: [PATCH 5/5] Extraneous logging --- manage-dns.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manage-dns.pl b/manage-dns.pl index 155afe9..4ae2811 100755 --- a/manage-dns.pl +++ b/manage-dns.pl @@ -277,7 +277,7 @@ foreach my $z (keys %{$in->[0]->{zones}}) { } } unless ($skip) { - _notice("Delete %s %s %s", $record->{host}, $record->{type}, $record->{data}); + # _notice("Will delete %s %s %s", $record->{host}, $record->{type}, $record->{data}); delete_record($z, $record); } } -- 2.47.1