From 47c42267889bb7c0445d9f1edb52b6f17933a4f7 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 20 Jul 2023 16:50:22 +0100 Subject: [PATCH] refactor CustomFields so with_custom_fields is available on backend --- lib/App/Netdisco/Util/CustomFields.pm | 59 ++++++++++++++++++++++++ lib/App/Netdisco/Util/Permission.pm | 1 + lib/App/Netdisco/Web/CustomFields.pm | 64 ++------------------------- lib/App/Netdisco/Worker/Runner.pm | 2 + share/config.yml | 2 +- 5 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 lib/App/Netdisco/Util/CustomFields.pm diff --git a/lib/App/Netdisco/Util/CustomFields.pm b/lib/App/Netdisco/Util/CustomFields.pm new file mode 100644 index 00000000..67ed6835 --- /dev/null +++ b/lib/App/Netdisco/Util/CustomFields.pm @@ -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; + diff --git a/lib/App/Netdisco/Util/Permission.pm b/lib/App/Netdisco/Util/Permission.pm index 009fd0b0..e279d6dc 100644 --- a/lib/App/Netdisco/Util/Permission.pm +++ b/lib/App/Netdisco/Util/Permission.pm @@ -6,6 +6,7 @@ use Dancer qw/:syntax :script/; use Scalar::Util qw/blessed reftype/; use NetAddr::IP::Lite ':lower'; + use App::Netdisco::Util::DNS 'hostname_from_ip'; use base 'Exporter'; diff --git a/lib/App/Netdisco/Web/CustomFields.pm b/lib/App/Netdisco/Web/CustomFields.pm index 05dfc5b4..b469cde4 100644 --- a/lib/App/Netdisco/Web/CustomFields.pm +++ b/lib/App/Netdisco/Web/CustomFields.pm @@ -1,86 +1,30 @@ package App::Netdisco::Web::CustomFields; use Dancer ':syntax'; -use Dancer::Plugin::DBIC; - -use App::Netdisco::DB::ResultSet::Device; -use App::Netdisco::DB::ResultSet::DevicePort; use App::Netdisco::Web::Plugin; - -my @inline_device_actions = (); -my @inline_device_port_actions = (); +use App::Netdisco::Util::CustomFields; foreach my $config (@{ setting('custom_fields')->{'device'} || [] }) { - - if (! $config->{'name'}) { - error 'custom_field missing name'; - next; - } + next unless $config->{'name'}; register_device_details({ %{ $config }, field => ('cf_' . $config->{'name'}), label => ($config->{'label'} || ucfirst $config->{'name'}), }) unless $config->{'hidden'}; - - push @inline_device_actions, $config->{'name'}; - } foreach my $config (@{ setting('custom_fields')->{'device_port'} || [] }) { - - if (! $config->{'name'}) { - error 'custom_field missing name'; - next; - } + next unless $config->{'name'}; register_device_port_column({ position => 'right', # or "mid" or "right" - default => undef, # or undef + default => undef, # or "checked" %{ $config }, field => ('cf_' . $config->{'name'}), label => ($config->{'label'} || ucfirst $config->{'name'}), }) 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; diff --git a/lib/App/Netdisco/Worker/Runner.pm b/lib/App/Netdisco/Worker/Runner.pm index e4edf6ce..f0cbeeb8 100644 --- a/lib/App/Netdisco/Worker/Runner.pm +++ b/lib/App/Netdisco/Worker/Runner.pm @@ -2,6 +2,8 @@ package App::Netdisco::Worker::Runner; use Dancer qw/:moose :syntax/; use Dancer::Plugin::DBIC 'schema'; + +use App::Netdisco::Util::CustomFields; use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/; use aliased 'App::Netdisco::Worker::Status'; diff --git a/share/config.yml b/share/config.yml index bd87cef4..8e226999 100644 --- a/share/config.yml +++ b/share/config.yml @@ -503,6 +503,7 @@ job_prio: - stats worker_plugins: + - 'Internal::SNMPFastDiscover' - 'AddPseudoDevice' - 'Arpnip' - 'Arpnip::Hooks' @@ -536,7 +537,6 @@ worker_plugins: - 'Hook' - 'Hook::Exec' - 'Hook::HTTP' - - 'Internal::SNMPFastDiscover' - 'LoadMIBs' - 'Location' - 'Macsuck'