File Coverage

blib/lib/Armadito/Agent/Task/Getjobs.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 8 0.0
condition 0 3 0.0
subroutine 6 11 54.5
pod 2 2 100.0
total 26 77 33.7


line stmt bran cond sub pod time code
1             package Armadito::Agent::Task::Getjobs;
2              
3 1     1   5490803 use strict;
  1         2  
  1         45  
4 1     1   5 use warnings;
  1         5  
  1         61  
5 1     1   3 use base 'Armadito::Agent::Task';
  1         33  
  1         402  
6              
7 1     1   5 use Armadito::Agent::Storage;
  1         1  
  1         8  
8 1     1   18 use Data::Dumper;
  1         1  
  1         40  
9 1     1   5 use JSON;
  1         1  
  1         19  
10              
11             sub new {
12 0     0 1   my ( $class, %params ) = @_;
13              
14 0           my $self = $class->SUPER::new(%params);
15              
16 0 0         if ( $params{debug} ) {
17 0           $self->{debug} = 1;
18             }
19              
20             my $task = {
21             name => "Getjobs",
22             antivirus => $self->{agent}->{antivirus}->getJobj()
23 0           };
24              
25 0           $self->{jobj}->{task} = $task;
26              
27 0           return $self;
28             }
29              
30             sub _storeJobs {
31 0     0     my ( $self, $jobs ) = @_;
32              
33             # We merge stored jobs with new ones
34 0           my $data = $self->{agent}->{armadito_storage}->restore( name => 'Armadito-Agent-Jobs' );
35 0 0         if ( defined( $data->{jobs} ) ) {
36 0           foreach ( @{ $data->{jobs} } ) {
  0            
37 0           push( @$jobs, $_ );
38             }
39             }
40              
41             $self->{agent}->{armadito_storage}->save(
42 0           name => 'Armadito-Agent-Jobs',
43             data => {
44             jobs => $jobs
45             }
46             );
47             }
48              
49             sub _handleResponse {
50 0     0     my ( $self, $response ) = @_;
51              
52 0           $self->{logger}->info( "Successful Response : " . $response->content() );
53 0           my $obj = from_json( $response->content(), { utf8 => 1 } );
54              
55 0 0 0       if ( defined( $obj->{jobs} ) && ref( $obj->{jobs} ) eq "ARRAY" ) {
56 0           $self->_storeJobs( $obj->{jobs} );
57             }
58              
59 0           $self->{logger}->info( "all Jobs : " . Dumper($obj) );
60 0           return $self;
61             }
62              
63             sub _handleError {
64 0     0     my ( $self, $response ) = @_;
65              
66 0           $self->{logger}->info( "Error Response : " . $response->content() );
67 0           my $obj = from_json( $response->content(), { utf8 => 1 } );
68 0           $self->{logger}->error( Dumper($obj) );
69              
70 0           return $self;
71             }
72              
73             sub run {
74 0     0 1   my ( $self, %params ) = @_;
75              
76 0           $self = $self->SUPER::run(%params);
77              
78             my $response = $self->{glpi_client}->sendRequest(
79             "url" => $self->{agent}->{config}->{server}[0] . "/api/jobs",
80             args => {
81             antivirus => $self->{jobj}->{task}->{antivirus}->{name},
82             agent_id => $self->{jobj}->{agent_id}
83             },
84 0           method => "GET"
85             );
86              
87 0 0         if ( $response->is_success() ) {
88 0           $self->_handleResponse($response);
89 0           $self->{logger}->info("Getjobs successful...");
90             }
91             else {
92 0           $self->_handleError($response);
93 0           $self->{logger}->info("Getjobs failed...");
94             }
95              
96 0           return $self;
97             }
98              
99             1;
100              
101             __END__
102              
103             =head1 NAME
104              
105             Armadito::Agent::Task::Getjobs - Getjobs Task base class.
106              
107             =head1 DESCRIPTION
108              
109             This task inherits from L<Armadito::Agent::Task>. Send a pull GET request to get jobs agent has to do according to Armadito Plugin for GLPI.
110              
111             =head1 FUNCTIONS
112              
113             =head2 run ( $self, %params )
114              
115             Run the task.
116              
117             =head2 new ( $self, %params )
118              
119             Instanciate Task.
120              
121              
122