-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathCommon.pm
More file actions
124 lines (96 loc) · 3.24 KB
/
Copy pathCommon.pm
File metadata and controls
124 lines (96 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package GLPI::Agent::Task::Collect::Common;
use strict;
use warnings;
use constant function => "";
use constant disabled => 0;
use constant json_validation => {};
use constant OPTIONAL => 0;
use constant MANDATORY => 1;
use constant OPTIONAL_EXCLUSIVE => 2;
use GLPI::Agent::Tools;
sub new {
my ($class, %params) = @_;
my $self = $params{job} // {};
$self->{logger} = $params{logger};
$self->{plugin} = glpiVersion($params{plugin} // "1.0.0");
bless $self, $class;
return $self;
}
sub pluginSupport {
my ($self, $version) = @_;
return (glpiVersion($version) <= $self->{plugin}) ? 1 : 0;
}
sub _validateSpec {
my ($self, $base, $key, $spec) = @_;
if (ref($spec) eq 'HASH') {
if (!exists($base->{$key})) {
$self->{logger}->debug("$key mandatory values are missing in job");
return 0;
}
$self->{logger}->debug2("$key mandatory values are present in job");
foreach my $attribute (keys(%{$spec})) {
return 0 unless $self->_validateSpec($base->{$key}, $attribute, $spec->{$attribute});
}
return 1;
}
if ($spec == MANDATORY) {
if (!exists($base->{$key})) {
$self->{logger}->debug("$key mandatory value is missing in job");
return 0;
}
$self->{logger}->debug2("$key mandatory value is present in job");
return 1;
}
if ($spec == OPTIONAL && exists($base->{$key})) {
$self->{logger}->debug2("$key optional value is present in job");
}
1;
}
sub validateAnswer {
my ($self, %params) = @_;
my $modules = $params{modules};
my $json_validation = $params{json_validation};
unless (ref($modules) && ref($json_validation)) {
$self->{logger}->debug("Validation failure");
return 0;
}
my $answer = $params{answer};
unless (defined($answer)) {
$self->{logger}->debug("Bad JSON: No answer from server.");
return 0;
}
if (ref($answer) ne 'HASH') {
$self->{logger}->debug("Bad JSON: Bad answer from server. Not a hash reference.");
return 0;
}
if (!defined($answer->{jobs}) || ref($answer->{jobs}) ne 'ARRAY') {
$self->{logger}->debug("Bad JSON: Missing jobs");
return 0;
}
foreach my $job (@{$answer->{jobs}}) {
foreach (qw/uuid function/) {
if (!defined($job->{$_})) {
$self->{logger}->debug("Bad JSON: Missing key '$_' in job");
return 0;
}
}
my $function = $job->{function};
unless (exists($modules->{$function})) {
$self->{logger}->debug("Bad JSON: not supported 'function' key value in job");
return 0;
}
my $validation = $json_validation->{$function};
unless (ref($validation)) {
$self->{logger}->debug("Bad JSON: Can't validate job");
return 0;
}
foreach my $attribute (keys(%{$validation})) {
unless ($self->_validateSpec($job, $attribute, $validation->{$attribute})) {
$self->{logger}->debug("Bad JSON: '$function' job JSON format is not valid");
return 0;
}
}
}
return 1;
}
1;