App-Netdisco

 view release on metacpan or  search on metacpan

lib/App/Netdisco/Web/API/Objects.pm  view on Meta::CPAN

package App::Netdisco::Web::API::Objects;

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

use App::Netdisco::JobQueue 'jq_insert';
use Try::Tiny;

swagger_path {
  tags => ['Objects'],
  path => (setting('api_base') || '').'/object/device/{ip}',
  description => 'Returns a row from the device table',
  parameters  => [
    ip => {
      description => 'Canonical IP of the Device. Use Search methods to find this.',
      required => 1,
      in => 'path',
    },
  ],
  responses => { default => {} },
}, get '/api/v1/object/device/:ip' => require_role api => sub {
  my $device = try { schema(vars->{'tenant'})->resultset('Device')
    ->find( params->{ip} ) } or send_error('Bad Device', 404);

  my $data = $device->TO_JSON;

  my @modules = try {
    schema(vars->{'tenant'})->resultset('DevicePower')
      ->search({ 'me.ip' => $device->ip })->with_poestats->hri->all;
  } catch { () };

  if (@modules) {
    my %totals = (
      modules             => scalar @modules,
      power_total         => 0,
      poe_capable_ports   => 0,
      poe_powered_ports   => 0,
      poe_disabled_ports  => 0,
      poe_errored_ports   => 0,
      poe_power_committed => 0,
      poe_power_delivering => 0,
    );
    for my $m (@modules) {
      $totals{power_total}          += $m->{power}               // 0;
      $totals{poe_capable_ports}    += $m->{poe_capable_ports}   // 0;
      $totals{poe_powered_ports}    += $m->{poe_powered_ports}   // 0;
      $totals{poe_disabled_ports}   += $m->{poe_disabled_ports}  // 0;
      $totals{poe_errored_ports}    += $m->{poe_errored_ports}   // 0;
      $totals{poe_power_committed}  += $m->{poe_power_committed} // 0;
      $totals{poe_power_delivering} += $m->{poe_power_delivering} // 0;
    }
    $data->{poe} = \%totals;
  } else {
    $data->{poe} = undef;
  }

  return to_json $data;
};

foreach my $rel (qw/device_ips vlans ports modules port_vlans wireless_ports ssids powered_ports/) {
    swagger_path {
      tags => ['Objects'],
      path => (setting('api_base') || '')."/object/device/{ip}/$rel",
      description => "Returns $rel rows for a given device",
      parameters  => [
        ip => {
          description => 'Canonical IP of the Device. Use Search methods to find this.',



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