Improve code (#2458)

This commit is contained in:
Christophe Maudoux 2021-02-18 23:35:08 +01:00
parent 122512f01a
commit 057cfb29e8
2 changed files with 16 additions and 21 deletions

View File

@ -66,7 +66,6 @@ sub display {
DOWNLOAD => $self->conf->{checkDevOpsDownload}, DOWNLOAD => $self->conf->{checkDevOpsDownload},
MSG => 'checkDevOps', MSG => 'checkDevOps',
ALERTE => 'alert-info', ALERTE => 'alert-info',
FILE => '',
TOKEN => ( TOKEN => (
$self->ottRule->( $req, {} ) $self->ottRule->( $req, {} )
? $self->ott->createToken() ? $self->ott->createToken()
@ -82,7 +81,8 @@ sub display {
sub run { sub run {
my ( $self, $req ) = @_; my ( $self, $req ) = @_;
my ( $headers, $rules ) = ( [], [] ); my ( $headers, $rules ) = ( [], [] );
my ( $msg, $json, $alert, $url ); my ( $msg, $json, $url );
my $alert = 'alert-danger';
# Check token # Check token
if ( $self->ottRule->( $req, {} ) ) { if ( $self->ottRule->( $req, {} ) ) {
@ -111,7 +111,6 @@ sub run {
DOWNLOAD => $self->conf->{checkDevOpsDownload}, DOWNLOAD => $self->conf->{checkDevOpsDownload},
MSG => "PE$msg", MSG => "PE$msg",
ALERTE => 'alert-warning', ALERTE => 'alert-warning',
FILE => '',
TOKEN => $token, TOKEN => $token,
}; };
return $self->p->sendJSONresponse( $req, $params ) return $self->p->sendJSONresponse( $req, $params )
@ -132,22 +131,20 @@ sub run {
my ($proto) = $url =~ m#^(https?://).*#i; my ($proto) = $url =~ m#^(https?://).*#i;
$proto ||= 'http://'; $proto ||= 'http://';
$url = "$proto$vhost/rules.json"; $url = "$proto$vhost/rules.json";
my $response = my $resp = $self->ua->get( $url, 'Accept' => 'application/json' );
$self->ua->get( $url, 'Accept' => 'application/json' );
$self->logger->debug( "Code/Message from $url: " $self->logger->debug( "Code/Message from $url: "
. $response->{_rc} . '/' . $resp->code . '/'
. $response->{_msg} ); . $resp->message );
$self->logger->debug( my $content = $resp->decoded_content;
"Content from $url: " . $response->{_content} ) if $response->{_content}; $self->logger->debug("Content received from $url: $content") if $content;
if ( $response->{_rc} == 200 ) { if ( $resp->is_success ) {
$json = eval { from_json( $response->{_content} ) }; $json = eval { from_json($content, { allow_nonref => 1 }) };
if ($@) { if ($@) {
# Prepare form params # Prepare form params
undef $json;
$msg = 'PE' . PE_BAD_DEVOPS_FILE; $msg = 'PE' . PE_BAD_DEVOPS_FILE;
$alert = 'alert-danger';
$json = '';
$self->userLogger->error( $self->userLogger->error(
"CheckDevOps: bad 'rules.json' file retrieved from $url ($@)" "CheckDevOps: bad 'rules.json' file retrieved from $url ($@)"
); );
@ -157,8 +154,6 @@ sub run {
# Prepare form params # Prepare form params
$msg = 'PE' . PE_FILENOTFOUND; $msg = 'PE' . PE_FILENOTFOUND;
$alert = 'alert-danger';
$json = '';
$self->userLogger->error( $self->userLogger->error(
"CheckDevOps: Unable to download 'rules.json' file from $url" "CheckDevOps: Unable to download 'rules.json' file from $url"
); );
@ -168,21 +163,21 @@ sub run {
# Prepare form params # Prepare form params
$msg = 'PE' . PE_BADURL; $msg = 'PE' . PE_BADURL;
$alert = 'alert-danger';
$json = '';
$self->userLogger->error('CheckDevOps: bad provided URL'); $self->userLogger->error('CheckDevOps: bad provided URL');
} }
} }
unless ( $json || $msg ) { unless ( $json || $msg ) {
$json = eval { from_json( $req->param('checkDevOpsFile') ) }; $json = eval {
from_json( $req->param('checkDevOpsFile'), { allow_nonref => 1 } );
};
if ($@) { if ($@) {
# Prepare form params # Prepare form params
undef $json;
$msg = 'PE' . PE_BAD_DEVOPS_FILE; $msg = 'PE' . PE_BAD_DEVOPS_FILE;
$alert = 'alert-danger'; $alert = 'alert-danger';
$json = '';
$self->userLogger->error( $self->userLogger->error(
"CheckDevOps: bad 'rules.json' file provided ($@)"); "CheckDevOps: bad provided 'rules.json' file ($@)");
} }
} }

View File

@ -25,7 +25,7 @@ my $file = '{
}'; }';
my $client = LLNG::Manager::Test->new( { my $client = LLNG::Manager::Test->new( {
ini => { ini => {
logLevel => 'debug', logLevel => 'error',
authentication => 'Demo', authentication => 'Demo',
userDB => 'Same', userDB => 'Same',
requireToken => 0, requireToken => 0,