mitchell852 commented on a change in pull request #2029: [Issue 1907] TO API 
for backup edge cachegroup
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2029#discussion_r184154970
 
 

 ##########
 File path: traffic_ops/app/lib/API/CachegroupFallback.pm
 ##########
 @@ -0,0 +1,208 @@
+package API::CachegroupFallback;
+#
+#
+# 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.
+#
+#
+#
+# a note about locations and cachegroups. This used to be "Location", before 
we had physical locations in 12M. Very confusing.
+# What used to be called a location is now called a "cache group" and location 
is now a physical address, not a group of caches working together.
+#
+
+# JvD Note: you always want to put Utils as the first use. Sh*t don't work if 
it's after the Mojo lines.
+use UI::Utils;
+use Mojo::Base 'Mojolicious::Controller';
+use Data::Dumper;
+use JSON;
+use MojoPlugins::Response;
+use Validate::Tiny ':all';
+
+sub delete {
+       my $self = shift;
+       my $cache_id = $self->param('cacheGroupId');
+       my $params = $self->req->json;
+
+       if ( !&is_oper($self) ) {
+               return $self->forbidden();
+       }
+
+       #only integers
+       if ( $cache_id !~ /^\d+?$/ ) {
+               &log( $self, "No such Cachegroup id $cache_id");
+               return $self->not_found();
+       }
+
+       my $cachegroup = $self->db->resultset('Cachegroup')->search( { id => 
$cache_id } )->single();
+       if ( !defined($cachegroup) ) {
+               &log( $self, "No such Cachegroup $cache_id");
+               return $self->not_found();
+       }
+
+       if ( ($cachegroup->type->name ne "EDGE_LOC") ) {
+               return $self->alert("cachegroup should be type EDGE_LOC.");
+       }
+
+       my $rs_backups = $self->db->resultset('CachegroupFallback')->search( { 
primary_cg => $cache_id } );
+       if ( ($rs_backups->count > 0) ) {
+               my $del_records = $rs_backups->delete();
+               if ($del_records) {
+                       &log( $self, "Backup configuration for cache group 
$cache_id DELETED", "APICHANGE");
+                       return $self->success_message("Backup configuration for 
cache group $cache_id DELETED");
+               } else {
+                       return $self->alert( "Backup configuration for cache 
group $cache_id DELETED." );
+               }
+       } else {
+               &log( $self, "No backup Cachegroups for $cache_id");
+               return $self->not_found();
+       }
+}
+
+sub show {
+       my $self = shift;
+       my $cache_id = $self->param("cacheGroupId");
+       my $fallback_id = $self->param("fallbackId");
+       my $id = $cache_id ? $cache_id : $fallback_id;
+
+       #only integers
+       if ( $id !~ /^\d+?$/ ) {
+               &log( $self, "No such Cachegroup id $id");
+               return $self->not_found();
+       }
+
+       my $cachegroup = $self->db->resultset('Cachegroup')->search( { id => 
$id } )->single();
+       if ( !defined($cachegroup) ) {
+               &log( $self, "No such Cachegroup $id");
+               return $self->not_found();
+       }
+
+       if ( ($cachegroup->type->name ne "EDGE_LOC") ) {
+               return $self->alert("cachegroup should be type EDGE_LOC.");
+       }
+
+       if ( defined ($cache_id) ) {
+               my $rs_backups = 
$self->db->resultset('CachegroupFallback')->search({ primary_cg => $cache_id}, 
{order_by => 'set_order'});
+                       if ( ($rs_backups->count > 0) ) {
+                               my $response;
+                               my $backup_cnt = 0;
+                               while ( my $row = $rs_backups->next ) {
+                                       
$response->[$backup_cnt]{"cacheGroupId"} = $cache_id;
+                                       
$response->[$backup_cnt]{"cacheGroupName"} = $row->primary_cg->name;
+                                       
$response->[$backup_cnt]{"fallbackName"} = $row->backup_cg->name;
+                                       $response->[$backup_cnt]{"fallbackId"} 
= $row->backup_cg->id;
+                                       
$response->[$backup_cnt]{"fallbackOrder"} = $row->set_order;
+                                       $backup_cnt++;
+                               }
+                               return $self->success( $response );
+                       } else {
+                               &log( $self, "No backup Cachegroups for 
$cache_id");
+                               return $self->not_found();
+                       }
+
+       } elsif ( defined ($fallback_id) ) {
+               my $rs_backups = 
$self->db->resultset('CachegroupFallback')->search({ backup_cg => 
$fallback_id}, {order_by => 'set_order'});
+                       if ( ($rs_backups->count > 0) ) {
+                               my $response;
+                               my $backup_cnt = 0;
+                               while ( my $row = $rs_backups->next ) {
+                                       $response->[$backup_cnt]{"fallbackId"} 
= $fallback_id;
+                                       
$response->[$backup_cnt]{"fallbackName"} = $row->backup_cg->name;
+                                       
$response->[$backup_cnt]{"cacheGroupName"} = $row->primary_cg->name;
+                                       
$response->[$backup_cnt]{"cacheGroupId"} = $row->primary_cg->id;
+                                       
$response->[$backup_cnt]{"fallbackOrder"} = $row->set_order;
+                                       $backup_cnt++;
+                               }
+                               return $self->success( $response );
+                       } else {
+                               &log( $self, "No Cachegroups has $fallback_id 
as backup");
+                               return $self->not_found();
+                       }
+       }
+}
+
+sub update {
+       my $self = shift;
+       my $cache_id = $self->param('cacheGroupId');
+       my $params = $self->req->json;
+
+       if ( !defined($cache_id)) {
+               my @param_array = @{$params};
+               $cache_id = $param_array[0]{cacheGroupId};
+       }
+
+       if ( !defined($params) ) {
+               return $self->alert("parameters must be in JSON format,  please 
check!");
+       }
+
+       if ( !&is_oper($self) ) {
+               return $self->forbidden();
+       }
+
+       #only integers
+       if ( $cache_id !~ /^\d+?$/ ) {
+               &log( $self, "No such Cachegroup id $cache_id");
+               return $self->not_found();
+       }
+
+       my $cachegroup = $self->db->resultset('Cachegroup')->search( { id => 
$cache_id } )->single();
+       if ( !defined($cachegroup) ) {
+               return $self->not_found();
+       }
+
+       if ( ($cachegroup->type->name ne "EDGE_LOC") ) {
+               return $self->alert("cachegroup should be type EDGE_LOC.");
+       }
+
+       my $rs_backups = $self->db->resultset('CachegroupFallback')->search( { 
primary_cg => $cache_id } );
+       if ( defined ($rs_backups->next) ) {
+               &log( $self, "Deleting all existing backup configuration for 
cache group $cache_id");
+               $rs_backups->delete();
 
 Review comment:
   this is deleting the entire list and replacing it with the contents of the 
POST
   
   i thought we discussed simply adding the item if it doesn't exist in the db 
and updating it if it does.
   
   i really don't think doing a POST (create) should be deleting things. it is 
misleading to the api consumer. for example, say there are 5 cg fallbacks and i 
want to add one to the list. so i call the POST with an array of 1 item. now i 
would expect to have 6 fallbacks but instead i only end up with 1.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to