Implement dry-run
This commit is contained in:
		@@ -13,6 +13,7 @@ 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;
 | 
					my %seen;
 | 
				
			||||||
 | 
					my $DRY_RUN = $ENV{DRY_RUN};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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};
 | 
				
			||||||
@@ -51,6 +52,8 @@ sub is_unsupported($) {
 | 
				
			|||||||
    return $supported_types{$type};
 | 
					    return $supported_types{$type};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_notice("Dry run - no changes will be applied") if ${DRY_RUN};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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(
 | 
				
			||||||
@@ -178,6 +181,7 @@ sub check_and_update_record($$$$$) {
 | 
				
			|||||||
            # Update the record
 | 
					            # Update the record
 | 
				
			||||||
            $record->{ttl} = $in->[0]->{defaults}->{ttl}->{$zone};
 | 
					            $record->{ttl} = $in->[0]->{defaults}->{ttl}->{$zone};
 | 
				
			||||||
            _debug("Update ", $url, $record, to_json($record));
 | 
					            _debug("Update ", $url, $record, to_json($record));
 | 
				
			||||||
 | 
					            unless ($DRY_RUN) {
 | 
				
			||||||
                my $res = $ua->put(
 | 
					                my $res = $ua->put(
 | 
				
			||||||
                    $url,
 | 
					                    $url,
 | 
				
			||||||
                    "Content-Type" => "application/json",
 | 
					                    "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;
 | 
					                warn "Failed to update $url: " . $res->status_line unless $res->is_success;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        # Create new record
 | 
					        # Create new record
 | 
				
			||||||
        my $new = format_record($zone, $type, $host, $value);
 | 
					        my $new = format_record($zone, $type, $host, $value);
 | 
				
			||||||
        _notice("Created new record: %s %s %s", $host, $type, $value);
 | 
					        _notice("Created new record: %s %s %s", $host, $type, $value);
 | 
				
			||||||
        _debug($new);
 | 
					        _debug($new);
 | 
				
			||||||
 | 
					        unless ($DRY_RUN) {
 | 
				
			||||||
            my $res = $ua->post(
 | 
					            my $res = $ua->post(
 | 
				
			||||||
                $url,
 | 
					                $url,
 | 
				
			||||||
                "Content-Type" => "application/json",
 | 
					                "Content-Type" => "application/json",
 | 
				
			||||||
@@ -199,10 +205,12 @@ sub check_and_update_record($$$$$) {
 | 
				
			|||||||
            );
 | 
					            );
 | 
				
			||||||
            warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success;
 | 
					            warn "Failed to create $url: " . $res->status_line . "\n" . $res->content unless $res->is_success;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sub delete_record($$) {
 | 
					sub delete_record($$) {
 | 
				
			||||||
    my ($zone, $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 $url = $in->[0]->{defaults}->{api} . "/$zone/records/$record->{host}/$record->{type}?host=$record->{host}&data=$record->{data}";
 | 
				
			||||||
    my $res = $ua->delete($url);
 | 
					    my $res = $ua->delete($url);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user