| 1 | package App::Netdisco::Web::Plugin::AdminTask::PseudoDevice; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::Ajax; | ||
| 5 | use Dancer::Plugin::DBIC; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use App::Netdisco::Util::DNS 'hostname_from_ip'; | ||
| 9 | use App::Netdisco::Util::Statistics 'pretty_version'; | ||
| 10 | use App::Netdisco::Web::Plugin; | ||
| 11 | use NetAddr::IP::Lite ':lower'; | ||
| 12 | |||
| 13 | register_admin_task({ | ||
| 14 | tag => 'pseudodevice', | ||
| 15 | label => 'Pseudo Devices', | ||
| 16 | }); | ||
| 17 | |||
| 18 | # the count becomes a range which is materialized into a list, so it is bounded | ||
| 19 | # well above any real chassis rather than left to the request | ||
| 20 | sub ports_count_ok { | ||
| 21 | my $count = shift; | ||
| 22 | return 0 unless defined $count and $count =~ m/^[[:digit:]]+$/; | ||
| 23 | return ($count > 0 and $count <= 9999) ? 1 : 0; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _sanity_ok { | ||
| 27 | return 0 unless param('name') | ||
| 28 | and param('name') =~ m/^[[:print:]]+$/ | ||
| 29 | and param('name') !~ m/[[:space:]]/; | ||
| 30 | |||
| 31 | my $ip = NetAddr::IP::Lite->new(param('ip')); | ||
| 32 | return 0 unless ($ip and $ip->addr ne '0.0.0.0'); | ||
| 33 | |||
| 34 | return 0 unless ports_count_ok( param('ports') ); | ||
| 35 | |||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | ajax '/ajax/control/admin/pseudodevice/add' => require_role admin => sub { | ||
| 40 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 41 | |||
| 42 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 43 | my $device = schema(vars->{'tenant'})->resultset('Device') | ||
| 44 | ->create({ | ||
| 45 | ip => param('ip'), | ||
| 46 | dns => (hostname_from_ip(param('ip')) || ''), | ||
| 47 | name => param('name'), | ||
| 48 | vendor => 'netdisco', | ||
| 49 | model => 'pseudodevice', | ||
| 50 | num_ports => param('ports'), | ||
| 51 | os => 'netdisco', | ||
| 52 | os_ver => pretty_version($App::Netdisco::VERSION, 3), | ||
| 53 | layers => param('layers'), | ||
| 54 | last_discover => \'LOCALTIMESTAMP', | ||
| 55 | is_pseudo => \'true', | ||
| 56 | }); | ||
| 57 | return unless $device; | ||
| 58 | |||
| 59 | $device->ports->populate([ | ||
| 60 | [qw/port type descr/], | ||
| 61 | map {["Port$_", 'other', "Port$_"]} @{[1 .. param('ports')]}, | ||
| 62 | ]); | ||
| 63 | |||
| 64 | # device_ip table is used to show whether topo is "broken" | ||
| 65 | schema(vars->{'tenant'})->resultset('DeviceIp') | ||
| 66 | ->create({ | ||
| 67 | ip => param('ip'), | ||
| 68 | alias => param('ip'), | ||
| 69 | vrf => '', | ||
| 70 | }); | ||
| 71 | }); | ||
| 72 | |||
| 73 | return ''; | ||
| 74 | }; | ||
| 75 | |||
| 76 | ajax '/ajax/control/admin/pseudodevice/update' => require_role admin => sub { | ||
| 77 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 78 | |||
| 79 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 80 | my $device = schema(vars->{'tenant'})->resultset('Device') | ||
| 81 | ->with_port_count->find({ip => param('ip')}); | ||
| 82 | return unless $device; | ||
| 83 | my $count = $device->port_count; | ||
| 84 | |||
| 85 | if (param('ports') > $count) { | ||
| 86 | my $start = $count + 1; | ||
| 87 | $device->ports->populate([ | ||
| 88 | [qw/port type descr/], | ||
| 89 | map {["Port$_", 'other', "Port$_"]} @{[$start .. param('ports')]}, | ||
| 90 | ]); | ||
| 91 | } | ||
| 92 | elsif (param('ports') < $count) { | ||
| 93 | my $start = param('ports') + 1; | ||
| 94 | |||
| 95 | foreach my $port ($start .. $count) { | ||
| 96 | $device->ports | ||
| 97 | ->single({port => "Port${port}"})->delete; | ||
| 98 | |||
| 99 | # clear outdated manual topology links | ||
| 100 | schema(vars->{'tenant'})->resultset('Topology')->search({ | ||
| 101 | -or => [ | ||
| 102 | { dev1 => $device->ip, port1 => "Port${port}" }, | ||
| 103 | { dev2 => $device->ip, port2 => "Port${port}" }, | ||
| 104 | ], | ||
| 105 | })->delete; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | $device->update({num_ports => param('ports')}); | ||
| 109 | |||
| 110 | # also set layers | ||
| 111 | $device->update({layers => param('layers')}); | ||
| 112 | |||
| 113 | # and update last_discover, since device properties changed | ||
| 114 | $device->update({last_discover => \'LOCALTIMESTAMP'}); | ||
| 115 | }); | ||
| 116 | |||
| 117 | return ''; | ||
| 118 | }; | ||
| 119 | |||
| 120 | ajax '/ajax/content/admin/pseudodevice' => require_role admin => sub { | ||
| 121 | my $set = schema(vars->{'tenant'})->resultset('Device') | ||
| 122 | ->search( | ||
| 123 | {-bool => 'is_pseudo'}, | ||
| 124 | {order_by => { -desc => 'last_discover' }}, | ||
| 125 | )->with_port_count; | ||
| 126 | |||
| 127 | content_type('text/html'); | ||
| 128 | template 'ajax/admintask/pseudodevice.tt', { | ||
| 129 | results => $set, | ||
| 130 | }, { layout => undef }; | ||
| 131 | }; | ||
| 132 | |||
| 133 | true; |