| 1 | package App::Netdisco::Web::Plugin::Report::L3Domains; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Web::Plugin; | ||
| 8 | |||
| 9 | register_report( | ||
| 10 | { category => 'IP', | ||
| 11 | tag => 'l3domains', | ||
| 12 | label => 'L3 Domains', | ||
| 13 | provides_csv => 1, | ||
| 14 | api_endpoint => 1, | ||
| 15 | api_parameters => [ | ||
| 16 | domain => { | ||
| 17 | description => 'Comma-separated L3 domain ids whose member devices and VRFs to list', | ||
| 18 | }, | ||
| 19 | ], | ||
| 20 | } | ||
| 21 | ); | ||
| 22 | |||
| 23 | # names repeat across domains, so a domain is chosen by id; 0 is the default | ||
| 24 | # table and has no members | ||
| 25 | sub domain_ids { | ||
| 26 | my $param = shift; | ||
| 27 | my @given = map { split /,/ } grep { defined } ( ref [] eq ref $param ? @$param : ($param) ); | ||
| 28 | my %seen; | ||
| 29 | return [ grep { not $seen{$_}++ } | ||
| 30 | map { 0 + $_ } | ||
| 31 | grep { /^\s*[1-9][0-9]{0,8}\s*$/ } @given ]; | ||
| 32 | } | ||
| 33 | |||
| 34 | # the reportLink renderer sends "blank" for a row with no domain, so a "not | ||
| 35 | # grouped" link asks for the VRFs that have no member row | ||
| 36 | sub wants_ungrouped { | ||
| 37 | my $param = shift; | ||
| 38 | my @given = map { split /,/ } grep { defined } ( ref [] eq ref $param ? @$param : ($param) ); | ||
| 39 | return scalar grep { /^\s*blank\s*$/ } @given; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub domain_lookup { | ||
| 43 | return { map { ($_ => 1) } @{ domain_ids(shift) } }; | ||
| 44 | } | ||
| 45 | |||
| 46 | # the page loads its content by submitting the sidebar form, so ?domain= has to | ||
| 47 | # arrive as selected options of the form's select | ||
| 48 | hook 'before_template' => sub { | ||
| 49 | my $tokens = shift; | ||
| 50 | my $page = uri_for('/report/l3domains')->path; | ||
| 51 | my $content = uri_for('/ajax/content/report/l3domains')->path; | ||
| 52 | return unless request->path eq $page or index(request->path, $content) == 0; | ||
| 53 | |||
| 54 | $tokens->{l3domain_lkp} = domain_lookup( param('domain') ); | ||
| 55 | $tokens->{l3domain_list} = [ _summary() ] if request->path eq $page; | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub _summary { | ||
| 59 | return schema(vars->{'tenant'})->resultset('L3Domain')->search( | ||
| 60 | { 'me.id' => { '!=' => 0 } }, | ||
| 61 | { join => 'members', | ||
| 62 | select => [ | ||
| 63 | 'me.id', | ||
| 64 | 'me.name', | ||
| 65 | \q{CASE WHEN me.inferred_name LIKE 'manual:%' THEN 'set by vrf_domains' ELSE min(members.reason) END}, | ||
| 66 | \q{array_agg(DISTINCT members.vrf ORDER BY members.vrf)}, | ||
| 67 | { count => { distinct => 'members.ip' } }, | ||
| 68 | ], | ||
| 69 | as => [qw/ id name evidence vrfs devices /], | ||
| 70 | group_by => [qw/ me.id me.name me.inferred_name /], | ||
| 71 | having => \q{count(members.ip) > 0}, | ||
| 72 | order_by => [qw/ me.name me.id /], | ||
| 73 | } | ||
| 74 | )->hri->all; | ||
| 75 | } | ||
| 76 | |||
| 77 | # every named VRF, placed in a domain or not, so the report can show why a VRF | ||
| 78 | # did not join the domain an operator expected | ||
| 79 | sub vrf_search { | ||
| 80 | my ($ids, $ungrouped) = @_; | ||
| 81 | my $where = { 'me.vrf' => { '!=' => '' } }; | ||
| 82 | if ($ungrouped and scalar @$ids) { | ||
| 83 | $where->{-or} = [ { 'l3_member.domain_id' => { -in => $ids } }, { 'l3_member.domain_id' => undef } ]; | ||
| 84 | } | ||
| 85 | elsif ($ungrouped) { | ||
| 86 | $where->{'l3_member.domain_id'} = undef; | ||
| 87 | } | ||
| 88 | elsif (scalar @$ids) { | ||
| 89 | $where->{'l3_member.domain_id'} = { -in => $ids }; | ||
| 90 | } | ||
| 91 | return ( $where, { | ||
| 92 | join => [ 'device', { l3_member => 'domain' } ], | ||
| 93 | select => [ | ||
| 94 | \q{host(me.ip)}, 'device.dns', 'device.name', 'me.vrf', 'l3_member.domain_id', | ||
| 95 | \q{CASE WHEN domain.id IS NULL THEN 'not grouped' ELSE domain.name || ' (#' || domain.id || ')' END}, | ||
| 96 | \q{CASE WHEN l3_member.source = 'manual' THEN 'set by vrf_domains' ELSE l3_member.reason END}, | ||
| 97 | 'me.rd', 'me.rt_import', 'me.rt_export', 'me.l3_isid', | ||
| 98 | ], | ||
| 99 | as => [qw/ ip dns device_name vrf domain_id domain grouped_by rd rt_import rt_export l3_isid /], | ||
| 100 | order_by => [qw/ domain.name domain.id me.ip me.vrf /], | ||
| 101 | } ); | ||
| 102 | } | ||
| 103 | |||
| 104 | get '/ajax/content/report/l3domains' => require_login sub { | ||
| 105 | my @results = schema(vars->{'tenant'})->resultset('DeviceVrf') | ||
| 106 | ->search( vrf_search( domain_ids( param('domain') ), wants_ungrouped( param('domain') ) ) )->hri->all; | ||
| 107 | |||
| 108 | return unless scalar @results; | ||
| 109 | |||
| 110 | if ( request->is_ajax ) { | ||
| 111 | template 'ajax/report/l3domains.tt', { results => to_json(\@results) }, { layout => 'noop' }; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 115 | template 'ajax/report/l3domains_csv.tt', { results => \@results }, { layout => 'noop' }; | ||
| 116 | } | ||
| 117 | }; | ||
| 118 | |||
| 119 | true; |