Support for connecting to external databases for custom Reports/Plugins

This commit is contained in:
Oliver Gorwits
2015-05-06 23:37:55 +01:00
parent e7d86a5611
commit 9f9aba68a2
9 changed files with 88 additions and 2 deletions

View File

@@ -1,5 +1,9 @@
2.032004 -
[NEW FEATURES]
* Support for connecting to external databases for custom Reports/Plugins
[ENHANCEMENTS]
* Allow "hidden" option to reports config

View File

@@ -143,6 +143,8 @@ lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-7-8-PostgreSQL.sql
lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-8-9-PostgreSQL.sql
lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-9-10-PostgreSQL.sql
lib/App/Netdisco/Environment.pm
lib/App/Netdisco/GenericDB.pm
lib/App/Netdisco/GenericDB/Result/Virtual/GenericReport.pm
lib/App/Netdisco/JobQueue.pm
lib/App/Netdisco/JobQueue/PostgreSQL.pm
lib/App/Netdisco/Manual/BSDInstall.pod

View File

@@ -33,6 +33,13 @@ if (ref {} eq ref setting('database')) {
schema_class => 'App::Netdisco::DB',
};
foreach my $c (@{setting('external_databases')}) {
my $schema = delete $c->{tag} or next;
next if $schema eq 'netdisco';
setting('plugins')->{DBIC}->{$schema} = $c;
setting('plugins')->{DBIC}->{$schema}->{schema_class}
||= 'App::Netdisco::GenericDB';
}
}
# defaults for workers

View File

@@ -0,0 +1,10 @@
use utf8;
package App::Netdisco::GenericDB;
use strict;
use warnings;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces();
1;

View File

@@ -0,0 +1,13 @@
package App::Netdisco::GenericDB::Result::Virtual::GenericReport;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
__PACKAGE__->table("generic_report");
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(q{});
1;

View File

@@ -80,6 +80,27 @@ Additional library paths for the application (both web frontend and backend
poller daemons). You can also use a colon-separated list in the
"C<NETDISCO_INC>" environment variable.
=head3 C<external_databases>
Value: List of Database Configuration Hashes. Default: None.
The Plugins and User Reports features of Netdisco can gather data from
external databases. You need to install the Perl database driver for the
target database platform, and configure this setting appropriately. For
example:
external_databases:
- tag: externaldb
dsn: 'dbi:Pg:dbname=myexternaldb;host=192.0.2.1'
user: oliver
password: letmein
options:
pg_enable_utf8: true
Note that C<tag> is I<required> and maps to the C<database> key if you use the
Generic Reports feature (see "L</reports>", below), or becomes the Dancer
database schema name if you program the Plugin directly.
=head2 Web Frontend
=head3 C<domain_suffix>

View File

@@ -330,6 +330,32 @@ C<port_control>:
Take care over the subtle differences in syntax, especially the placement of
the fat comma ("C<< => >>").
=head1 Database Connections
The Netdisco database is available via the C<netdisco> schema key, as below.
You can also use the C<external_databases> configuration item to set up
connections to other databases.
# possibly install another database driver
~netdisco/bin/localenv cpanm --notest DBD::mysql
# deployment.yml
external_databases:
- tag: externaldb
dsn: 'dbi:mysql:dbname=myexternaldb;host=192.0.2.1'
user: oliver
password: letmein
# plugin code
use Dancer::Plugin::DBIC;
schema('netdisco')->resultset('Devices')->search({vendor => 'cisco'});
schema('externaldb')->resultset('MyTable')->search({field => 'foobar'});
You'll need to install a L<DBIx::Class> Schema and also set C<schema_class> to
its name within the C<external_databases> setting, for the second example
above.
=head1 Templates
All of Netdisco's web page templates are stashed away in its distribution,

View File

@@ -26,7 +26,9 @@ foreach my $report (@{setting('reports')}) {
# TODO: this should be done by creating a new Virtual Result class on
# the fly (package...) and then calling DBIC register_class on it.
my $rs = schema('netdisco')->resultset('Virtual::GenericReport')->result_source;
my $schema = ($report->{database} || 'netdisco');
my $rs = schema($schema)->resultset('Virtual::GenericReport')->result_source;
$rs->view_definition($report->{query});
$rs->remove_columns($rs->columns);
$rs->add_columns( exists $report->{query_columns}
@@ -34,7 +36,7 @@ foreach my $report (@{setting('reports')}) {
: (map {keys %{$_}} @{$report->{columns}})
);
my $set = schema('netdisco')->resultset('Virtual::GenericReport')
my $set = schema($schema)->resultset('Virtual::GenericReport')
->search(undef, {
result_class => 'DBIx::Class::ResultClass::HashRefInflator',
( (exists $report->{bind_params})

View File

@@ -12,6 +12,7 @@
log: 'warning'
logger_format: '[%P] %U %L %m'
include_paths: []
external_databases: []
# ------------
# WEB FRONTEND