make the branch authonly
This commit is contained in:
		@@ -13,24 +13,19 @@ use base 'DBIx::Class::Core';
 | 
			
		||||
__PACKAGE__->table("node_ip");
 | 
			
		||||
__PACKAGE__->add_columns(
 | 
			
		||||
  "mac",
 | 
			
		||||
  { data_type => "macaddr", is_nullable => 0,
 | 
			
		||||
    extra => { descr => 'MAC address' } },
 | 
			
		||||
  { data_type => "macaddr", is_nullable => 0 },
 | 
			
		||||
  "ip",
 | 
			
		||||
  { data_type => "inet", is_nullable => 0,
 | 
			
		||||
    extra => { descr => 'IP address' } },
 | 
			
		||||
  { data_type => "inet", is_nullable => 0 },
 | 
			
		||||
  "dns",
 | 
			
		||||
  { data_type => "text", is_nullable => 1,
 | 
			
		||||
    extra => { descr => 'FQDN of the node' } },
 | 
			
		||||
  { data_type => "text", is_nullable => 1 },
 | 
			
		||||
  "active",
 | 
			
		||||
  { data_type => "boolean", is_nullable => 1,
 | 
			
		||||
    extra => { descr => 'Whether the entry is still "fresh"' } },
 | 
			
		||||
  { data_type => "boolean", is_nullable => 1 },
 | 
			
		||||
  "time_first",
 | 
			
		||||
  {
 | 
			
		||||
    data_type     => "timestamp",
 | 
			
		||||
    default_value => \"current_timestamp",
 | 
			
		||||
    is_nullable   => 1,
 | 
			
		||||
    original      => { default_value => \"now()" },
 | 
			
		||||
    extra => { hide_from_api => 1 },
 | 
			
		||||
  },
 | 
			
		||||
  "time_last",
 | 
			
		||||
  {
 | 
			
		||||
@@ -38,7 +33,6 @@ __PACKAGE__->add_columns(
 | 
			
		||||
    default_value => \"current_timestamp",
 | 
			
		||||
    is_nullable   => 1,
 | 
			
		||||
    original      => { default_value => \"now()" },
 | 
			
		||||
    extra => { hide_from_api => 1 },
 | 
			
		||||
  },
 | 
			
		||||
);
 | 
			
		||||
__PACKAGE__->set_primary_key("mac", "ip");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,102 +0,0 @@
 | 
			
		||||
package App::Netdisco::Util::API;
 | 
			
		||||
 | 
			
		||||
use strict;
 | 
			
		||||
use warnings;
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::DBIC 'schema';
 | 
			
		||||
 | 
			
		||||
use List::MoreUtils 'singleton';
 | 
			
		||||
 | 
			
		||||
use base 'Exporter';
 | 
			
		||||
our @EXPORT = ();
 | 
			
		||||
our @EXPORT_OK = qw/
 | 
			
		||||
  resultsource_to_openapi_params
 | 
			
		||||
  parse_search_params
 | 
			
		||||
  format_data
 | 
			
		||||
  format_error
 | 
			
		||||
/;
 | 
			
		||||
our %EXPORT_TAGS = (all => \@EXPORT_OK);
 | 
			
		||||
 | 
			
		||||
sub resultsource_to_openapi_params {
 | 
			
		||||
  my $sourcename = shift or return ();
 | 
			
		||||
  my @params = ();
 | 
			
		||||
 | 
			
		||||
  my $rs = schema('netdisco')->source($sourcename) or return ();
 | 
			
		||||
  my $columns = $rs->columns_info;
 | 
			
		||||
 | 
			
		||||
  foreach my $col ($rs->primary_columns,
 | 
			
		||||
                   (singleton ($rs->primary_columns, keys %{ $columns }))) {
 | 
			
		||||
    my $data = $columns->{$col};
 | 
			
		||||
    next if $data->{extra}->{hide_from_api};
 | 
			
		||||
    push @params, (
 | 
			
		||||
      $col => {
 | 
			
		||||
        description => $data->{extra}->{descr},
 | 
			
		||||
        type => ($data->{data_type} =~ m/int/ ? 'integer'
 | 
			
		||||
          : $data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return @params;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub parse_search_params {
 | 
			
		||||
    my $params = shift;
 | 
			
		||||
    my $search = {};
 | 
			
		||||
    my $partial = $params->{partial} || false;
 | 
			
		||||
 | 
			
		||||
    foreach my $param (keys %{$params}) {
 | 
			
		||||
        if ($param ne 'return_url' and $param ne 'partial') {
 | 
			
		||||
            if ($partial eq 'true') {
 | 
			
		||||
                $search->{"text(".$param.")"} = { -ilike => '%'.$params->{$param}.'%'};
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                $search->{$param} = $params->{$param};
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return $search;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub format_data {
 | 
			
		||||
    my $items = shift;
 | 
			
		||||
    my $results = {};
 | 
			
		||||
 | 
			
		||||
    if (ref($items) =~ m/ResultSet/) {
 | 
			
		||||
        my @hashes;
 | 
			
		||||
 | 
			
		||||
        foreach my $item ($items->all) {
 | 
			
		||||
            my $c = {};
 | 
			
		||||
            my $columns = $item->{_column_data};
 | 
			
		||||
 | 
			
		||||
            foreach my $col (keys %{$columns}) {
 | 
			
		||||
                $c->{$col} = $columns->{$col};
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            push @hashes, $c;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $results->{data} = \@hashes;
 | 
			
		||||
    }
 | 
			
		||||
    elsif (ref($items) =~ m/Result/) {
 | 
			
		||||
        $results->{data} = $items->{_column_data};
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        $results->{data} = $items;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    header('Content-Type' => 'application/json');
 | 
			
		||||
    return to_json $results;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
sub format_error {
 | 
			
		||||
    my $status = shift;
 | 
			
		||||
    my $message = shift;
 | 
			
		||||
 | 
			
		||||
    status $status;
 | 
			
		||||
    header('Content-Type' => 'application/json');
 | 
			
		||||
    return to_json { error => $message };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
true;
 | 
			
		||||
@@ -232,14 +232,6 @@ hook 'after' => sub {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
# forward API calls to AJAX route handlers
 | 
			
		||||
any '/api/:type/:identifier/:method' => require_login sub {
 | 
			
		||||
    vars->{'is_api'} = 1;
 | 
			
		||||
    my $target =
 | 
			
		||||
      sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
 | 
			
		||||
    forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
any qr{.*} => sub {
 | 
			
		||||
    var('notfound' => true);
 | 
			
		||||
    status 'not_found';
 | 
			
		||||
 
 | 
			
		||||
@@ -4,25 +4,13 @@ use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::Ajax;
 | 
			
		||||
use Dancer::Plugin::DBIC;
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use Dancer::Plugin::Swagger;
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
 | 
			
		||||
register_device_tab({ tag => 'details', label => 'Details' });
 | 
			
		||||
 | 
			
		||||
# device details table
 | 
			
		||||
swagger_path {
 | 
			
		||||
    description => 'Get properties and power details for a device.',
 | 
			
		||||
    path => '/api/device/{identifier}/details',
 | 
			
		||||
    tags => ['Devices'],
 | 
			
		||||
    parameters => [
 | 
			
		||||
        { name => 'identifier', in => 'path', required => 1, type => 'string' },
 | 
			
		||||
    ],
 | 
			
		||||
    responses => { default => { examples => {
 | 
			
		||||
        'application/json' => { device => {}, power => {} },
 | 
			
		||||
    } } },
 | 
			
		||||
},
 | 
			
		||||
get '/ajax/content/device/details' => require_login sub {
 | 
			
		||||
ajax '/ajax/content/device/details' => require_login sub {
 | 
			
		||||
    my $q = param('q');
 | 
			
		||||
    my $device = schema('netdisco')->resultset('Device')
 | 
			
		||||
      ->search_for_device($q) or send_error('Bad device', 400);
 | 
			
		||||
@@ -36,18 +24,10 @@ get '/ajax/content/device/details' => require_login sub {
 | 
			
		||||
        = schema('netdisco')->resultset('DevicePower')
 | 
			
		||||
        ->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all;
 | 
			
		||||
 | 
			
		||||
    if (vars->{'is_api'}) {
 | 
			
		||||
        content_type('application/json');
 | 
			
		||||
        # TODO merge power into device details
 | 
			
		||||
        # TODO remove sensitive data (community)
 | 
			
		||||
        to_json { device => $results[0], power => \@power };
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
    content_type('text/html');
 | 
			
		||||
    template 'ajax/device/details.tt', {
 | 
			
		||||
      d => $results[0], p => \@power
 | 
			
		||||
    }, { layout => undef };
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
1;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user