refactor CustomFields so with_custom_fields is available on backend

This commit is contained in:
Oliver Gorwits
2023-07-20 16:50:22 +01:00
parent 1eabe7064f
commit 47c4226788
5 changed files with 67 additions and 61 deletions

View File

@@ -0,0 +1,59 @@
package App::Netdisco::Util::CustomFields;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use App::Netdisco::DB::ResultSet::Device;
use App::Netdisco::DB::ResultSet::DevicePort;
my @inline_device_actions = ();
my @inline_device_port_actions = ();
foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) {
next unless $config->{'name'};
push @inline_device_actions, $config->{'name'};
}
foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) {
next unless $config->{'name'};
push @inline_device_port_actions, $config->{'name'};
}
{
package App::Netdisco::DB::ResultSet::Device;
sub with_custom_fields {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{ '+columns' => {
map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )}
@inline_device_actions
}});
}
}
{
package App::Netdisco::DB::ResultSet::DevicePort;
sub with_custom_fields {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{ '+columns' => {
map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )}
@inline_device_port_actions
}});
}
}
set('_inline_actions' => [
map {'cf_' . $_} (@inline_device_actions, @inline_device_port_actions)
]);
true;

View File

@@ -6,6 +6,7 @@ use Dancer qw/:syntax :script/;
use Scalar::Util qw/blessed reftype/; use Scalar::Util qw/blessed reftype/;
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use App::Netdisco::Util::DNS 'hostname_from_ip'; use App::Netdisco::Util::DNS 'hostname_from_ip';
use base 'Exporter'; use base 'Exporter';

View File

@@ -1,86 +1,30 @@
package App::Netdisco::Web::CustomFields; package App::Netdisco::Web::CustomFields;
use Dancer ':syntax'; use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use App::Netdisco::DB::ResultSet::Device;
use App::Netdisco::DB::ResultSet::DevicePort;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::CustomFields;
my @inline_device_actions = ();
my @inline_device_port_actions = ();
foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) { foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) {
next unless $config->{'name'};
if (! $config->{'name'}) {
error 'custom_field missing name';
next;
}
register_device_details({ register_device_details({
%{ $config }, %{ $config },
field => ('cf_' . $config->{'name'}), field => ('cf_' . $config->{'name'}),
label => ($config->{'label'} || ucfirst $config->{'name'}), label => ($config->{'label'} || ucfirst $config->{'name'}),
}) unless $config->{'hidden'}; }) unless $config->{'hidden'};
push @inline_device_actions, $config->{'name'};
} }
foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) { foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) {
next unless $config->{'name'};
if (! $config->{'name'}) {
error 'custom_field missing name';
next;
}
register_device_port_column({ register_device_port_column({
position => 'right', # or "mid" or "right" position => 'right', # or "mid" or "right"
default => undef, # or undef default => undef, # or "checked"
%{ $config }, %{ $config },
field => ('cf_' . $config->{'name'}), field => ('cf_' . $config->{'name'}),
label => ($config->{'label'} || ucfirst $config->{'name'}), label => ($config->{'label'} || ucfirst $config->{'name'}),
}) unless $config->{'hidden'}; }) unless $config->{'hidden'};
push @inline_device_port_actions, $config->{'name'};
} }
{
package App::Netdisco::DB::ResultSet::Device;
sub with_custom_fields {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{ '+columns' => {
map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )}
@inline_device_actions
}});
}
}
{
package App::Netdisco::DB::ResultSet::DevicePort;
sub with_custom_fields {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{ '+columns' => {
map {( ('cf_'. $_) => \[ 'me.custom_fields ->> ?' => $_ ] )}
@inline_device_port_actions
}});
}
}
set('_inline_actions' => [
map {'cf_' . $_} (@inline_device_actions, @inline_device_port_actions)
]);
true; true;

View File

@@ -2,6 +2,8 @@ package App::Netdisco::Worker::Runner;
use Dancer qw/:moose :syntax/; use Dancer qw/:moose :syntax/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::CustomFields;
use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/; use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/;
use aliased 'App::Netdisco::Worker::Status'; use aliased 'App::Netdisco::Worker::Status';

View File

@@ -503,6 +503,7 @@ job_prio:
- stats - stats
worker_plugins: worker_plugins:
- 'Internal::SNMPFastDiscover'
- 'AddPseudoDevice' - 'AddPseudoDevice'
- 'Arpnip' - 'Arpnip'
- 'Arpnip::Hooks' - 'Arpnip::Hooks'
@@ -536,7 +537,6 @@ worker_plugins:
- 'Hook' - 'Hook'
- 'Hook::Exec' - 'Hook::Exec'
- 'Hook::HTTP' - 'Hook::HTTP'
- 'Internal::SNMPFastDiscover'
- 'LoadMIBs' - 'LoadMIBs'
- 'Location' - 'Location'
- 'Macsuck' - 'Macsuck'