implement "no devices" prompt for admin users to do first discover

This commit is contained in:
Oliver Gorwits
2013-05-09 23:30:32 +01:00
parent 2e8ac83173
commit 70f5da8bb6
12 changed files with 231 additions and 6 deletions

View File

@@ -55,4 +55,46 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key("job");
# You can replace this text with custom code or comments, and it will be preserved on regeneration
=head1 ADDITIONAL COLUMNS
=head2 entererd_stamp
Formatted version of the C<entered> field, accurate to the minute.
The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
between the date stamp and time stamp. That is:
2012-02-06 12:49
=cut
sub entered_stamp { return (shift)->get_column('entered_stamp') }
=head2 started_stamp
Formatted version of the C<started> field, accurate to the minute.
The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
between the date stamp and time stamp. That is:
2012-02-06 12:49
=cut
sub started_stamp { return (shift)->get_column('started_stamp') }
=head2 finished_stamp
Formatted version of the C<finished> field, accurate to the minute.
The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
between the date stamp and time stamp. That is:
2012-02-06 12:49
=cut
sub finished_stamp { return (shift)->get_column('finished_stamp') }
1;

View File

@@ -0,0 +1,41 @@
package App::Netdisco::DB::ResultSet::Admin;
use base 'DBIx::Class::ResultSet';
use strict;
use warnings FATAL => 'all';
=head1 ADDITIONAL METHODS
=head2 with_times
This is a modifier for any C<search()> (including the helpers below) which
will add the following additional synthesized columns to the result set:
=over 4
=item entered_stamp
=item started_stamp
=item finished_stamp
=back
=cut
sub with_times {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{
'+columns' => {
entered_stamp => \"to_char(entered, 'YYYY-MM-DD HH24:MI')",
started_stamp => \"to_char(started, 'YYYY-MM-DD HH24:MI')",
finished_stamp => \"to_char(finished, 'YYYY-MM-DD HH24:MI')",
},
});
}
1;

View File

@@ -16,6 +16,7 @@ use App::Netdisco::Web::Report;
use App::Netdisco::Web::AdminTask;
use App::Netdisco::Web::TypeAhead;
use App::Netdisco::Web::PortControl;
use App::Netdisco::Web::JobControl;
sub _load_web_plugins {
my $plugin_list = shift;
@@ -54,6 +55,12 @@ hook 'before_template' => sub {
};
get '/' => sub {
if (var('user') and var('user')->admin) {
if (schema('netdisco')->resultset('Device')->count == 0) {
var('nodevices' => true);
}
}
template 'index';
};

View File

@@ -0,0 +1,25 @@
package App::Netdisco::Web::JobControl;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
post '/admin/discover' => sub {
return unless var('user') and var('user')->admin;
my $ip = NetAddr::IP::Lite->new(param('device'));
return unless $ip
and $ip->addr ne '0.0.0.0';
schema('netdisco')->resultset('Admin')->create({
device => $ip->addr,
action => 'discover',
status => 'queued',
username => session('user'),
userip => request->remote_address,
});
status(302);
header(Location => uri_for('/admin/jobqueue')->path_query());
};
true;

View File

@@ -0,0 +1,27 @@
package App::Netdisco::Web::Plugin::AdminTask::JobQueue;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
register_admin_task({
tag => 'jobqueue',
label => 'Job Queue',
});
ajax '/ajax/content/admin/jobqueue' => sub {
return unless var('user') and var('user')->admin;
my $set = schema('netdisco')->resultset('Admin')
->with_times
->search({}, {order_by => { -desc => [qw/entered device action/] }});
content_type('text/html');
template 'ajax/admintask/jobqueue.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -13,7 +13,7 @@ register_admin_task({
});
sub _sanity_ok {
return 0 unless var('user')->admin;
return 0 unless var('user') and var('user')->admin;
return 0 unless length param('dns')
and param('dns') =~ m/^[[:print:]]+$/
@@ -86,7 +86,7 @@ ajax '/ajax/content/admin/pseudodevice/update' => sub {
};
ajax '/ajax/content/admin/pseudodevice' => sub {
return unless var('user')->admin;
return unless var('user') and var('user')->admin;
my $set = schema('netdisco')->resultset('Device')
->search(

View File

@@ -13,7 +13,7 @@ register_admin_task({
});
sub _sanity_ok {
return 0 unless var('user')->admin;
return 0 unless var('user') and var('user')->admin;
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0');
@@ -54,7 +54,7 @@ ajax '/ajax/content/admin/topology/del' => sub {
};
ajax '/ajax/content/admin/topology' => sub {
return unless var('user')->admin;
return unless var('user') and var('user')->admin;
my $set = schema('netdisco')->resultset('Topology')
->search({},{order_by => [qw/dev1 dev2 port1/]});