blib/lib/App/Netdisco/Web/TypeAhead.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 77 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 313 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Web::TypeAhead;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin::Ajax;
5 use Dancer::Plugin::DBIC;
6 use Dancer::Plugin::Auth::Extensible;
7  
8 use App::Netdisco::Util::Web (); # for sort_port
9 use List::MoreUtils ();
10  
11 ajax '/ajax/data/queue/typeahead/backend' => require_role admin => sub {
12 my $q = quotemeta( param('query') || param('term') || param('backend') );
13 my @backends =
14 grep { $q ? m/$q/ : true }
15 List::MoreUtils::uniq
16 sort
17 grep { defined }
18 schema(vars->{'tenant'})->resultset('DeviceSkip')->get_distinct_col('backend');
19  
20 content_type 'application/json';
21 to_json \@backends;
22 };
23  
24 ajax '/ajax/data/queue/typeahead/username' => require_role admin => sub {
25 my $q = quotemeta( param('query') || param('term') || param('username') );
26 my @users =
27 grep { $q ? m/$q/ : true }
28 List::MoreUtils::uniq
29 sort
30 grep { defined }
31 schema(vars->{'tenant'})->resultset('Admin')->get_distinct_col('username');
32  
33 content_type 'application/json';
34 to_json \@users;
35 };
36  
37 ajax '/ajax/data/queue/typeahead/action' => require_role admin => sub {
38 my @actions = ();
39 my @core_plugins = @{ setting('worker_plugins') || [] };
40 my @user_plugins = @{ setting('extra_worker_plugins') || [] };
41  
42 # scan worker plugins for our action
43 foreach my $plugin (@user_plugins, @core_plugins) {
44 $plugin =~ s/^X::/+App::NetdiscoX::Worker::Plugin::/;
45 $plugin = 'App::Netdisco::Worker::Plugin::'. $plugin
46 if $plugin !~ m/^\+/;
47 $plugin =~ s/^\+//;
48  
49 next if $plugin =~ m/::Plugin::Internal::/;
50  
51 if ($plugin =~ m/::Plugin::(Hook::[^:]+)/) {
52 push @actions, lc $1;
53 next;
54 }
55  
56 next if $plugin =~ m/::Plugin::Hook$/;
57 next unless $plugin =~ m/::Plugin::([^:]+)(?:::|$)/;
58  
59 push @actions, lc $1;
60 }
61  
62 push @actions,
63 schema(vars->{'tenant'})->resultset('Admin')->get_distinct_col('action');
64  
65 my $q = quotemeta( param('query') || param('term') || param('action') );
66  
67 content_type 'application/json';
68 to_json [
69 grep { $q ? m/^$q/ : true }
70 grep { defined }
71 List::MoreUtils::uniq
72 sort
73 @actions
74 ];
75 };
76  
77 ajax '/ajax/data/queue/typeahead/status' => require_role admin => sub {
78 my $q = quotemeta( param('query') || param('term') || param('status') );
79 my @actions =
80 grep { $q ? m/^$q/ : true }
81 qw(Queued Running Done Info Deferred Error);
82  
83 content_type 'application/json';
84 to_json \@actions;
85 };
86  
87 ajax '/ajax/data/devicename/typeahead' => require_login sub {
88 content_type 'application/json';
89 return '[]' unless setting('navbar_autocomplete');
90  
91 my $q = (param('query') || param('term')) or return '[]';
92 my $set = schema(vars->{'tenant'})->resultset('Device')
93 ->search_fuzzy($q)->search(undef, {rows => setting('max_typeahead_rows')});
94  
95 to_json [map {($_->dns || $_->name || $_->ip)} $set->all];
96 };
97  
98 ajax '/ajax/data/deviceip/typeahead' => require_login sub {
99 content_type 'application/json';
100 my $q = (param('query') || param('term')) or return '[]';
101 my $set = schema(vars->{'tenant'})->resultset('Device')
102 ->search_fuzzy($q)->search(undef, {rows => setting('max_typeahead_rows')});
103  
104 my @data = ();
105 while (my $d = $set->next) {
106 my $label = $d->ip;
107 if ($d->dns or $d->name) {
108 $label = sprintf '%s (%s)',
109 ($d->dns || $d->name), $d->ip;
110 }
111 push @data, { label => $label, value => $d->ip };
112 }
113  
114 content_type 'application/json';
115 to_json \@data;
116 };
117  
118 ajax '/ajax/data/devices/typeahead' => require_login sub {
119 content_type 'application/json';
120 my $q = (param('left_rule') || param('right_rule')) or return '[]';
121 my $mode = param('aclhost') || 'ip';
122 if ($mode eq 'dynamic') {
123 $mode = (($q =~ m/^\d/ or $q =~ m/:/) ? 'ip' : 'name');
124 }
125  
126 my @data = ();
127  
128 # ACL Manager host ACLs are in host_groups too, put there by the loader
129 my @host_groups = sort {$a cmp $b}
130 grep {$_ !~ m/^synthesized_group_/}
131 keys %{ setting('host_groups')};
132  
133 # if q starts group: then search for host groups (excluding synthesized)
134 if ($q =~ m/^(?:group:|acl:)/i) {
135 return to_json [ map { 'group:'. $_ } @host_groups ];
136 }
137  
138 if (scalar grep { $_ =~ m/\Q$q\E/i } @host_groups) {
139 push @data, map { 'group:'. $_ }
140 grep { $_ =~ m/\Q$q\E/i } @host_groups
141 }
142  
143 my $set = schema(vars->{'tenant'})->resultset('Device')
144 ->search_fuzzy($q)->search(undef, {rows => setting('max_typeahead_rows')});
145  
146 while (my $d = $set->next) {
147 my $name = ($d->dns || $d->name);
148  
149 if (not $name or $mode eq 'ip') {
150 push @data, { value => $d->ip, label => ($name ? sprintf('%s (%s)', $d->ip, $name) : $d->ip) };
151 }
152 elsif ($mode eq 'name') {
153 push @data, { value => $name, label => sprintf('%s (%s)', $name, $d->ip) };
154 }
155 }
156  
157 return to_json \@data;
158 };
159  
160 ajax '/ajax/data/port/typeahead' => require_login sub {
161 my $dev = param('dev1') || param('dev2');
162 my $port = param('port1') || param('port2');
163 send_error('Missing device', 400) unless $dev;
164  
165 my $device = schema(vars->{'tenant'})->resultset('Device')
166 ->find({ip => $dev});
167 send_error('Bad device', 400) unless $device;
168  
169 my $set = $device->ports({},{order_by => 'port'});
170 $set = $set->search({port => { -ilike => "\%$port\%" }})
171 if $port;
172  
173 my $results = [
174 map {{ label => (sprintf "%s (%s)", $_->port, ($_->name || '')), value => $_->port }}
175 sort { &App::Netdisco::Util::Web::sort_port($a->port, $b->port) } $set->all
176 ];
177  
178 content_type 'application/json';
179 to_json \@$results;
180 };
181  
182 ajax '/ajax/data/subnet/typeahead' => require_login sub {
183 my $q = param('query') || param('term');
184 $q = "$q\%" if $q !~ m/\%/;
185 my $nets = schema(vars->{'tenant'})->resultset('Subnet')->search(
186 { 'me.net::text' => { '-ilike' => $q }},
187 { columns => ['net'], distinct => 1, order_by => 'net', rows => setting('max_typeahead_rows') } );
188  
189 content_type 'application/json';
190 to_json [map {$_->net} $nets->all];
191 };
192  
193 true;