App-Netdisco

 view release on metacpan or  search on metacpan

lib/App/Netdisco/Web/Plugin/Report/L3Domains.pm  view on Meta::CPAN

package App::Netdisco::Web::Plugin::Report::L3Domains;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;

use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::L3Domain qw/vrf_attrs l3_domain_label l3_domain_labels l3_domain_label_attrs
  attach_domain_labels/;

register_report(
    {   category     => 'IP',
        tag          => 'l3domains',
        label        => 'L3 Domains',
        provides_csv => 1,
        api_endpoint => 1,
        api_parameters => [
          domain => {
            description => 'Comma-separated L3 domain ids whose member devices and VRFs to list',
          },
        ],
    }
);

# names repeat across domains, so a domain is chosen by id; 0 is the default
# table and has no members
sub domain_ids {
    my $param = shift;
    my @given = map { split /,/ } grep { defined } ( ref [] eq ref $param ? @$param : ($param) );
    my %seen;
    return [ grep { not $seen{$_}++ }
             map  { 0 + $_ }
             grep { /^\s*[1-9][0-9]{0,8}\s*$/ } @given ];
}

# the reportLink renderer sends "blank" for a row with no domain, so a "not
# grouped" link asks for the VRFs that have no member row
sub wants_ungrouped {
    my $param = shift;
    my @given = map { split /,/ } grep { defined } ( ref [] eq ref $param ? @$param : ($param) );
    return scalar grep { /^\s*blank\s*$/ } @given;
}

sub domain_lookup {
    return { map { ($_ => 1) } @{ domain_ids(shift) } };
}

# the page loads its content by submitting the sidebar form, so ?domain= has to
# arrive as selected options of the form's select
hook 'before_template' => sub {
    my $tokens = shift;
    my $page = uri_for('/report/l3domains')->path;
    my $content = uri_for('/ajax/content/report/l3domains')->path;
    return unless request->path eq $page or index(request->path, $content) == 0;

    $tokens->{l3domain_lkp} = domain_lookup( param('domain') );
    $tokens->{l3domain_ungrouped} = wants_ungrouped( param('domain') ) ? 1 : 0;
    $tokens->{l3domain_list} = [ with_missing_domains( domain_ids( param('domain') ), _summary() ) ]
      if request->path eq $page;
};

# a link can outlive its domain (a regroup deletes emptied ones); without an
# option for the id the form would send no domain and show the whole report
sub with_missing_domains {
    my ($ids, @listed) = @_;
    my %listed = map { ($_->{id} => 1) } @listed;
    return ( ( map { { id => $_, name => '', label => "L3 domain $_ (does not exist)" } }
               grep { not $listed{$_} } @$ids ), @listed );
}

sub _summary {
    return summary_rows( schema(vars->{'tenant'})->resultset('L3Domain')->search(
        { 'me.id' => { '!=' => 0 } },
        {   %{ l3_domain_label_attrs() },
            having   => \q{count(members.ip) > 0},
            order_by => [qw/ me.name me.id /],



( run in 0.911 second using v1.01-cache-2.11-cpan-eb134014566 )