add frontend update/del forms, and display port count
This commit is contained in:
@@ -180,6 +180,16 @@ __PACKAGE__->has_many(
|
||||
|
||||
=head1 ADDITIONAL COLUMNS
|
||||
|
||||
=head2 port_count
|
||||
|
||||
Returns the number of ports on this device. Enable this
|
||||
column by applying the C<with_port_count()> modifier to C<search()>.
|
||||
|
||||
=cut
|
||||
|
||||
sub port_count { return (shift)->get_column('port_count') }
|
||||
|
||||
|
||||
=head2 uptime_age
|
||||
|
||||
Formatted version of the C<uptime> field.
|
||||
|
||||
@@ -491,4 +491,34 @@ sub get_distinct_col {
|
||||
)->get_column($col)->all;
|
||||
}
|
||||
|
||||
=head2 with_port_count
|
||||
|
||||
This is a modifier for any C<search()> which
|
||||
will add the following additional synthesized column to the result set:
|
||||
|
||||
=over 4
|
||||
|
||||
=item port_count
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
sub with_port_count {
|
||||
my ($rs, $cond, $attrs) = @_;
|
||||
|
||||
return $rs
|
||||
->search_rs($cond, $attrs)
|
||||
->search({},
|
||||
{
|
||||
'+columns' => { port_count =>
|
||||
$rs->result_source->schema->resultset('DevicePort')
|
||||
->search(
|
||||
{ 'dp.ip' => { -ident => 'me.ip' } },
|
||||
{ alias => 'dp' }
|
||||
)->count_rs->as_query
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
@@ -5,18 +5,64 @@ use Dancer::Plugin::Ajax;
|
||||
use Dancer::Plugin::DBIC;
|
||||
|
||||
use App::Netdisco::Web::Plugin;
|
||||
use NetAddr::IP::Lite ':lower';
|
||||
use Try::Tiny;
|
||||
|
||||
register_admin_task({
|
||||
tag => 'pseudodevice',
|
||||
label => 'Manage Pseudo Devices',
|
||||
});
|
||||
|
||||
sub _sanity_ok {
|
||||
my $happy = 0;
|
||||
|
||||
try {
|
||||
return 0 unless length param('dns')
|
||||
and param('dns') =~ m/^[[:print:]]+$/
|
||||
and param('dns') !~ m/[[:space:]]/;
|
||||
|
||||
my $ip = NetAddr::IP::Lite->new(param('ip'));
|
||||
return 0 if $ip->addr eq '0.0.0.0';
|
||||
|
||||
return 0 unless length param('ports')
|
||||
and param('ports') =~ m/^[[:digit:]]+$/;
|
||||
|
||||
$happy = 1;
|
||||
};
|
||||
|
||||
return $happy;
|
||||
}
|
||||
|
||||
ajax '/ajax/content/admin/pseudodevice/add' => sub {
|
||||
forward '/ajax/content/admin/pseudodevice'
|
||||
unless _sanity_ok();
|
||||
|
||||
try {
|
||||
schema('netdisco')->txn_do(sub {
|
||||
my $device = schema('netdisco')->resultset('Device')
|
||||
->create({
|
||||
ip => param('ip'),
|
||||
dns => param('dns'),
|
||||
vendor => 'netdisco',
|
||||
last_discover => \'now()',
|
||||
});
|
||||
|
||||
$device->ports->populate([
|
||||
['port'],
|
||||
map {["Port$_"]} @{[1 .. param('ports')]},
|
||||
]);
|
||||
});
|
||||
};
|
||||
|
||||
forward '/ajax/content/admin/pseudodevice';
|
||||
};
|
||||
|
||||
ajax '/ajax/content/admin/pseudodevice' => sub {
|
||||
my $set = schema('netdisco')->resultset('Device')
|
||||
->search(
|
||||
{vendor => 'netdisco'},
|
||||
{order_by => { -desc => 'last_discover' }},
|
||||
);
|
||||
)->with_port_count;
|
||||
|
||||
content_type('text/html');
|
||||
template 'ajax/admintask/pseudodevice.tt', {
|
||||
|
||||
@@ -196,6 +196,10 @@ td {
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.nd_inline_form {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
/* tabs */
|
||||
|
||||
|
||||
@@ -20,11 +20,18 @@
|
||||
</tr>
|
||||
[% WHILE (row = results.next) %]
|
||||
<tr>
|
||||
<form name="update">
|
||||
<td class="center_cell"><a class="nd_linkcell"
|
||||
href="[% device_ports %]&q=[% row.dns | uri %]">[% row.dns | html_entity %]</a></td>
|
||||
<td class="center_cell">[% row.ip | html_entity %]</td>
|
||||
<td class="center_cell">[% row.ports.count | html_entity %]</td>
|
||||
<td class="center_cell"><a class="btn btn-small" href="#"><i class="icon-plus-sign"></i> Add</a></td>
|
||||
<td class="center_cell"><input name="ports" type="number" value="[% row.port_count | html_entity %]"</td>
|
||||
<td class="center_cell">
|
||||
<button class="btn" name="update" type="submit"><i class="icon-save text-warning"></i></button>
|
||||
</form>
|
||||
<form name="del" class="nd_inline_form">
|
||||
<button class="btn" name="del" type="submit"><i class="icon-trash text-error"></i></button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
[% END %]
|
||||
</tbody>
|
||||
|
||||
Reference in New Issue
Block a user