From eeb5705fbc318626eee74d1ead0a2b2f48a98fb8 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 4 Mar 2014 21:09:54 +0000 Subject: [PATCH] support netdisco-rancid-export GROUP config --- Web-Plugin-RANCID/Changes | 5 ++ .../lib/App/NetdiscoX/Web/Plugin/RANCID.pm | 70 +++++++++++++++---- .../views/plugin/rancid/device_details.tt | 7 +- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/Web-Plugin-RANCID/Changes b/Web-Plugin-RANCID/Changes index e8f9ea47..6bc275f0 100644 --- a/Web-Plugin-RANCID/Changes +++ b/Web-Plugin-RANCID/Changes @@ -1,3 +1,8 @@ +2.002000 - 2014-02-04 + + * Support for netdisco-rancid-export config for GROUP name + * Change to use location and open_in_same_window + 2.001000 - 2013-12-08 * Initial release diff --git a/Web-Plugin-RANCID/lib/App/NetdiscoX/Web/Plugin/RANCID.pm b/Web-Plugin-RANCID/lib/App/NetdiscoX/Web/Plugin/RANCID.pm index bbd5285e..7afd11e0 100644 --- a/Web-Plugin-RANCID/lib/App/NetdiscoX/Web/Plugin/RANCID.pm +++ b/Web-Plugin-RANCID/lib/App/NetdiscoX/Web/Plugin/RANCID.pm @@ -1,9 +1,11 @@ package App::NetdiscoX::Web::Plugin::RANCID; -our $VERSION = '2.001001'; +our $VERSION = '2.002000'; use Dancer ':syntax'; + use App::Netdisco::Web::Plugin; +use App::Netdisco::Util::Permission 'check_acl'; use File::ShareDir 'dist_dir'; register_template_path( @@ -15,6 +17,34 @@ register_device_details({ default => 'on', }); +hook 'before_template' => sub { + return unless + index(request->path, uri_for('/ajax/content/device/details')->path) == 0; + + my $config = config; + my $tokens = shift; + my $device = $tokens->{d}; + + # defaults + $tokens->{rancidgroup} = ''; + $tokens->{ranciddevice} = ($device->{dns} || $device->{name} || $device->{ip}); + + return unless exists $config->{rancid}; + my $rancid = $config->{rancid}; + + $rancid->{groups} ||= {}; + $rancid->{by_ip} ||= []; + + foreach my $g (keys %{ $rancid->{groups} }) { + if (check_acl( $d, $rancid->{groups}->{$g} )) { + $tokens->{rancidgroup} = $g; + $tokens->{ranciddevice} = $device->{ip} + if 0 < scalar grep {$_ eq $g} @{ $rancid->{by_ip} }; + last; + } + } +}; + =head1 NAME App::NetdiscoX::Web::Plugin::RANCID - Link to device backups in RANCID/WebSVN @@ -27,8 +57,8 @@ App::NetdiscoX::Web::Plugin::RANCID - Link to device backups in RANCID/WebSVN - X::RANCID plugin_rancid: - location: 'websvn-server.example.com/rancid' - open_in_new_window: true + location: 'https://websvn-server.example.com/rancid/%GROUP%/' + open_in_same_window: false =head1 Description @@ -49,18 +79,34 @@ Value: String, Required. Name of the server hosting your local WebSVN installation. This should also include the path under which backup files are stored for the devices. -=head2 open_in_new_window +B + +The text "C<%GROUP%>" will be replaced with the group name for this device, if +known to Netdisco. This uses the same configuration as for +L, an example of which is below: + +rancid: + by_ip: [ other ] + groups: + switch: [ 'name:.*[Ss][Ww].*' ] + rtr: [ 'name:[rR]tr.*' ] + ap: [ 'name:[aA][pP].*' ] + +Briefly, each group value is a list of rules for matching devices similar to +those used by any C<*_only> configuration item. You can provide an IP, subnet +or prefix, regular expression to match a device name, or device attribute and +regular expression as in the above example. + +The device DNS name is used, or if missing the device SNMP sysName. Adding the +group to the list in C will make the link include the device IP +instead. + +=head2 open_in_same_window Value: Boolean. Default: false. -If set to true, the hyperlink is configured to open the WebSVN page in a new -browser window or tab. - -=head1 TODO - -At the moment the device name is used in the link. This is kind of useless -for most installations. Needs config to control how to specify device in -generated link. +If set to true, the hyperlink is configured to open the WebSVN page in the +same browser window or tab as Netdisco. =head1 AUTHOR diff --git a/Web-Plugin-RANCID/share/views/plugin/rancid/device_details.tt b/Web-Plugin-RANCID/share/views/plugin/rancid/device_details.tt index 2e137fa7..32f498e5 100644 --- a/Web-Plugin-RANCID/share/views/plugin/rancid/device_details.tt +++ b/Web-Plugin-RANCID/share/views/plugin/rancid/device_details.tt @@ -1,4 +1,3 @@ -View Configuration +View Configuration