Delete device from CLI with new "delete" option to netdisco-do

This commit is contained in:
Oliver Gorwits
2015-02-04 13:54:00 +00:00
parent af7f36774a
commit aabc65b87a
6 changed files with 68 additions and 17 deletions

View File

@@ -40,6 +40,8 @@ __PACKAGE__->add_columns(
},
);
__PACKAGE__->set_primary_key("id");
=head1 ADDITIONAL COLUMNS
=head2 creation_stamp

View File

@@ -11,15 +11,16 @@ use base 'DBIx::Class::Core';
__PACKAGE__->table("device_port_wireless");
__PACKAGE__->add_columns(
"ip",
{ data_type => "inet", is_nullable => 1 },
{ data_type => "inet", is_nullable => 0 },
"port",
{ data_type => "text", is_nullable => 1 },
{ data_type => "text", is_nullable => 0 },
"channel",
{ data_type => "integer", is_nullable => 1 },
"power",
{ data_type => "integer", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("port", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:T5GmnCj/9BB7meiGZ3xN7g

View File

@@ -8,6 +8,7 @@ use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
get_device
delete_device
check_device_no
check_device_only
is_discoverable
@@ -61,6 +62,39 @@ sub get_device {
->find_or_new({ip => $ip});
}
=head2 delete_device( $ip, $archive? )
Given an IP address, deletes the device from Netdisco, including all related
data such as logs and nodes. If the C<$archive> parameter is true, then nodes
will be maintained in an archive state.
Returns true if the transaction completes, else returns false.
=cut
sub delete_device {
my ($ip, $archive, $log) = @_;
my $device = get_device($ip) or return 0;
return 0 if not $device->in_storage;
my $happy = 0;
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('UserLog')->create({
username => session('logged_in_user'),
userip => scalar eval {request->remote_address},
event => "Delete device ". $device->ip ." ($ip)",
details => $log,
});
# will delete everything related too...
schema('netdisco')->resultset('Device')
->search({ ip => $device->ip })->delete({archive_nodes => $archive});
$happy = 1;
});
return $happy;
}
=head2 check_device_no( $ip, $setting_name )
Given the IP address of a device, returns true if the configuration setting

View File

@@ -6,6 +6,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::JobQueue 'jq_insert';
use App::Netdisco::Util::Device 'delete_device';
sub add_job {
my ($action, $device, $subaction) = @_;
@@ -48,20 +49,9 @@ ajax '/ajax/control/admin/delete' => require_role admin => sub {
send_error('Bad device', 400)
if ! $device or $device->addr eq '0.0.0.0';
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('UserLog')->create({
username => session('logged_in_user'),
userip => request->remote_address,
event => "Delete device ". $device->addr,
details => param('log'),
});
my $device = schema('netdisco')->resultset('Device')
->search({ip => param('device')});
# will delete everything related too...
$device->delete({archive_nodes => param('archive')});
});
return delete_device(
$device->addr, param('archive'), param('log'),
);
};
get '/admin/*' => require_role admin => sub {