App-Netdisco

 view release on metacpan or  search on metacpan

lib/App/Netdisco/DB/Result/Device.pm  view on Meta::CPAN

Will update this device and all related database records to use the new IP
C<$new_ip>. Returns C<undef> if $new_ip seems invalid, otherwise returns the
Device row object.

=cut

sub renumber {
  my ($device, $ip) = @_;
  my $schema = $device->result_source->schema;

  my $new_addr = NetAddr::IP::Lite->new($ip)
    or return;

  my $old_ip = $device->ip;
  my $new_ip = $new_addr->addr;

  return if $new_ip eq '0.0.0.0';

  # the special record in device_ip which is always there
  $schema->resultset('DeviceIp')
    ->search({ip => $old_ip, alias => $old_ip})
    ->update({ip => $new_ip, alias => $new_ip});
  $schema->resultset('DeviceIp')
    ->search({ip => $old_ip, alias => $new_ip})->delete();

  # Community is not included as SNMP::test_connection will take care of it
  foreach my $set (qw/
    DeviceBrowser
    DeviceIp
    DeviceModule
    DevicePower
    DeviceVlan
    DevicePort
    DevicePortLog
    DevicePortPower
    DevicePortProperties
    DevicePortSsid
    DevicePortVlan
    DevicePortWireless
  /) {
    $schema->resultset($set)
      ->search({ip => $old_ip})
      ->update({ip => $new_ip});
  }

  $schema->resultset('DeviceSkip')
    ->search({device => $new_ip})->delete;
  $schema->resultset('DeviceSkip')
    ->search({device => $old_ip})
    ->update({device => $new_ip});

  $schema->resultset('DevicePort')
    ->search({remote_ip => $old_ip})
    ->update({remote_ip => $new_ip});

  $schema->resultset('Node')
    ->search({switch => $old_ip})
    ->update({switch => $new_ip});

  # this whole shenanigans exists because I cannot work out how to
  # pass an escaped SQL placeholder into DBIx::Class/SQL::Abstract
  # see https://www.mail-archive.com/dbix-class@lists.scsys.co.uk/msg07079.html
  $schema->storage->dbh_do(sub {
    my ($storage, $dbh, @extra) = @_;
    local $dbh->{TraceLevel} = ($ENV{DBIC_TRACE} ? '1|SQL' : $dbh->{TraceLevel});

    my $router_first_sql = q{
      UPDATE node_ip
        SET seen_on_router_first = seen_on_router_first - ?::text || jsonb_build_object(?::text, seen_on_router_first -> ?::text)
        WHERE seen_on_router_first \? ?::text
    };
    $dbh->do($router_first_sql, undef, $old_ip, $new_ip, $old_ip, $old_ip);

    my $router_last_sql = q{
      UPDATE node_ip
        SET seen_on_router_last = seen_on_router_last - ?::text || jsonb_build_object(?::text, seen_on_router_last -> ?::text)
        WHERE seen_on_router_last \? ?::text
    };
    $dbh->do($router_last_sql, undef, $old_ip, $new_ip, $old_ip, $old_ip);
  });

  $schema->resultset('Topology')
    ->search({dev1 => $old_ip})
    ->update({dev1 => $new_ip});

  $schema->resultset('Topology')
    ->search({dev2 => $old_ip})
    ->update({dev2 => $new_ip});

  $schema->resultset('Admin')->search({
    device => $old_ip,
  })->delete;

  $device->update({
    ip  => $new_ip,
    dns => (hostname_from_ip($new_ip)
      || eval { $schema->resultset('DeviceIp')->find($new_ip,$new_ip)->dns } || undef),
  });

  return $device;
}

=head1 ADDITIONAL COLUMNS

=head2 port_count

Returns the number of ports on this device. Enable this
column by applying the C<with_port_count()> modifier to C<search()>.

=cut

sub port_count { return (shift)->get_column('port_count') }

=head2 is_discoverable

Returns the number of backends able to discover the device. Enable this
column by applying the C<with_layer_features()> modifier to C<search()>.

=cut

sub is_discoverable { return (shift)->get_column('is_discoverable') }



( run in 1.020 second using v1.01-cache-2.11-cpan-39bf76dae61 )