Commit df44cc6c authored by Yadd's avatar Yadd
Browse files

New REST method: diff between 2 confs (#575)

parent 39524dda
......@@ -14,6 +14,7 @@ lib/Lemonldap/NG/Manager/Build/Tree.pm
lib/Lemonldap/NG/Manager/Cli.pm
lib/Lemonldap/NG/Manager/Cli/Lib.pm
lib/Lemonldap/NG/Manager/Conf.pm
lib/Lemonldap/NG/Manager/Conf/Diff.pm
lib/Lemonldap/NG/Manager/Conf/Parser.pm
lib/Lemonldap/NG/Manager/Conf/Tests.pm
lib/Lemonldap/NG/Manager/Conf/Zero.pm
......
......@@ -54,6 +54,9 @@ sub addRoutes {
['POST']
)
# Difference between confs
->addRoute( diff => { ':conf1' => { ':conf2' => 'diff' } } )
# Url loader
->addRoute( 'prx', undef, ['POST'] );
}
......@@ -1017,6 +1020,35 @@ sub applyConf {
return $status;
}
sub diff {
my ( $self, $req, @path ) = @_;
return $self->sendError( $req, 'to many arguments in path info', 400 )
if (@path);
my @cfgNum = ( $req->params('conf1'), $req->params('conf2') );
my @conf;
# Load the 2 configurations
for ( my $i = 0 ; $i < 2 ; $i++ ) {
if ( %{ $self->currentConf }
and $cfgNum[$i] == $self->currentConf->{cfgNum} )
{
$conf[$i] = $self->currentConf;
}
else {
$conf[$i] = $self->confAcc->getConf(
{ cfgNum => $cfgNum[$i], raw => 1, noCache => 1 } );
return $self->sendError(
$req,
"Configuration $cfgNum[$i] not available $Lemonldap::NG::Common::Conf::msg",
400
) unless ( $conf[$i] );
}
}
require Lemonldap::NG::Manager::Conf::Diff;
return $self->sendJSONresponse( $req,
[ Lemonldap::NG::Manager::Conf::Diff->diff( $conf[0], $conf[1] ) ] );
}
1;
__END__
......
package Lemonldap::NG::Manager::Conf::Diff;
use strict;
use Mouse;
use Lemonldap::NG::Manager::Conf::Parser;
*defaultValue = \&Lemonldap::NG::Manager::Conf::Parser::defaultValue;
sub diff {
my ( $self, @conf ) = @_;
my @res;
my @keys = ( [ keys %{ $conf[0] } ], [ keys %{ $conf[1] } ] );
while ( my $key = shift @{ $keys[0] } ) {
# TODO
next if ( $key eq 'applicationList' );
if ( ref $conf[0]->{$key} eq 'HASH' ) {
if ( ref $conf[1]->{$key} ) {
my @tmp = $self->diff( $conf[0]->{$key}, $conf[1]->{$key}, 1 );
for ( my $i = 0 ; $i < @tmp ; $i++ ) {
$res[$i]->{$key} = $tmp[$i] if ( $tmp[$i] );
}
}
else {
$res[0]->{$key} = $conf[0]->{$key};
}
}
elsif ( ref $conf[0]->{$key} eq 'ARRAY' ) {
die 'TODO';
}
elsif ( exists $conf[1]->{$key} ) {
if ( $conf[0]->{$key} ne $conf[1]->{$key} ) {
$res[0]->{$key} = $conf[0]->{$key};
$res[1]->{$key} = $conf[1]->{$key};
}
}
elsif ( $conf[2] or $conf[0]->{$key} ne $self->defaultValue($key) ) {
$res[0]->{$key} = $conf[0]->{$key};
}
$keys[1] = [ grep { $_ ne $key } @{ $keys[1] } ];
}
while ( my $key = shift @{ $keys[1] } ) {
# TODO
next if ( $key eq 'applicationList' );
if (
(
not ref( $conf[1]->{$key} )
and
( $conf[2] or $conf[1]->{$key} ne $self->defaultValue($key) )
)
or ( ref( $conf[1]->{$key} ) eq 'HASH' and %{ $conf[1]->{$key} } )
)
{
$res[1]->{$key} = $conf[1]->{$key};
}
}
return @res;
}
1;
......@@ -63,6 +63,11 @@ if ($bug) {
count(7);
# TODO: check result of this
ok(&client->jsonResponse('/diff/1/2'), 'Diff called');
count(1);
unlink $confFiles->[1];
done_testing( count() );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment