Support for connecting to external databases for custom Reports/Plugins
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								Netdisco/lib/App/Netdisco/GenericDB.pm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Netdisco/lib/App/Netdisco/GenericDB.pm
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
use utf8;
 | 
			
		||||
package App::Netdisco::GenericDB;
 | 
			
		||||
 | 
			
		||||
use strict;
 | 
			
		||||
use warnings;
 | 
			
		||||
 | 
			
		||||
use base 'DBIx::Class::Schema';
 | 
			
		||||
__PACKAGE__->load_namespaces();
 | 
			
		||||
 | 
			
		||||
1;
 | 
			
		||||
@@ -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;
 | 
			
		||||
@@ -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>
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user