Github user mitchell852 commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/199#discussion_r97381236
  
    --- Diff: traffic_ops/app/lib/API/Configs/ApacheTrafficServer.pm ---
    @@ -0,0 +1,1941 @@
    +package API::Configs::ApacheTrafficServer;
    +
    +#
    +#
    +# 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 Date::Manip;
    +use NetAddr::IP;
    +use Data::Dumper;
    +use UI::DeliveryService;
    +use JSON;
    +use API::DeliveryService::KeysUrlSig qw(URL_SIG_KEYS_BUCKET);
    +use URI;
    +use File::Basename;
    +use File::Path;
    +
    +use constant {
    +   HTTP                                    => 0,
    +   HTTPS                                   => 1,
    +   HTTP_AND_HTTPS                  => 2,
    +   HTTP_TO_HTTPS                   => 3,
    +   CONSISTENT_HASH                 => 0,
    +   PRIMARY_BACKUP                  => 1,
    +   STRICT_ROUND_ROBIN              => 2,
    +   IP_ROUND_ROBIN                  => 3,
    +   LATCH_ON_FAILOVER               => 4,
    +   RRH_DONT_CACHE                  => 0,
    +   RRH_BACKGROUND_FETCH    => 1,
    +   RRH_CACHE_RANGE_REQUEST => 2,
    +};
    +
    +#Sub to generate config metadata
    +sub get_config_metadata {
    +   my $self     = shift;
    +   my $id       = $self->param('id');
    +
    +   ##check user access
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   ##verify that a valid server ID has been used
    +   my $server_obj = $self->server_data($id);
    +   if ( !defined($server_obj) ) {
    +           return $self->not_found();
    +   }
    +
    +   my $data_obj;
    +   my $host_name = $server_obj->host_name;
    +
    +   my %condition = ( 'me.host_name' => $host_name );
    +   my $rs_server = $self->db->resultset('Server')->search( \%condition, { 
prefetch => [ 'cdn', 'profile' ] } );
    +
    +   my $server = $rs_server->next;
    +   if ($server) {
    +           my $cdn_name = $server->cdn->name;
    +
    +           $data_obj->{'profile'}->{'name'}   = $server->profile->name;
    +           $data_obj->{'profile'}->{'id'}     = $server->profile->id;
    +           $data_obj->{'other'}->{'CDN_name'} = $cdn_name;
    +
    +           %condition = (
    +                   'profile_parameters.profile' => 
$data_obj->{'profile'}->{'id'},
    +                   -or                          => [ 'name' => 'location', 
'name' => 'scope' ]
    +           );
    +           my $rs_param = $self->db->resultset('Parameter')->search( 
\%condition, { join => 'profile_parameters' } );
    +           while ( my $param = $rs_param->next ) {
    +                   if ( $param->name eq 'location' ) {
    +                           $data_obj->{'config_files'}->{ 
$param->config_file }->{'location'} = $param->value;
    +                   }
    +           }
    +   }
    +
    +   foreach my $config_file ( keys $data_obj->{'config_files'} ) {
    +           if ( !defined( 
$data_obj->{'config_files'}->{$config_file}->{'scope'} ) ) {
    +                   $data_obj->{'config_files'}->{$config_file}->{'scope'} 
= $self->get_scope($config_file);
    +           }
    +   }
    +
    +   my $file_contents = encode_json($data_obj);
    +
    +   return $self->render( text => $file_contents, format => 'txt' );
    +}
    +
    +#entry point for server scope api route.
    +sub get_server_config {
    +   my $self     = shift;
    +   my $filename = $self->param("filename");
    +   my $id       = $self->param('id');
    +   my $scope    = $self->get_scope($filename);
    +
    +   ##check user access
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   ##check the scope - is this the correct route?
    +   if ( $scope ne 'server' ) {
    +           return $self->alert( "Error - incorrect file scope for route 
used.  Please use the " . $scope . " route." );
    +   }
    +
    +   ##verify that a valid server ID has been used
    +   my $server_obj = $self->server_data($id);
    +   if ( !defined($server_obj) ) {
    +           return $self->not_found();
    +   }
    +
    +   #generate the config file using the appropriate function
    +   my $file_contents;
    +   if ( $filename eq "12M_facts" ) { $file_contents = $self->facts( 
$server_obj, $filename ); }
    +   elsif ( $filename =~ /to_ext_.*\.config/ ) { $file_contents = 
$self->to_ext_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "ip_allow.config" ) { $file_contents = 
$self->ip_allow_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "parent.config" ) { $file_contents = 
$self->parent_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "records.config" ) { $file_contents = 
$self->generic_server_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "remap.config" ) { $file_contents = 
$self->remap_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "hosting.config" ) { $file_contents = 
$self->hosting_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "cache.config" ) { $file_contents = 
$self->cache_dot_config( $server_obj, $filename ); }
    +   elsif ( $filename eq "packages" ) {
    +           $file_contents = $self->get_package_versions( $server_obj, 
$filename );
    +           $file_contents = encode_json($file_contents);
    +   }
    +   elsif ( $filename eq "chkconfig" ) {
    +           $file_contents = $self->get_chkconfig( $server_obj, $filename );
    +           $file_contents = encode_json($file_contents);
    +   }
    +   else {
    +           my $file_param = $self->db->resultset('Parameter')->search( [ 
config_file => $filename ] )->single;
    +           if ( !defined($file_param) ) {
    +                   return $self->not_found();
    +           }
    +           $file_contents = $self->take_and_bake_server( $server_obj, 
$filename );
    +   }
    +
    +   #if we get an empty file, just send back an error.
    +   if ( !defined($file_contents) ) {
    +           return $self->not_found();
    +   }
    +
    +   #return the file contents for fetch and db actions.
    +   return $self->render( text => $file_contents, format => 'txt' );
    +}
    +
    +#entry point for cdn scope api route.
    +sub get_cdn_config {
    +   my $self     = shift;
    +   my $filename = $self->param("filename");
    +   my $id       = $self->param('id');
    +   my $scope    = $self->get_scope($filename);
    +
    +   ##check user access
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   ##check the scope - is this the correct route?
    +   if ( $scope ne 'cdn' ) {
    +           return $self->alert( "Error - incorrect file scope for route 
used.  Please use the " . $scope . " route." );
    +   }
    +
    +   ##verify that a valid cdn ID has been used
    +   my $cdn_obj = $self->cdn_data($id);
    +   if ( !defined($cdn_obj) ) {
    +           return $self->not_found();
    +   }
    +
    +   #generate the config file using the appropriate function
    +   my $file_contents;
    +   if ( $filename eq "bg_fetch.config" ) { $file_contents = 
$self->bg_fetch_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename =~ /cacheurl.*\.config/ ) { $file_contents = 
$self->cacheurl_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename =~ /hdr_rw_.*\.config/ ) { $file_contents = 
$self->header_rewrite_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename =~ /regex_remap_.*\.config/ ) { $file_contents = 
$self->regex_remap_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename eq "regex_revalidate.config" ) { $file_contents = 
$self->regex_revalidate_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename =~ /set_dscp_.*\.config/ ) { $file_contents = 
$self->set_dscp_dot_config( $cdn_obj, $filename ); }
    +   elsif ( $filename eq "ssl_multicert.config" ) { $file_contents = 
$self->ssl_multicert_dot_config( $cdn_obj, $filename ); }
    +   else                                          { return 
$self->not_found(); }
    +
    +   if ( !defined($file_contents) ) {
    +           return $self->not_found();
    +   }
    +
    +   return $self->render( text => $file_contents, format => 'txt' );
    +}
    +
    +#entry point for profile scope api route.
    +sub get_profile_config {
    +   my $self     = shift;
    +   my $filename = $self->param("filename");
    +   my $id       = $self->param('id');
    +   my $scope    = $self->get_scope($filename);
    +
    +   ##check user access
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   ##check the scope - is this the correct route?
    +   if ( $scope ne 'profile' ) {
    +           return $self->alert( "Error - incorrect file scope for route 
used.  Please use the " . $scope . " route." );
    +   }
    +
    +   ##verify that a valid profile ID has been used
    +   my $profile_obj = $self->profile_data($id);
    +   if ( !defined($profile_obj) ) {
    +           return $self->not_found();
    +   }
    +
    +   #generate the config file using the appropriate function
    +   my $file_contents;
    +   if ( $filename eq "50-ats.rules" ) { $file_contents = 
$self->ats_dot_rules( $profile_obj, $filename ); }
    +   elsif ( $filename eq "astats.config" ) { $file_contents = 
$self->generic_profile_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "drop_qstring.config" ) { $file_contents = 
$self->drop_qstring_dot_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "logs_xml.config" ) { $file_contents = 
$self->logs_xml_dot_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "plugin.config" ) { $file_contents = 
$self->generic_profile_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "storage.config" ) { $file_contents = 
$self->storage_dot_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "sysctl.conf" ) { $file_contents = 
$self->generic_profile_config( $profile_obj, $filename ); }
    +   elsif ( $filename =~ /url_sig_.*\.config/ ) { $file_contents = 
$self->url_sig_dot_config( $profile_obj, $filename ); }
    +   elsif ( $filename eq "volume.config" ) { $file_contents = 
$self->volume_dot_config( $profile_obj, $filename ); }
    +   else {
    +           my $file_param = $self->db->resultset('Parameter')->search( [ 
config_file => $filename ] )->single;
    +           if ( !defined($file_param) ) {
    +                   return $self->not_found();
    +           }
    +           $file_contents = $self->take_and_bake_profile( $profile_obj, 
$filename );
    +   }
    +
    +   if ( !defined($file_contents) ) {
    +           return $self->not_found();
    +   }
    +
    +   return $self->render( text => $file_contents, format => 'txt' );
    +}
    +
    +my $separator ||= {
    +   "records.config"  => " ",
    +   "plugin.config"   => " ",
    +   "sysctl.conf"     => " = ",
    +   "url_sig_.config" => " = ",
    +   "astats.config"   => "=",
    +};
    +
    +#identify the correct scope for each filename.  if not found, returns 
server scope as any
    +#undefined parameter based configs are designed with server scope using 
the take-and-bake sub.
    +sub get_scope {
    +   my $self  = shift;
    +   my $fname = shift;
    +   my $scope;
    +
    +   if    ( $fname eq "12M_facts" )               { $scope = 'server' }
    +   elsif ( $fname eq "ip_allow.config" )         { $scope = 'server' }
    +   elsif ( $fname eq "parent.config" )           { $scope = 'server' }
    +   elsif ( $fname eq "records.config" )          { $scope = 'server' }
    +   elsif ( $fname eq "remap.config" )            { $scope = 'server' }
    +   elsif ( $fname =~ /to_ext_.*\.config/ )       { $scope = 'server' }
    +   elsif ( $fname eq "hosting.config" )          { $scope = 'server' }
    +   elsif ( $fname eq "cache.config" )            { $scope = 'server' }
    +   elsif ( $fname eq "packages" )                { $scope = 'server' }
    +   elsif ( $fname eq "chkconfig" )               { $scope = 'server' }
    +   elsif ( $fname eq "50-ats.rules" )            { $scope = 'profile' }
    +   elsif ( $fname eq "astats.config" )           { $scope = 'profile' }
    +   elsif ( $fname eq "drop_qstring.config" )     { $scope = 'profile' }
    +   elsif ( $fname eq "logs_xml.config" )         { $scope = 'profile' }
    +   elsif ( $fname eq "plugin.config" )           { $scope = 'profile' }
    +   elsif ( $fname eq "storage.config" )          { $scope = 'profile' }
    +   elsif ( $fname eq "sysctl.conf" )             { $scope = 'profile' }
    +   elsif ( $fname =~ /url_sig_.*\.config/ )      { $scope = 'profile' }
    +   elsif ( $fname eq "volume.config" )           { $scope = 'profile' }
    +   elsif ( $fname eq "bg_fetch.config" )         { $scope = 'cdn' }
    +   elsif ( $fname =~ /cacheurl.*\.config/ )      { $scope = 'cdn' }
    +   elsif ( $fname =~ /hdr_rw_.*\.config/ )       { $scope = 'cdn' }
    +   elsif ( $fname =~ /regex_remap_.*\.config/ )  { $scope = 'cdn' }
    +   elsif ( $fname eq "regex_revalidate.config" ) { $scope = 'cdn' }
    +   elsif ( $fname =~ /set_dscp_.*\.config/ )     { $scope = 'cdn' }
    +   elsif ( $fname eq "ssl_multicert.config" )    { $scope = 'cdn' }
    +   else {
    +           $scope = $self->db->resultset('Parameter')->search( { -and => [ 
name => 'scope', config_file => $fname ] } )->get_column('value')->first();
    +           if ( !defined($scope) ) {
    +                   $self->app->log->error("Filename not found.  Setting 
Server scope.");
    +                   $scope = 'server';
    +           }
    +   }
    +
    +   return $scope;
    +}
    +
    +#takes the server name or ID and turns it into a server object that can 
reference either, making either work for the request.
    +sub server_data {
    +   my $self = shift;
    +   my $id   = shift;
    +
    +   my $server_obj;
    +
    +   #if an ID is passed, look up by ID.  Otherwise, look up by hostname.
    +   if ( $id =~ /^\d+$/ ) {
    +           $server_obj = $self->db->resultset('Server')->search( { id => 
$id } )->single;
    +   }
    +   else {
    +           $server_obj = $self->db->resultset('Server')->search( { 
host_name => $id } )->single;
    +   }
    +
    +   return $server_obj;
    +}
    +
    +#takes the profile name or ID and turns it into a server object that can 
reference either, making either work for the request.
    +sub profile_data {
    +   my $self = shift;
    +   my $id   = shift;
    +
    +   #if an ID is passed, look up by ID.  Otherwise, look up by profile name.
    +   my $profile_obj;
    +   if ( $id =~ /^\d+$/ ) {
    +           $profile_obj = $self->db->resultset('Profile')->search( { id => 
$id } )->single;
    +   }
    +   else {
    +           $profile_obj = $self->db->resultset('Profile')->search( { name 
=> $id } )->single;
    +   }
    +
    +   return $profile_obj;
    +}
    +
    +#takes the server name or ID and turns it into a server object that can 
reference either, making either work for the request.
    +sub cdn_data {
    +   my $self = shift;
    +   my $id   = shift;
    +   my $cdn_obj;
    +
    +   if ( $id =~ /^\d+$/ ) {
    +           $cdn_obj = $self->db->resultset('Cdn')->search( { id => $id } 
)->single;
    +   }
    +   else {
    +           $cdn_obj = $self->db->resultset('Cdn')->search( { name => $id } 
)->single;
    +   }
    +
    +   return $cdn_obj;
    +}
    +
    +#generates the comment at the top of config files.
    +sub header_comment {
    +   my $self = shift;
    +   my $name = shift;
    +
    +   my $text = "# DO NOT EDIT - Generated for " . $name . " by " . 
&name_version_string($self) . " on " . `date`;
    +   return $text;
    +}
    +
    +#retrieves parameter data for a specific server by searching by the 
server's assigned profile.
    +sub param_data {
    +   my $self       = shift;
    +   my $server_obj = shift;
    +   my $filename   = shift;
    +   my $data;
    +
    +   my $rs = $self->db->resultset('ProfileParameter')->search( { -and => [ 
profile => $server_obj->profile->id, 'parameter.config_file' => $filename ] },
    +           { prefetch => [ { parameter => undef }, { profile => undef } ] 
} );
    +   while ( my $row = $rs->next ) {
    +           if ( $row->parameter->name eq "location" ) {
    +                   next;
    +           }
    +           if ( $row->parameter->name eq "scope" ) {
    --- End diff --
    
    i thought you got rid of "scope" parameters?


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