Github user nir-sopher commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/371#discussion_r106616105
  
    --- Diff: traffic_ops/app/lib/API/Tenant.pm ---
    @@ -0,0 +1,247 @@
    +package API::Tenant;
    +#
    +#
    +# 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.
    +#
    +#
    +#
    +
    +use UI::Utils;
    +
    +use Mojo::Base 'Mojolicious::Controller';
    +use Data::Dumper;
    +use JSON;
    +use MojoPlugins::Response;
    +
    +my $finfo = __FILE__ . ":";
    +
    +sub getTenantName {
    +   my $self                = shift;
    +   my $tenant_id           = shift;
    +   return defined($tenant_id) ? $self->db->resultset('Tenant')->search( { 
id => $tenant_id } )->get_column('name')->single() : "n/a";
    +}
    +
    +sub isRootTenant {
    +   my $self        = shift;
    +   my $tenant_id   = shift;
    +   return !defined($self->db->resultset('Tenant')->search( { id => 
$tenant_id } )->get_column('parent_id')->single());
    +}
    +
    +sub index {
    +   my $self        = shift;
    +
    +   my @data;
    +   my $orderby = $self->param('orderby') || "name";
    +   my $rs_data = $self->db->resultset("Tenant")->search( undef, {order_by 
=> 'me.' . $orderby } );
    +   while ( my $row = $rs_data->next ) {
    +           push(
    +                   @data, {
    +                           "id"           => $row->id,
    +                           "name"         => $row->name,
    +                           "active"       => \$row->active,
    +                           "parentId"     => $row->parent_id,
    +                   }
    +           );
    +   }
    +   $self->success( \@data );
    +}
    +
    +
    +sub show {
    +   my $self = shift;
    +   my $id   = $self->param('id');
    +
    +   my $rs_data = $self->db->resultset("Tenant")->search( { 'me.id' => $id 
});
    +   my @data = ();
    +   while ( my $row = $rs_data->next ) {
    +           push(
    +                   @data, {
    +                           "id"           => $row->id,
    +                           "name"         => $row->name,
    +                           "active"       => \$row->active,
    +                           "parentId"     => $row->parent_id,
    +                   }
    +           );
    +   }
    +   $self->success( \@data );
    +}
    +
    +sub update {
    +   my $self   = shift;
    +   my $id     = $self->param('id');
    +   my $params = $self->req->json;
    +
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   my $tenant = $self->db->resultset('Tenant')->find( { id => $id } );
    +   if ( !defined($tenant) ) {
    +           return $self->not_found();
    +   }
    +
    +   if ( !defined($params) ) {
    +           return $self->alert("Parameters must be in JSON format.");
    +   }
    +
    +   if ( !defined( $params->{name} ) ) {
    +           return $self->alert("Tenant name is required.");
    +   }
    +   
    +   if ( $params->{name} ne $self->getTenantName($id) ) {
    +           my $name = $params->{name};
    +           my $existing = $self->db->resultset('Tenant')->search( { name 
=> $name } )->get_column('name')->single();
    +           if ($existing) {
    +                   return $self->alert("A tenant with name \"$name\" 
already exists.");
    +           }       
    +   }       
    +
    +   if ( !defined( $params->{parentId}) && !$self->isRootTenant($id) ) {
    +           return $self->alert("Parent Id is required.");
    +   }
    +   
    +   my $is_active = $params->{active};
    +   
    +   if ( !$params->{active} && $self->isRootTenant($id)) {
    --- End diff --
    
    I do not fully understand your point here. 
    The highlighted block does not come to verify that the parameter is set. It 
comes to block the ability to disable a "root" tenant - a change that cannot be 
fixed back without working directly with the DB.
    I also believe "active" should be mandatory. Should I test the the 
params->{active} "exists"?
    Nir


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to