device search supports IP prefix

This commit is contained in:
Oliver Gorwits
2012-01-22 17:43:01 +00:00
parent 1d1a5270c9
commit 2871fa813b

View File

@@ -43,7 +43,7 @@ sub by_field {
return $set unless ref {} eq ref $p; return $set unless ref {} eq ref $p;
my $op = $p->{matchall} ? '-and' : '-or'; my $op = $p->{matchall} ? '-and' : '-or';
# this is a bit of a dreadful hack to catch junk entry # this is a bit of an ungraceful backflip to catch junk entry
# whilst avoiding returning all devices in the DB # whilst avoiding returning all devices in the DB
my $ip = ($p->{ip} ? my $ip = ($p->{ip} ?
(NetAddr::IP::Lite->new($p->{ip}) || NetAddr::IP::Lite->new('255.255.255.255')) (NetAddr::IP::Lite->new($p->{ip}) || NetAddr::IP::Lite->new('255.255.255.255'))
@@ -89,6 +89,21 @@ sub by_any {
return $set unless $q; return $set unless $q;
$q = "\%$q\%" if $q !~ m/\%/; $q = "\%$q\%" if $q !~ m/\%/;
# basic IP check is a string match
my $ip_clause = [
'me.ip::text' => { '-ilike' => $q },
'device_ips.alias::text' => { '-ilike' => $q },
];
# but also allow prefix search
(my $qc = $q) =~ s/\%//g;
if (my $ip = NetAddr::IP::Lite->new($qc)) {
$ip_clause = [
'me.ip' => { '<<=' => $ip->cidr },
'device_ips.alias' => { '<<=' => $ip->cidr },
];
}
return $set->search( return $set->search(
{ {
-or => [ -or => [
@@ -101,10 +116,7 @@ sub by_any {
'me.dns' => { '-ilike' => $q }, 'me.dns' => { '-ilike' => $q },
'device_ips.dns' => { '-ilike' => $q }, 'device_ips.dns' => { '-ilike' => $q },
], ],
-or => [ -or => $ip_clause,
'me.ip::text' => { '-ilike' => $q },
'device_ips.alias::text' => { '-ilike' => $q },
],
], ],
}, },
{ {