Tenant list - order by

Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/2dcc1394
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/2dcc1394
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/2dcc1394

Branch: refs/heads/master
Commit: 2dcc1394d3cea970eb42fa6322267062008dd869
Parents: 2eb94bf
Author: nir-sopher <[email protected]>
Authored: Fri Jun 2 15:16:11 2017 +0300
Committer: Jeremy Mitchell <[email protected]>
Committed: Tue Jul 18 12:12:31 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/lib/API/Tenant.pm     |  5 +++--
 traffic_ops/app/lib/UI/TenantUtils.pm | 22 ++++++++++++++--------
 traffic_ops/app/t/api/1.2/tenant.t    | 27 ++++++++++++++++++++++-----
 3 files changed, 39 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/2dcc1394/traffic_ops/app/lib/API/Tenant.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/Tenant.pm 
b/traffic_ops/app/lib/API/Tenant.pm
index ca67365..ab335e2 100644
--- a/traffic_ops/app/lib/API/Tenant.pm
+++ b/traffic_ops/app/lib/API/Tenant.pm
@@ -33,7 +33,8 @@ sub getTenantName {
 }
 
 sub index {
-       my $self        = shift;
+       my $self        = shift;        
+       my $orderby = $self->param('orderby') || "name";
 
        my %idnames;
        my $rs_data = $self->db->resultset("Tenant")->search();
@@ -43,7 +44,7 @@ sub index {
 
        my @data = ();
        my $tenantUtils = UI::TenantUtils->new($self);
-       my @tenants_list = $tenantUtils->get_hierarchic_tenants_list();
+       my @tenants_list = $tenantUtils->get_hierarchic_tenants_list(undef, 
$orderby);
        foreach my $row (@tenants_list) {
                if ($tenantUtils->is_tenant_resource_readable($row->id)) {
                        push(

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/2dcc1394/traffic_ops/app/lib/UI/TenantUtils.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/UI/TenantUtils.pm 
b/traffic_ops/app/lib/UI/TenantUtils.pm
index a7a33e4..118ba79 100644
--- a/traffic_ops/app/lib/UI/TenantUtils.pm
+++ b/traffic_ops/app/lib/UI/TenantUtils.pm
@@ -28,6 +28,8 @@ sub new {
                user_tenant_id => -1,           
                tenants_dict => undef,
                root_tenants => undef,
+               order_by => -1,
+               ordered_by => undef,
        };
        return bless $self, $class;
 }
@@ -45,8 +47,9 @@ sub current_user_tenant {
 sub get_hierarchic_tenants_list {
        my $self = shift;
        my $tree_root = shift;  
+       my $order_by = shift;   
        
-       $self->_init_tenants_if_needed();
+       $self->_init_tenants_if_needed($order_by);
 
        my @stack = ();
        if (defined($tree_root)){
@@ -100,12 +103,13 @@ sub is_tenant_resource_writeable {
 
 sub _init_tenants {
        my $self = shift;
-       my $tenants_table = $self->{context}->db->resultset("Tenant")->search( 
undef, { order_by => "id" });
+       $self->{order_by} = shift || "name";#some default
+       my $tenants_table = $self->{context}->db->resultset("Tenant")->search( 
undef, { order_by => $self->{order_by} });
        
-       my @ordered_by_id = ();
+       $self->{ordered_by} = ();
        $self->{tenants_dict} = {};
        while ( my $row = $tenants_table->next ) {
-               push (@ordered_by_id, $row->id);
+               push (@{ $self->{ordered_by} }, $row->id);
                $self->{tenants_dict}->{$row->id} = {
                        row => $row,
                        parent => $row->parent_id,
@@ -114,7 +118,7 @@ sub _init_tenants {
        }
        
        $self->{root_tenants} = ();
-       foreach my $key (@ordered_by_id) {
+       foreach my $key (@{ $self->{ordered_by} }) {
                my $value = $self->{tenants_dict}->{$key};
                my $parent = $value->{parent};
                if (!defined($parent))
@@ -129,8 +133,10 @@ sub _init_tenants {
 
 sub _init_tenants_if_needed {
        my $self = shift;
-       if (!defined($self->{tenants_dict})) {
-               $self->_init_tenants();
+       my $order_by = shift;
+       if (($self->{order_by} == -1) || (defined($order_by) && $order_by != 
$self->{order_by})) {
+               ## first run to build the list OR (the order is important AND 
is not the current order)
+               $self->_init_tenants($order_by);
        }
 }
 
@@ -165,7 +171,7 @@ sub _is_resource_accessable {
                return 0;
        }
 
-       $self->_init_tenants_if_needed();
+       $self->_init_tenants_if_needed(undef);
        my $tenant_record = $self->{tenants_dict}->{$user_tenant};
        my $is_active_tenant = $tenant_record->{row}->active;
        if (! $is_active_tenant) {

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/2dcc1394/traffic_ops/app/t/api/1.2/tenant.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/tenant.t 
b/traffic_ops/app/t/api/1.2/tenant.t
index 285ea24..c2662b3 100644
--- a/traffic_ops/app/t/api/1.2/tenant.t
+++ b/traffic_ops/app/t/api/1.2/tenant.t
@@ -61,14 +61,14 @@ ok $t->post_ok('/api/1.2/tenants' => {Accept => 
'application/json'} => json => {
 
 #no parent - would not accept
 ok $t->post_ok('/api/1.2/tenants' => {Accept => 'application/json'} => json => 
{
-        "name" => "tenantB" })->status_is(400);
+        "name" => "tenant" })->status_is(400);
 
 #now getting it excepted
 ok $t->post_ok('/api/1.2/tenants' => {Accept => 'application/json'} => json => 
{
-        "name" => "tenantB", "parentId" => $root_tenant_id })->status_is(200);
+        "name" => "tenant", "parentId" => $root_tenant_id })->status_is(200);
 
 my $tenantA_id = &get_tenant_id('tenantA');
-my $tenantB_id = &get_tenant_id('tenantB');
+my $tenantB_id = &get_tenant_id('tenant');
 #rename, and move to active
 ok $t->put_ok('/api/1.2/tenants/' . $tenantA_id  => {Accept => 
'application/json'} => json => {
                        "name" => "tenantA2", "active" => 1, "parentId" => 
$root_tenant_id 
@@ -148,13 +148,30 @@ ok $t->post_ok('/api/1.2/tenants' => {Accept => 
'application/json'} => json => {
 my $tenantD_id = &get_tenant_id('tenantD');
 my $tenantE_id = &get_tenant_id('tenantE');
 
-#list tenants- verify heirachic order
-$t->get_ok("/api/1.2/tenants")->status_is(200)->json_is( "/response/0/id", 
$root_tenant_id )
+#list tenants- verify heirachic order - order by id
+$t->get_ok("/api/1.2/tenants?orderby=id")->status_is(200)
+       ->json_is( "/response/0/id", $root_tenant_id )
        ->json_is( "/response/1/id", $tenantA_id)
        ->json_is( "/response/2/id", $tenantD_id)
        ->json_is( "/response/3/id", $tenantE_id)
        ->json_is( "/response/4/id", $tenantB_id)->or( sub { diag 
$t->tx->res->content->asset->{content}; } );;
 
+#list tenants- verify heirachic order - order by name
+$t->get_ok("/api/1.2/tenants?orderby=name")->status_is(200)
+       ->json_is( "/response/0/id", $root_tenant_id )
+       ->json_is( "/response/2/id", $tenantA_id)
+       ->json_is( "/response/3/id", $tenantD_id)
+       ->json_is( "/response/4/id", $tenantE_id)
+       ->json_is( "/response/1/id", $tenantB_id)->or( sub { diag 
$t->tx->res->content->asset->{content}; } );;
+
+#list tenants- verify heirachic order - order by name (default)
+$t->get_ok("/api/1.2/tenants")->status_is(200)
+       ->json_is( "/response/0/id", $root_tenant_id )
+       ->json_is( "/response/2/id", $tenantA_id)
+       ->json_is( "/response/3/id", $tenantD_id)
+       ->json_is( "/response/4/id", $tenantE_id)
+       ->json_is( "/response/1/id", $tenantB_id)->or( sub { diag 
$t->tx->res->content->asset->{content}; } );;
+
 #cannot delete a tenant that have children
 ok $t->delete_ok('/api/1.2/tenants/' . $tenantA_id)->status_is(400)
        ->json_is( "/alerts/0/text" => "Tenant 'tenantA2' has children 
tenant(s): e.g 'tenantD'. Please update these tenants and retry." )

Reply via email to