Skip to content
This repository has been archived by the owner on Nov 13, 2018. It is now read-only.

Issue/863 - The API @ /api/1.2/hwinfo.json will fail with an empty reply from the server when too many rows are returned #869

Merged
merged 4 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions traffic_ops/app/lib/API/HwInfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ use UI::Utils;
use Mojo::Base 'Mojolicious::Controller';

sub index {
my $self = shift;
my $self = shift;
my $orderby = $self->param('orderby') || "serverid";
my $limit = $self->param('limit') || 1000;
my @data;

# get list of servers in one query
my $rs_data = $self->db->resultset("Hwinfo")->search( undef, { prefetch => [ { 'serverid' => undef, } ], order_by => 'me.' . $orderby } );
my $rs_data =
$self->db->resultset("Hwinfo")->search( undef, { prefetch => [ { 'serverid' => undef, } ], order_by => 'me.' . $orderby, rows => $limit } );
while ( my $row = $rs_data->next ) {
my $id = $row->id;
push(
Expand All @@ -39,7 +41,7 @@ sub index {
);
}

$self->success( \@data );
$self->success( \@data, undef, $limit, undef );
}

1;
126 changes: 101 additions & 25 deletions traffic_ops/app/lib/API/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use UI::Tools;
use MojoPlugins::Response;
use MojoPlugins::Job;
use Utils::Helper::ResponseHelper;
use String::CamelCase qw(decamelize);

sub index {
my $self = shift;
Expand Down Expand Up @@ -59,9 +60,12 @@ sub getserverdata {
my $self = shift;
my $ds_id = shift;
my @data;
my $isadmin = &is_admin($self);
my $orderby = $self->param('orderby') || "host_name";
my $isadmin = &is_admin($self);
my $orderby = $self->param('orderby') || "hostName";
my $limit = $self->param('limit') || 1000;
my $orderby_snakecase = lcfirst( decamelize($orderby) );
my $servers;

if ( defined $ds_id ) {

# we want the edge cache servers and mid cache servers (but only mids if the delivery service uses mids)
Expand All @@ -82,7 +86,7 @@ sub getserverdata {
$servers = $self->db->resultset('Server')->search(
[@criteria], {
prefetch => [ 'cdn', 'cachegroup', 'type', 'profile', 'status', 'phys_location' ],
order_by => 'me.' . $orderby,
order_by => 'me.' . $orderby_snakecase,
}
);
}
Expand All @@ -91,7 +95,7 @@ sub getserverdata {
$servers = $self->db->resultset('Server')->search(
undef, {
prefetch => [ 'cdn', 'cachegroup', 'type', 'profile', 'status', 'phys_location' ],
order_by => 'me.' . $orderby,
order_by => 'me.' . $orderby_snakecase,
}
);
}
Expand All @@ -105,8 +109,6 @@ sub getserverdata {
"hostName" => $row->host_name,
"domainName" => $row->domain_name,
"tcpPort" => $row->tcp_port,
"xmppId" => $row->xmpp_id,
"xmppPasswd" => "**********",
"interfaceName" => $row->interface_name,
"ipAddress" => $row->ip_address,
"ipNetmask" => $row->ip_netmask,
Expand Down Expand Up @@ -139,25 +141,24 @@ sub getserverdata {
return ( \@data );
}

sub summary {
sub totals {
my $self = shift;

# TODO: drichardson - loop through this select to make this more dynamic.
# Based on this: SELECT * FROM TYPE WHERE ID IN (SELECT TYPE FROM SERVER);
my $edges = $self->get_count_by_type('EDGE');
my $mids = $self->get_count_by_type('MID');
my $rascals = $self->get_count_by_type('RASCAL');
my $ccrs = $self->get_count_by_type('CCR');
my $redis = $self->get_count_by_type('REDIS');

my $response_body = [
{ type => 'CCR', count => $ccrs },
{ type => 'EDGE', count => $edges },
{ type => 'MID', count => $mids },
{ type => 'REDIS', count => $redis },
{ type => 'RASCAL', count => $rascals }
];
return $self->success($response_body);
my @data;
my @rs = $self->db->resultset('ServerTypes')->search();
foreach my $rs (@rs) {
my $type_name = $rs->name;
my $count = $self->get_count_by_type($type_name);
push(
@data, {
"type" => $rs->name,
"count" => $count,
}
);
}

return $self->success( \@data );

}

sub get_count_by_type {
Expand All @@ -166,7 +167,7 @@ sub get_count_by_type {
return $self->db->resultset('Server')->search( { 'type.name' => $type_name }, { join => 'type' } )->count();
}

sub details {
sub details_v11 {
my $self = shift;
my @data;
my $isadmin = &is_admin($self);
Expand All @@ -181,7 +182,7 @@ sub details {
"domainName" => $row->domain_name,
"tcpPort" => $row->tcp_port,
"xmppId" => $row->xmpp_id,
"xmppPasswd" => $row->xmpp_passwd,
"xmppPasswd" => $isadmin ? $row->xmpp_passwd : "********",
"interfaceName" => $row->interface_name,
"ipAddress" => $row->ip_address,
"ipNetmask" => $row->ip_netmask,
Expand Down Expand Up @@ -221,4 +222,79 @@ sub details {
$self->success(@data);
}

sub details {
my $self = shift;
my $orderby = $self->param('orderby') || "hostName";
my $orderby_snakecase = lcfirst( decamelize($orderby) );
my $limit = $self->param('limit') || 1000;
my @data;
my $isadmin = &is_admin($self);
my $phys_location_id = $self->param('physLocationID');
my $host_name = $self->param('hostName');

if ( !defined($phys_location_id) && !defined($host_name) ) {
return $self->alert("Missing required fields: 'hostName' or 'physLocationID'");
}

my $rs_data = $self->db->resultset('Server')->search(
[ { host_name => $host_name }, { phys_location => $phys_location_id } ], {
prefetch => [ 'cachegroup', 'type', 'profile', 'status', 'phys_location', 'hwinfos', 'deliveryservice_servers' ],
order_by => 'me.' . $orderby_snakecase
}
);

if ( $rs_data->count() > 0 ) {

while ( my $row = $rs_data->next ) {

my $serv = {
"id" => $row->id,
"hostName" => $row->host_name,
"domainName" => $row->domain_name,
"tcpPort" => $row->tcp_port,
"xmppId" => $row->xmpp_id,
"xmppPasswd" => $isadmin ? $row->xmpp_passwd : "********",
"interfaceName" => $row->interface_name,
"ipAddress" => $row->ip_address,
"ipNetmask" => $row->ip_netmask,
"ipGateway" => $row->ip_gateway,
"ip6Address" => $row->ip6_address,
"ip6Gateway" => $row->ip6_gateway,
"interfaceMtu" => $row->interface_mtu,
"cachegroup" => $row->cachegroup->name,
"physLocation" => $row->phys_location->name,
"rack" => $row->rack,
"type" => $row->type->name,
"status" => $row->status->name,
"profile" => $row->profile->name,
"mgmtIpAddress" => $row->mgmt_ip_address,
"mgmtIpNetmask" => $row->mgmt_ip_netmask,
"mgmtIpGateway" => $row->mgmt_ip_gateway,
"iloIpAddress" => $row->ilo_ip_address,
"iloIpNetmask" => $row->ilo_ip_netmask,
"iloIpGateway" => $row->ilo_ip_gateway,
"iloUsername" => $row->ilo_username,
"routerHostName" => $row->router_host_name,
"routerPortName" => $row->router_port_name,
};
my $hw_rs = $row->hwinfos;
while ( my $hwinfo_row = $hw_rs->next ) {
$serv->{hardwareInfo}->{ $hwinfo_row->description } = $hwinfo_row->val;
}

my $rs_ds_data = $row->deliveryservice_servers;
while ( my $dsrow = $rs_ds_data->next ) {
push( @{ $serv->{deliveryservices} }, $dsrow->deliveryservice->id );
}

push( @data, $serv );
}
my $size = @data;
$self->success( \@data, $orderby, $limit, $size );
}
else {
$self->success( [] );
}
}

1;
27 changes: 27 additions & 0 deletions traffic_ops/app/lib/Fixtures/Hwinfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ my %definition_for = (
val => '1.0.0.29',
},
},
hw3 => {
new => 'Hwinfo',
using => {
id => 3,
serverid => 2,
description => 'ServiceTag',
val => 'XXX',
},
},
hw4 => {
new => 'Hwinfo',
using => {
id => 4,
serverid => 2,
description => 'Manufacturer',
val => 'Dell Inc.',
},
},
hw5 => {
new => 'Hwinfo',
using => {
id => 5,
serverid => 2,
description => 'Model',
val => 'Beetle',
},
},
);

sub get_definition {
Expand Down
8 changes: 4 additions & 4 deletions traffic_ops/app/lib/Fixtures/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ my %definition_for = (
profile => 2,
cdn_id => 2,
cachegroup => 2,
phys_location => 1,
phys_location => 2,
},
},
riak_server2 => {
Expand Down Expand Up @@ -331,7 +331,7 @@ my %definition_for = (
profile => 5,
cdn_id => 1,
cachegroup => 1,
phys_location => 1,
phys_location => 2,
},
},
influxdb_server1 => {
Expand Down Expand Up @@ -366,7 +366,7 @@ my %definition_for = (
profile => 5,
cdn_id => 1,
cachegroup => 1,
phys_location => 1,
phys_location => 3,
},
},
influxdb_server2 => {
Expand Down Expand Up @@ -401,7 +401,7 @@ my %definition_for = (
profile => 5,
cdn_id => 1,
cachegroup => 1,
phys_location => 1,
phys_location => 3,
},
},
);
Expand Down
24 changes: 12 additions & 12 deletions traffic_ops/app/lib/MojoPlugins/Response.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ my $STATUS_KEY = "status";
my $JSON_KEY = "json";
my $RESPONSE_KEY = "response";
my $LIMIT_KEY = "limit";
my $SIZE_KEY = "size";
my $ORDERBY_KEY = "orderby";
my $PAGE_KEY = "page";
my $INFO_KEY = "supplemental";
Expand All @@ -50,24 +51,23 @@ sub register {
# optional args
my $orderby = shift;
my $limit = shift;
my $size = shift;
my $page = shift;

my $response_body;
if ( defined($limit) && defined($page) && defined($orderby) ) {
$response_body = { $RESPONSE_KEY => $body, $LIMIT_KEY => $limit, $PAGE_KEY => $page, $ORDERBY_KEY => $orderby };
my $response_body = { $RESPONSE_KEY => $body };
if ( defined($orderby) ) {
$response_body = merge( $response_body, { $ORDERBY_KEY => $orderby } );
}
elsif ( defined($limit) && defined($page) ) {
$response_body = { $RESPONSE_KEY => $body, $LIMIT_KEY => $limit, $PAGE_KEY => $page };
if ( defined($limit) ) {
$response_body = merge( $response_body, { $LIMIT_KEY => $limit } );
}
elsif ( defined($limit) ) {
$response_body = { $RESPONSE_KEY => $body, $LIMIT_KEY => $limit };
if ( defined($page) ) {
$response_body = merge( $response_body, { $PAGE_KEY => $page } );
}
elsif ( defined($page) ) {
$response_body = { $RESPONSE_KEY => $body, $PAGE_KEY => $limit };
}
else {
$response_body = { $RESPONSE_KEY => $body };
if ( defined($size) ) {
$response_body = merge( $response_body, { $SIZE_KEY => $size } );
}

return $self->render( $STATUS_KEY => 200, $JSON_KEY => $response_body );
}
);
Expand Down
51 changes: 51 additions & 0 deletions traffic_ops/app/lib/Schema/Result/ServerTypes.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use utf8;
#
# Copyright 2015 Comcast Cable Communications Management, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#

package Schema::Result::ServerTypes;

use strict;
use warnings;

use base 'DBIx::Class::Core';

__PACKAGE__->table_class('DBIx::Class::ResultSource::View');

__PACKAGE__->table("ServerTypes");

__PACKAGE__->result_source_instance->is_virtual(1);

__PACKAGE__->result_source_instance->view_definition("SELECT * FROM TYPE WHERE ID IN( SELECT TYPE FROM SERVER )");

__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"name",
{ data_type => "varchar", is_nullable => 0, size => 45 },
"description",
{ data_type => "varchar", is_nullable => 1, size => 256 },
"use_in_table",
{ data_type => "varchar", is_nullable => 1, size => 45 },
"last_updated", {
data_type => "timestamp",
datetime_undef_if_invalid => 1,
default_value => \"current_timestamp",
is_nullable => 1,
},
);
1;
Loading