You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2017/05/24 02:40:16 UTC
[11/14] incubator-trafficcontrol git commit: removed hardcoding row
index perltidy
removed hardcoding row index
perltidy
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/e782bc62
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/e782bc62
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/e782bc62
Branch: refs/heads/master
Commit: e782bc621e5964a3f51470a9b5980d0572e5acf2
Parents: 8d6ddc7
Author: naamashoresh <na...@qwilt.com>
Authored: Sun May 21 13:30:46 2017 +0300
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Tue May 23 20:39:03 2017 -0600
----------------------------------------------------------------------
traffic_ops/app/lib/API/ApiCapability.pm | 151 ++++++++++++++------------
1 file changed, 80 insertions(+), 71 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/e782bc62/traffic_ops/app/lib/API/ApiCapability.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/ApiCapability.pm b/traffic_ops/app/lib/API/ApiCapability.pm
index 5686d00..e143b1c 100644
--- a/traffic_ops/app/lib/API/ApiCapability.pm
+++ b/traffic_ops/app/lib/API/ApiCapability.pm
@@ -21,11 +21,9 @@ use UI::Utils;
use Mojo::Base 'Mojolicious::Controller';
use Data::Dumper;
-
-
my $finfo = __FILE__ . ":";
-my %valid_http_methods = map { $_ => 1 } ('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
+my %valid_http_methods = map { $_ => 1 } ( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' );
sub index {
my $self = shift;
@@ -43,31 +41,30 @@ sub index {
while ( my $row = $rs_data->next ) {
push(
@data, {
- "id" => $row->id,
- "httpMethod" => $row->http_method,
- "route" => $row->route,
- "capability" => $row->capability->name,
- "lastUpdated" => $row->last_updated
+ "id" => $row->id,
+ "httpMethod" => $row->http_method,
+ "route" => $row->route,
+ "capability" => $row->capability->name,
+ "lastUpdated" => $row->last_updated
}
);
}
$self->success( \@data );
}
-
sub renderResults {
- my $self = shift;
+ my $self = shift;
my $rs_data = shift;
my @data = ();
while ( my $row = $rs_data->next ) {
push(
@data, {
- "id" => $row->id,
- "httpMethod" => $row->http_method,
- "route" => $row->route,
- "capability" => $row->capability->name,
- "lastUpdated" => $row->last_updated
+ "id" => $row->id,
+ "httpMethod" => $row->http_method,
+ "route" => $row->route,
+ "capability" => $row->capability->name,
+ "lastUpdated" => $row->last_updated
}
);
}
@@ -76,25 +73,28 @@ sub renderResults {
sub show {
my $self = shift;
- my $id = $self->param('id');
+ my $id = $self->param('id');
my $rs_data = $self->db->resultset("ApiCapability")->search( 'me.id' => $id );
- $self->renderResults( $rs_data ) ;
+ if ( !defined($rs_data) ) {
+ return $self->not_found();
+ }
+ $self->renderResults($rs_data);
}
sub is_mapping_valid {
- my $self = shift;
- my $id = shift;
+ my $self = shift;
+ my $id = shift;
my $http_method = shift;
- my $route = shift;
- my $capability = shift;
+ my $route = shift;
+ my $capability = shift;
if ( !defined($http_method) ) {
return ( undef, "HTTP method is required." );
}
- if ( !exists( $valid_http_methods{ $http_method } ) ) {
- return ( undef, "HTTP method \'$http_method\' is invalid. Valid values are: " . join(", ", sort keys %valid_http_methods ) );
+ if ( !exists( $valid_http_methods{$http_method} ) ) {
+ return ( undef, "HTTP method \'$http_method\' is invalid. Valid values are: " . join( ", ", sort keys %valid_http_methods ) );
}
if ( !defined($route) or $route eq "" ) {
@@ -102,30 +102,35 @@ sub is_mapping_valid {
}
if ( !defined($capability) or $capability eq "" ) {
- return (undef, "Capability name is required." );
+ return ( undef, "Capability name is required." );
}
+
# check if capability exists
my $rs_data = $self->db->resultset("Capability")->search( { 'name' => { 'like', $capability } } )->single();
- if (!defined($rs_data)) {
- return (undef, "Capability '$capability' does not exist." );
+ if ( !defined($rs_data) ) {
+ return ( undef, "Capability '$capability' does not exist." );
}
# search a mapping for the same http_method & route
- $rs_data = $self->db->resultset("ApiCapability")->search( { 'route' => { 'like', $route } } )->search( {
- 'http_method' => { '=', $http_method } } )->single();
+ $rs_data = $self->db->resultset("ApiCapability")->search( { 'route' => { 'like', $route } } )->search(
+ {
+ 'http_method' => { '=', $http_method }
+ }
+ )->single();
+
# if adding a new entry, make sure it is unique
- if ( !defined( $id ) ) {
- if (defined($rs_data)) {
+ if ( !defined($id) ) {
+ if ( defined($rs_data) ) {
my $allocated_capability = $rs_data->capability->name;
- return (undef, "HTTP method '$http_method', route '$route' are already mapped to capability: $allocated_capability" );
+ return ( undef, "HTTP method '$http_method', route '$route' are already mapped to capability: $allocated_capability" );
}
}
else {
- if (defined($rs_data)) {
+ if ( defined($rs_data) ) {
my $lid = $rs_data->id;
- if ($lid ne $id) {
+ if ( $lid ne $id ) {
my $allocated_capability = $rs_data->capability->name;
- return (undef, "HTTP method '$http_method', route '$route' are already mapped to capability: $allocated_capability" );
+ return ( undef, "HTTP method '$http_method', route '$route' are already mapped to capability: $allocated_capability" );
}
}
}
@@ -134,7 +139,7 @@ sub is_mapping_valid {
}
sub create {
- my $self = shift;
+ my $self = shift;
my $params = $self->req->json;
if ( !&is_oper($self) ) {
@@ -145,45 +150,46 @@ sub create {
return $self->alert("Parameters must be in JSON format.");
}
- my $http_method = $params->{httpMethod} if defined($params->{httpMethod});
- my $route = $params->{route} if defined($params->{route});
- my $capability = $params->{capability} if defined($params->{capability});
- my $id = undef;
+ my $http_method = $params->{httpMethod} if defined( $params->{httpMethod} );
+ my $route = $params->{route} if defined( $params->{route} );
+ my $capability = $params->{capability} if defined( $params->{capability} );
+ my $id = undef;
my ( $is_valid, $errStr ) = $self->is_mapping_valid( $id, $http_method, $route, $capability );
if ( !$is_valid ) {
- return $self->alert( $errStr );
+ return $self->alert($errStr);
}
my $values = {
- id => $self->db->resultset('ApiCapability')->get_column('id')->max() + 1,
- http_method => $http_method,
- route => $route,
- capability => $capability
+ http_method => $http_method,
+ route => $route,
+ capability => $capability
};
my $insert = $self->db->resultset('ApiCapability')->create($values);
- my $rs = $insert->insert();
+ my $rs = $insert->insert();
if ($rs) {
my $response;
- $response->{id} = $rs->id;
- $response->{httpMethod} = $rs->http_method;
- $response->{route} = $rs->route;
- $response->{capability} = $rs->capability->name;
- $response->{lastUpdated} = $rs->last_updated;
+ $response->{id} = $rs->id;
+ $response->{httpMethod} = $rs->http_method;
+ $response->{route} = $rs->route;
+ $response->{capability} = $rs->capability->name;
+ $response->{lastUpdated} = $rs->last_updated;
- &log( $self, "Created API-Capability mapping: '$response->{httpMethod}', '$response->{route}', '$response->{capability}' for id: " . $response->{id}, "APICHANGE" );
+ &log( $self,
+ "Created API-Capability mapping: '$response->{httpMethod}', '$response->{route}', '$response->{capability}' for id: " . $response->{id},
+ "APICHANGE" );
return $self->success( $response, "API-Capability mapping was created." );
}
else {
- return $self->alert( "API-Capability mapping creation failed." );
+ return $self->alert("API-Capability mapping creation failed.");
}
}
sub update {
- my $self = shift;
- my $id = $self->param('id');
+ my $self = shift;
+ my $id = $self->param('id');
my $params = $self->req->json;
if ( !&is_oper($self) ) {
@@ -194,9 +200,9 @@ sub update {
return $self->alert("Parameters must be in JSON format.");
}
- my $http_method = $params->{httpMethod} if defined($params->{httpMethod});
- my $route = $params->{route} if defined($params->{route});
- my $capability = $params->{capability} if defined($params->{capability});
+ my $http_method = $params->{httpMethod} if defined( $params->{httpMethod} );
+ my $route = $params->{route} if defined( $params->{route} );
+ my $capability = $params->{capability} if defined( $params->{capability} );
my $mapping = $self->db->resultset('ApiCapability')->find( { id => $id } );
if ( !defined($mapping) ) {
@@ -205,36 +211,38 @@ sub update {
my ( $is_valid, $errStr ) = $self->is_mapping_valid( $id, $http_method, $route, $capability );
if ( !$is_valid ) {
- return $self->alert( $errStr );
+ return $self->alert($errStr);
}
my $values = {
- http_method => $http_method,
- route => $route,
- capability => $capability
+ http_method => $http_method,
+ route => $route,
+ capability => $capability
};
my $rs = $mapping->update($values);
if ($rs) {
my $response;
- $response->{id} = $rs->id;
- $response->{httpMethod} = $rs->http_method;
- $response->{route} = $rs->route;
- $response->{capability} = $rs->capability->name;
- $response->{lastUpdated} = $rs->last_updated;
+ $response->{id} = $rs->id;
+ $response->{httpMethod} = $rs->http_method;
+ $response->{route} = $rs->route;
+ $response->{capability} = $rs->capability->name;
+ $response->{lastUpdated} = $rs->last_updated;
- &log( $self, "Updated API-Capability mapping: '$response->{httpMethod}', '$response->{route}', '$response->{capability}' for id: " . $response->{id}, "APICHANGE" );
+ &log( $self,
+ "Updated API-Capability mapping: '$response->{httpMethod}', '$response->{route}', '$response->{capability}' for id: " . $response->{id},
+ "APICHANGE" );
return $self->success( $response, "API-Capability mapping was updated." );
}
else {
- return $self->alert( "API-Capability mapping update failed." );
+ return $self->alert("API-Capability mapping update failed.");
}
}
sub delete {
my $self = shift;
- my $id = $self->param('id');
+ my $id = $self->param('id');
if ( !&is_oper($self) ) {
return $self->forbidden();
@@ -248,8 +256,9 @@ sub delete {
my $rs = $mapping->delete();
if ($rs) {
return $self->success_message("API-capability mapping deleted.");
- } else {
- return $self->alert( "API-capability mapping deletion failed." );
+ }
+ else {
+ return $self->alert("API-capability mapping deletion failed.");
}
}