dewrich closed pull request #2191: revert changes that broke perl unit tests URL: https://github.com/apache/incubator-trafficcontrol/pull/2191
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/traffic_ops/app/lib/API/Topology.pm b/traffic_ops/app/lib/API/Topology.pm new file mode 100644 index 000000000..ff72b57dd --- /dev/null +++ b/traffic_ops/app/lib/API/Topology.pm @@ -0,0 +1,103 @@ +package API::Topology; +# +## +## 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. +## +## +## +# +## 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 Mojo::Base 'Mojolicious::Controller'; +use JSON; +use MojoPlugins::Response; +use UI::Utils; +use UI::Topology; +use Data::Dumper; + +sub SnapshotCRConfig { + my $self = shift; + my $cdn_id = $self->param('id'); + my $cdn_name = $self->param('cdn_name'); + my $cdn; + + if ( !&is_oper($self) ) { + return $self->forbidden("You must be an ADMIN or OPER to perform this operation!"); + } + + if ( defined $cdn_id ) { + $cdn = $self->db->resultset("Cdn")->find( { id => $cdn_id } ); + $cdn_name = $cdn->name if defined $cdn; + } + + if ( !defined $cdn ) { + $cdn = $self->db->resultset('Cdn')->find( { name => $cdn_name } ); + if ( !defined($cdn) ) { + return $self->not_found(); + } + } + + my @cdn_names = $self->db->resultset('Server')->search({ 'type.name' => 'EDGE' }, { prefetch => [ 'cdn', 'type' ], group_by => 'cdn.name' } )->get_column('cdn.name')->all(); + my $num = grep /^$cdn_name$/, @cdn_names; + if ($num <= 0) { + return $self->alert("CDN_name [" . $cdn_name. "] is not found in edge server cdn"); + } + + my $json = &UI::Topology::gen_crconfig_json($self, $cdn_name); + &UI::Topology::write_crconfig_json_to_db($self, $cdn_name, $json); + &UI::Utils::log($self, "Snapshot of CRConfig performed for $cdn_name", "APICHANGE"); + return $self->success("SUCCESS"); +} + +sub get_snapshot { + my $self = shift; + my $cdn_name = $self->param('name'); + + if ( !&is_oper($self) ) { + return $self->forbidden(); + } + + my $cdn = $self->db->resultset('Cdn')->find( { name => $cdn_name } ); + if ( !defined($cdn) ) { + return $self->not_found(); + } + + my $snapshot = $self->db->resultset('Snapshot')->search( { cdn => $cdn_name } )->get_column('content')->single(); + if ( !defined($snapshot) ) { + return $self->success( {} ); + } + + $self->success( decode_json($snapshot) ); +} + +sub get_new_snapshot { + my $self = shift; + my $cdn_name = $self->param('name'); + + if ( !&is_oper($self) ) { + return $self->forbidden(); + } + + my $cdn = $self->db->resultset('Cdn')->find( { name => $cdn_name } ); + if ( !defined($cdn) ) { + return $self->not_found(); + } + + my $json = &UI::Topology::gen_crconfig_json($self, $cdn_name); + + $self->success( $json ); +} + +1; diff --git a/traffic_ops/app/lib/TrafficOpsRoutes.pm b/traffic_ops/app/lib/TrafficOpsRoutes.pm index 81bf55bce..5c944b957 100644 --- a/traffic_ops/app/lib/TrafficOpsRoutes.pm +++ b/traffic_ops/app/lib/TrafficOpsRoutes.pm @@ -449,6 +449,13 @@ sub api_routes { # -- CDNS: ROUTING $r->get("/api/$version/cdns/routing")->over( authenticated => 1, not_ldap => 1 )->to( 'Cdn#routing', namespace => $namespace ); + # -- CDNS: SNAPSHOT + $r->get("/api/$version/cdns/:name/snapshot")->over( authenticated => 1, not_ldap => 1 )->to( 'Topology#get_snapshot', namespace => $namespace ); + $r->get("/api/$version/cdns/:name/snapshot/new")->over( authenticated => 1, not_ldap => 1 )->to( 'Topology#get_new_snapshot', namespace => $namespace ); + $r->put( "/api/$version/cdns/:id/snapshot" => [ id => qr/\d+/ ] )->over( authenticated => 1, not_ldap => 1 ) + ->to( 'Topology#SnapshotCRConfig', namespace => $namespace ); + $r->put("/api/$version/snapshot/:cdn_name")->over( authenticated => 1, not_ldap => 1 )->to( 'Topology#SnapshotCRConfig', namespace => $namespace ); + # -- CDNS: METRICS #WARNING: this is an intentionally "unauthenticated" route. $r->get("/api/$version/cdns/metric_types/:metric_type/start_date/:start_date/end_date/:end_date")->to( 'Cdn#metrics', namespace => $namespace ); diff --git a/traffic_ops/app/lib/UI/Topology.pm b/traffic_ops/app/lib/UI/Topology.pm index aef8cadd0..ee0e750f7 100644 --- a/traffic_ops/app/lib/UI/Topology.pm +++ b/traffic_ops/app/lib/UI/Topology.pm @@ -573,6 +573,22 @@ sub gen_crconfig_json { return ($data_obj); } +sub write_crconfig_json_to_db { + my $self = shift; + my $cdn_name = shift; + my $crconfig_db = shift; + my $crconfig_json = encode_json($crconfig_db); + + my $snapshot = $self->db->resultset('Snapshot')->find( { cdn => $cdn_name } ); + if ( defined($snapshot) ) { + $snapshot->update({ content => $crconfig_json }); + } else { + my $insert = $self->db->resultset('Snapshot')->create( { cdn => $cdn_name, content => $crconfig_json } ); + $insert->insert(); + } + +} + sub diff_crconfig_json { my $self = shift; my $json = shift; ---------------------------------------------------------------- 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
