tidy util code
This commit is contained in:
@@ -15,30 +15,35 @@ __PACKAGE__->add_columns(
|
|||||||
"mac",
|
"mac",
|
||||||
{ data_type => "macaddr", is_nullable => 0,
|
{ data_type => "macaddr", is_nullable => 0,
|
||||||
extra => { descr => 'MAC address' } },
|
extra => { descr => 'MAC address' } },
|
||||||
|
|
||||||
"ip",
|
"ip",
|
||||||
{ data_type => "inet", is_nullable => 0,
|
{ data_type => "inet", is_nullable => 0,
|
||||||
extra => { descr => 'IP address' } },
|
extra => { descr => 'IP address' } },
|
||||||
|
|
||||||
"dns",
|
"dns",
|
||||||
{ data_type => "text", is_nullable => 1,
|
{ data_type => "text", is_nullable => 1,
|
||||||
extra => { descr => 'FQDN of the node' } },
|
extra => { descr => 'FQDN of the node' } },
|
||||||
|
|
||||||
"active",
|
"active",
|
||||||
{ data_type => "boolean", is_nullable => 1,
|
{ data_type => "boolean", is_nullable => 1,
|
||||||
extra => { descr => 'Whether the entry is still "fresh"' } },
|
extra => { descr => 'Whether the entry is still "fresh"' } },
|
||||||
|
|
||||||
"time_first",
|
"time_first",
|
||||||
{
|
{
|
||||||
data_type => "timestamp",
|
data_type => "timestamp",
|
||||||
default_value => \"current_timestamp",
|
default_value => \"current_timestamp",
|
||||||
is_nullable => 1,
|
is_nullable => 1,
|
||||||
original => { default_value => \"now()" },
|
original => { default_value => \"now()" },
|
||||||
extra => { hide_from_api => 1 },
|
extra => { descr => 'When first seen on the network' },
|
||||||
},
|
},
|
||||||
|
|
||||||
"time_last",
|
"time_last",
|
||||||
{
|
{
|
||||||
data_type => "timestamp",
|
data_type => "timestamp",
|
||||||
default_value => \"current_timestamp",
|
default_value => \"current_timestamp",
|
||||||
is_nullable => 1,
|
is_nullable => 1,
|
||||||
original => { default_value => \"now()" },
|
original => { default_value => \"now()" },
|
||||||
extra => { hide_from_api => 1 },
|
extra => { descr => 'When last seen on the network' },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
__PACKAGE__->set_primary_key("mac", "ip");
|
__PACKAGE__->set_primary_key("mac", "ip");
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package App::Netdisco::Util::API;
|
package App::Netdisco::Util::API;
|
||||||
|
|
||||||
use strict;
|
|
||||||
use warnings;
|
|
||||||
use Dancer ':syntax';
|
use Dancer ':syntax';
|
||||||
use Dancer::Plugin::DBIC 'schema';
|
use Dancer::Plugin::DBIC 'schema';
|
||||||
|
|
||||||
@@ -12,8 +10,6 @@ our @EXPORT = ();
|
|||||||
our @EXPORT_OK = qw/
|
our @EXPORT_OK = qw/
|
||||||
resultsource_to_openapi_params
|
resultsource_to_openapi_params
|
||||||
parse_search_params
|
parse_search_params
|
||||||
format_data
|
|
||||||
format_error
|
|
||||||
/;
|
/;
|
||||||
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
||||||
|
|
||||||
@@ -26,13 +22,15 @@ sub resultsource_to_openapi_params {
|
|||||||
|
|
||||||
foreach my $col ($rs->primary_columns,
|
foreach my $col ($rs->primary_columns,
|
||||||
(singleton ($rs->primary_columns, keys %{ $columns }))) {
|
(singleton ($rs->primary_columns, keys %{ $columns }))) {
|
||||||
|
|
||||||
my $data = $columns->{$col};
|
my $data = $columns->{$col};
|
||||||
next if $data->{extra}->{hide_from_api};
|
next if $data->{extra}->{hide_from_api};
|
||||||
|
|
||||||
push @params, (
|
push @params, (
|
||||||
$col => {
|
$col => {
|
||||||
description => $data->{extra}->{descr},
|
description => $data->{extra}->{descr},
|
||||||
type => ($data->{data_type} =~ m/int/ ? 'integer'
|
type => ($data->{data_type} =~ m/int/ ? 'integer' :
|
||||||
: $data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
|
$data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -41,62 +39,26 @@ sub resultsource_to_openapi_params {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub parse_search_params {
|
sub parse_search_params {
|
||||||
my $params = shift;
|
my $sourcename = shift or return {};
|
||||||
my $search = {};
|
my $params = shift or return {};
|
||||||
my $partial = $params->{partial} || false;
|
my @pspec = resultsource_to_openapi_params($sourcename) or return {};
|
||||||
|
|
||||||
foreach my $param (keys %{$params}) {
|
my $partial = $params->{partial} || false;
|
||||||
if ($param ne 'return_url' and $param ne 'partial') {
|
my $search = {};
|
||||||
if ($partial eq 'true') {
|
|
||||||
$search->{"text(".$param.")"} = { -ilike => '%'.$params->{$param}.'%'};
|
foreach my $param (@pspec) {
|
||||||
|
next unless exists $params->{$param};
|
||||||
|
|
||||||
|
if ($partial) {
|
||||||
|
$search->{'text('. quotemeta($param) .')'}
|
||||||
|
= { -ilike => '%'. $params->{$param} .'%'};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$search->{$param} = $params->{$param};
|
$search->{$param} = $params->{$param};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $search;
|
return $search;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub format_data {
|
|
||||||
my $items = shift;
|
|
||||||
my $results = {};
|
|
||||||
|
|
||||||
if (ref($items) =~ m/ResultSet/) {
|
|
||||||
my @hashes;
|
|
||||||
|
|
||||||
foreach my $item ($items->all) {
|
|
||||||
my $c = {};
|
|
||||||
my $columns = $item->{_column_data};
|
|
||||||
|
|
||||||
foreach my $col (keys %{$columns}) {
|
|
||||||
$c->{$col} = $columns->{$col};
|
|
||||||
}
|
|
||||||
|
|
||||||
push @hashes, $c;
|
|
||||||
}
|
|
||||||
|
|
||||||
$results->{data} = \@hashes;
|
|
||||||
}
|
|
||||||
elsif (ref($items) =~ m/Result/) {
|
|
||||||
$results->{data} = $items->{_column_data};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$results->{data} = $items;
|
|
||||||
}
|
|
||||||
|
|
||||||
header('Content-Type' => 'application/json');
|
|
||||||
return to_json $results;
|
|
||||||
};
|
|
||||||
|
|
||||||
sub format_error {
|
|
||||||
my $status = shift;
|
|
||||||
my $message = shift;
|
|
||||||
|
|
||||||
status $status;
|
|
||||||
header('Content-Type' => 'application/json');
|
|
||||||
return to_json { error => $message };
|
|
||||||
}
|
|
||||||
|
|
||||||
true;
|
true;
|
||||||
|
|||||||
Reference in New Issue
Block a user