« blib/lib/App/Netdisco/DB/ResultSet/Subnet.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/BigIP.pm »
| 1 | package App::Netdisco::Web::Plugin; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin; | ||
| 5 | use Dancer::Plugin::Swagger; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use Path::Class 'dir'; | ||
| 9 | use List::Util 'pairs'; | ||
| 10 | use Storable 'dclone'; | ||
| 11 | |||
| 12 | set( | ||
| 13 | '_additional_css' => [], | ||
| 14 | '_additional_javascript' => [], | ||
| 15 | '_extra_device_port_cols' => [], | ||
| 16 | '_extra_device_details' => [], | ||
| 17 | '_navbar_items' => [], | ||
| 18 | '_search_tabs' => [], | ||
| 19 | '_device_tabs' => [], | ||
| 20 | '_admin_tasks' => {}, | ||
| 21 | '_admin_order' => [], | ||
| 22 | '_reports_menu' => {}, | ||
| 23 | '_reports' => {}, | ||
| 24 | '_report_order' => [qw/Device Port IP Node VLAN Network Wireless/, 'My Reports'], | ||
| 25 | ); | ||
| 26 | |||
| 27 | # this is what Dancer::Template::TemplateToolkit does by default | ||
| 28 | config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('views') ]; | ||
| 29 | |||
| 30 | register 'register_template_path' => sub { | ||
| 31 | my ($self, $path) = plugin_args(@_); | ||
| 32 | |||
| 33 | if (!$path) { | ||
| 34 | return error "bad template path to register_template_paths"; | ||
| 35 | } | ||
| 36 | |||
| 37 | unshift @{ config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} }, | ||
| 38 | dir($path, 'views')->stringify; | ||
| 39 | }; | ||
| 40 | |||
| 41 | sub _register_include { | ||
| 42 | my ($type, $plugin) = @_; | ||
| 43 | |||
| 44 | if (!$type) { | ||
| 45 | return error "bad type to _register_include"; | ||
| 46 | } | ||
| 47 | |||
| 48 | if (!$plugin) { | ||
| 49 | return error "bad plugin name to register_$type"; | ||
| 50 | } | ||
| 51 | |||
| 52 | push @{ setting("_additional_$type") }, $plugin; | ||
| 53 | } | ||
| 54 | |||
| 55 | register 'register_css' => sub { | ||
| 56 | my ($self, $plugin) = plugin_args(@_); | ||
| 57 | _register_include('css', $plugin); | ||
| 58 | }; | ||
| 59 | |||
| 60 | register 'register_javascript' => sub { | ||
| 61 | my ($self, $plugin) = plugin_args(@_); | ||
| 62 | _register_include('javascript', $plugin); | ||
| 63 | }; | ||
| 64 | |||
| 65 | register 'register_device_port_column' => sub { | ||
| 66 | my ($self, $config) = plugin_args(@_); | ||
| 67 | $config->{default} ||= ''; | ||
| 68 | $config->{position} ||= 'right'; | ||
| 69 | |||
| 70 | if (!$config->{name} or !$config->{label}) { | ||
| 71 | debug $config; | ||
| 72 | return error "bad config to register_device_port_column"; | ||
| 73 | } | ||
| 74 | |||
| 75 | foreach my $item (@{ setting('_extra_device_port_cols') }) { | ||
| 76 | if ($item->{name} eq $config->{name}) { | ||
| 77 | $item = $config; | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | push @{ setting('_extra_device_port_cols') }, $config; | ||
| 83 | }; | ||
| 84 | |||
| 85 | register 'register_device_details' => sub { | ||
| 86 | my ($self, $config) = plugin_args(@_); | ||
| 87 | |||
| 88 | if (!$config->{name} or !$config->{label}) { | ||
| 89 | debug $config; | ||
| 90 | return error "bad config to register_device_details"; | ||
| 91 | } | ||
| 92 | |||
| 93 | foreach my $item (@{ setting('_extra_device_details') }) { | ||
| 94 | if ($item->{name} eq $config->{name}) { | ||
| 95 | $item = $config; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | push @{ setting('_extra_device_details') }, $config; | ||
| 101 | }; | ||
| 102 | |||
| 103 | register 'register_navbar_item' => sub { | ||
| 104 | my ($self, $config) = plugin_args(@_); | ||
| 105 | |||
| 106 | if (!$config->{tag} | ||
| 107 | or !$config->{path} | ||
| 108 | or !$config->{label}) { | ||
| 109 | |||
| 110 | debug $config; | ||
| 111 | return error "bad config to register_navbar_item"; | ||
| 112 | } | ||
| 113 | |||
| 114 | foreach my $item (@{ setting('_navbar_items') }) { | ||
| 115 | if ($item->{tag} eq $config->{tag}) { | ||
| 116 | $item = $config; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | push @{ setting('_navbar_items') }, $config; | ||
| 122 | }; | ||
| 123 | |||
| 124 | register 'register_admin_task' => sub { | ||
| 125 | my ($self, $config) = plugin_args(@_); | ||
| 126 | |||
| 127 | if (!$config->{tag} | ||
| 128 | or !$config->{label}) { | ||
| 129 | |||
| 130 | debug $config; | ||
| 131 | return error "bad config to register_admin_task"; | ||
| 132 | } | ||
| 133 | |||
| 134 | push @{ setting('_admin_order') }, $config->{tag}; | ||
| 135 | return if $config->{tag} eq 'divider'; | ||
| 136 | setting('_admin_tasks')->{ $config->{tag} } = $config; | ||
| 137 | }; | ||
| 138 | |||
| 139 | sub _register_tab { | ||
| 140 | my ($nav, $config) = @_; | ||
| 141 | my $stash = setting("_${nav}_tabs"); | ||
| 142 | |||
| 143 | if (!$config->{tag} | ||
| 144 | or !$config->{label}) { | ||
| 145 | |||
| 146 | debug $config; | ||
| 147 | return error "bad config to register_${nav}_item"; | ||
| 148 | } | ||
| 149 | |||
| 150 | foreach my $item (@{ $stash }) { | ||
| 151 | if ($item->{tag} eq $config->{tag}) { | ||
| 152 | $item = $config; | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | push @{ $stash }, $config; | ||
| 158 | } | ||
| 159 | |||
| 160 | register 'register_search_tab' => sub { | ||
| 161 | my ($self, $config) = plugin_args(@_); | ||
| 162 | _register_tab('search', $config); | ||
| 163 | |||
| 164 | if ($config->{api_endpoint}) { | ||
| 165 | my $tag = $config->{tag}; | ||
| 166 | swagger_path { | ||
| 167 | tags => ['Search'], | ||
| 168 | path => setting('api_base')."/search/$tag", | ||
| 169 | description => $config->{label} .' Search', | ||
| 170 | parameters => $config->{api_parameters}, | ||
| 171 | responses => | ||
| 172 | ($config->{api_responses} || { default => {} }), | ||
| 173 | }, get "/api/v1/search/$tag" => require_role api => sub { | ||
| 174 | forward "/ajax/content/search/$tag"; | ||
| 175 | }; | ||
| 176 | } | ||
| 177 | }; | ||
| 178 | |||
| 179 | register 'register_device_tab' => sub { | ||
| 180 | my ($self, $config) = plugin_args(@_); | ||
| 181 | $config->{render_if} ||= sub { true }; | ||
| 182 | _register_tab('device', $config); | ||
| 183 | }; | ||
| 184 | |||
| 185 | register 'register_report' => sub { | ||
| 186 | my ($self, $config) = plugin_args(@_); | ||
| 187 | my @categories = @{ setting('_report_order') }; | ||
| 188 | |||
| 189 | if (!$config->{category} | ||
| 190 | or !$config->{tag} | ||
| 191 | or !$config->{label} | ||
| 192 | or 0 == scalar grep {$config->{category} eq $_} @categories) { | ||
| 193 | |||
| 194 | debug $config; | ||
| 195 | return error "bad config to register_report"; | ||
| 196 | } | ||
| 197 | |||
| 198 | if (0 == scalar grep {$_ eq $config->{tag}} | ||
| 199 | @{setting('_reports_menu')->{ $config->{category} }}) { | ||
| 200 | push @{setting('_reports_menu')->{ $config->{category} }}, $config->{tag}; | ||
| 201 | } | ||
| 202 | |||
| 203 | foreach my $tag (@{setting('_reports_menu')->{ $config->{category} }}) { | ||
| 204 | if ($config->{tag} eq $tag) { | ||
| 205 | setting('_reports')->{$tag} = $config; | ||
| 206 | |||
| 207 | if ($config->{api_endpoint}) { | ||
| 208 | (my $category_path = lc $config->{category}) =~ s/ /-/g; | ||
| 209 | my $params_copy = dclone ($config->{api_parameters} || []); # swagger plugin nukes it? | ||
| 210 | |||
| 211 | swagger_path { | ||
| 212 | tags => ['Reports'], | ||
| 213 | path => setting('api_base')."/report/$category_path/$tag", | ||
| 214 | description => $config->{label} .' Report', | ||
| 215 | parameters => | ||
| 216 | ($config->{api_parameters} || | ||
| 217 | ($config->{bind_params} ? [map { $_ => {} } @{ $config->{bind_params} }] : [])), | ||
| 218 | responses => | ||
| 219 | ($config->{api_responses} || { default => {} }), | ||
| 220 | }, | ||
| 221 | |||
| 222 | get "/api/v1/report/$category_path/$tag" => require_role api => sub { | ||
| 223 | # #1360 workaround for swagger missing that False is false | ||
| 224 | foreach my $spec (pairs @{ $params_copy }) { | ||
| 225 | my ($param, $conf) = @$spec; | ||
| 226 | next unless exists $conf->{type} and $conf->{type} eq 'boolean'; | ||
| 227 | next unless exists request->{'_query_params'}->{$param} | ||
| 228 | and defined request->{'_query_params'}->{$param} | ||
| 229 | and ref q{} eq ref request->{'_query_params'}->{$param}; # multiple params are arrayref | ||
| 230 | |||
| 231 | if (request->{'_query_params'}->{$param} eq 'False') { | ||
| 232 | delete request->{'_query_params'}->{$param}; | ||
| 233 | delete params->{$param}; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | forward "/ajax/content/report/$tag"; | ||
| 237 | }; | ||
| 238 | } | ||
| 239 | |||
| 240 | foreach my $rconfig (@{setting('reports')}) { | ||
| 241 | if ($rconfig->{tag} eq $tag) { | ||
| 242 | setting('_reports')->{$tag}->{'rconfig'} = $rconfig; | ||
| 243 | last; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | }; | ||
| 249 | |||
| 250 | register_plugin; | ||
| 251 | true; | ||
| 252 | |||
| 253 | =head1 NAME | ||
| 254 | |||
| 255 | App::Netdisco::Web::Plugin - Netdisco Web UI components | ||
| 256 | |||
| 257 | =head1 Introduction | ||
| 258 | |||
| 259 | L<App::Netdisco>'s plugin system allows you more control of what Netdisco | ||
| 260 | components are displayed in the web interface. Plugins can be distributed | ||
| 261 | independently from Netdisco and are a better alternative to source code | ||
| 262 | patches. | ||
| 263 | |||
| 264 | The following web interface components are implemented as plugins: | ||
| 265 | |||
| 266 | =over 4 | ||
| 267 | |||
| 268 | =item * | ||
| 269 | |||
| 270 | Navigation Bar items (e.g. Inventory link) | ||
| 271 | |||
| 272 | =item * | ||
| 273 | |||
| 274 | Tabs for Search and Device pages | ||
| 275 | |||
| 276 | =item * | ||
| 277 | |||
| 278 | Reports (pre-canned searches) | ||
| 279 | |||
| 280 | =item * | ||
| 281 | |||
| 282 | Additional Device Port Columns | ||
| 283 | |||
| 284 | =item * | ||
| 285 | |||
| 286 | Additional Device Details | ||
| 287 | |||
| 288 | =item * | ||
| 289 | |||
| 290 | Admin Menu function (job control, manual topology, pseudo devices) | ||
| 291 | |||
| 292 | =back | ||
| 293 | |||
| 294 | See L<https://github.com/netdisco/netdisco/wiki/Web-Plugins> for details. | ||
| 295 | |||
| 296 | =cut | ||
| 297 |