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

    
https://github.com/apache/incubator-trafficcontrol/pull/371#discussion_r106613824
  
    --- 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)) {
    +           return $self->alert("Root user cannot be in-active.");
    +   }
    +   
    +
    +   if ( !defined($params->{parentId}) && !isRootTenant($id) ) {
    +           return $self->alert("Only the \"root\" tenant can have no 
parent.");
    +   }
    +   
    +   my $values = {
    +           name      => $params->{name},
    +           active    => $params->{active},
    +           parent_id => $params->{parentId}
    +   };
    +
    +   my $rs = $tenant->update($values);
    +   if ($rs) {
    +           my $response;
    +           $response->{id}          = $rs->id;
    +           $response->{name}        = $rs->name;
    +           $response->{active}      = \$rs->active;
    +           $response->{parentId}    = $rs->parent_id;
    +           $response->{lastUpdated} = $rs->last_updated;
    +           &log( $self, "Updated Tenant name '" . $rs->name . "' for id: " 
. $rs->id, "APICHANGE" );
    +           return $self->success( $response, "Tenant update was 
successful." );
    +   }
    +   else {
    +           return $self->alert("Tenant update failed.");
    +   }
    +
    +}
    +
    +
    +sub create {
    +   my $self   = shift;
    +   my $params = $self->req->json;
    +
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   my $name = $params->{name};
    +   if ( !defined($name) ) {
    +           return $self->alert("Tenant name is required.");
    +   }
    +
    +   my $parent_id = $params->{parentId};
    +   if ( !defined($parent_id) ) {
    +           return $self->alert("Parent Id is required.");
    +   }
    +
    +   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.");
    +   }
    +
    +   my $is_active = exists($params->{active})? $params->{active} : 0; 
#optional, if not set use default
    +   
    +   if ( !$is_active && !defined($parent_id)) {
    +           return $self->alert("Root user cannot be in-active.");
    +   }
    +   
    +   my $values = {
    +           name            => $params->{name} ,
    +           active          => $is_active,
    +           parent_id       => $params->{parentId}
    +   };
    +
    +   my $insert = $self->db->resultset('Tenant')->create($values);
    +   my $rs = $insert->insert();
    +   if ($rs) {
    +           my $response;
    +           $response->{id}                 = $rs->id;
    +           $response->{name}               = $rs->name;
    +           $response->{active}             = \$rs->active;
    +           $response->{parentId}           = $rs->parent_id;
    +           $response->{lastUpdated}        = $rs->last_updated;
    +
    +           &log( $self, "Created Tenant name '" . $rs->name . "' for id: " 
. $rs->id, "APICHANGE" );
    +
    +           return $self->success( $response, "Tenant create was 
successful." );
    +   }
    +   else {
    +           return $self->alert("Tenant create failed.");
    +   }
    +
    +}
    +
    +
    +sub delete {
    +   my $self = shift;
    +   my $id     = $self->param('id');
    +
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   my $tenant = $self->db->resultset('Tenant')->find( { id => $id } );
    +   if ( !defined($tenant) ) {
    +           return $self->not_found();
    +   }       
    +   my $name = $self->db->resultset('Tenant')->search( { id => $id } 
)->get_column('name')->single();
    +   
    +   my $existing_child = $self->db->resultset('Tenant')->search( { 
parent_id => $id } )->get_column('name')->first();
    +   if ($existing_child) {
    +           return $self->alert("Tenant '$name' has children tenant(s): e.g 
'$existing_child'. Please update these tenants and retry.");
    +   }
    +
    +   #The order of the below tests is intentional - allowing UT to cover all 
cases - TODO(nirs) remove this comment when a full "tenancy" UT is added, 
including permissions and such (no use in putting effort into it yet)
    +   #TODO(nirs) - add back when making available: my $existing_ds = 
$self->db->resultset('Deliveryservice')->search( { tenant_id => $id 
})->get_column('xml_id')->first();
    +   #TODO(nirs) - add back when making available: if ($existing_ds) {
    +   #TODO(nirs) - add back when making available:   return 
$self->alert("Tenant '$name' is assign with delivery-services(s): e.g. 
'$existing_ds'. Please update/delete these delivery-services and retry.");
    +   #TODO(nirs) - add back when making available: }
    +
    +   #TODO(nirs) - add back when making available: my $existing_cdn = 
$self->db->resultset('Cdn')->search( { tenant_id => $id 
})->get_column('name')->first();
    --- End diff --
    
    No Problem. 
    Note that you'll need to be careful on the merge when the next 3 PRs are 
added - as the order of the tests is important for the UT, I hope to fix it 
later on.


---
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