line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
2
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
91
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# packge the DummyLogger together with the RPCClient package |
5
|
|
|
|
|
|
|
package Net::OpenNebula::DummyLogger; ## no critic |
6
|
|
|
|
|
|
|
$Net::OpenNebula::DummyLogger::VERSION = '0.309.0'; |
7
|
|
|
|
|
|
|
sub new { |
8
|
0
|
|
|
0
|
|
|
my $that = shift; |
9
|
0
|
|
0
|
|
|
|
my $proto = ref($that) || $that; |
10
|
0
|
|
|
|
|
|
my $self = { @_ }; |
11
|
|
|
|
|
|
|
|
12
|
0
|
|
|
|
|
|
bless($self, $proto); |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
return $self; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Mock basic methods of Log4Perl getLogger instance |
18
|
1
|
|
|
1
|
|
8
|
no strict 'refs'; ## no critic |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
66
|
|
19
|
|
|
|
|
|
|
foreach my $i (qw(error warn info verbose debug)) { |
20
|
|
|
|
|
|
|
*{$i} = sub {} |
21
|
|
|
|
|
|
|
} |
22
|
1
|
|
|
1
|
|
5
|
use strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
40
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
package Net::OpenNebula::RPCClient; |
26
|
|
|
|
|
|
|
$Net::OpenNebula::RPCClient::VERSION = '0.309.0'; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
1
|
|
874
|
use RPC::XML; |
|
1
|
|
|
|
|
599571
|
|
|
1
|
|
|
|
|
98
|
|
29
|
1
|
|
|
1
|
|
1118
|
use RPC::XML::Client; |
|
1
|
|
|
|
|
165072
|
|
|
1
|
|
|
|
|
53
|
|
30
|
1
|
|
|
1
|
|
1088
|
use Data::Dumper; |
|
1
|
|
|
|
|
8356
|
|
|
1
|
|
|
|
|
123
|
|
31
|
1
|
|
|
1
|
|
516
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use version; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
if(! defined($ENV{XML_SIMPLE_PREFERRED_PARSER})) { |
36
|
|
|
|
|
|
|
$ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser'; |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $has_libxml; |
40
|
|
|
|
|
|
|
eval "use XML::LibXML::Simple qw(XMLin);"; ## no critic (BuiltinFunctions::ProhibitStringyEval) |
41
|
|
|
|
|
|
|
if ($@) { |
42
|
|
|
|
|
|
|
use XML::Simple qw(XMLin XMLout); |
43
|
|
|
|
|
|
|
$has_libxml = 0; |
44
|
|
|
|
|
|
|
} else { |
45
|
|
|
|
|
|
|
use XML::Simple qw(XMLout); |
46
|
|
|
|
|
|
|
$has_libxml = 1; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use RPC::XML::ParserFactory (class => $has_libxml ? 'XML::LibXML' : 'XML::Parser'); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Caching |
52
|
|
|
|
|
|
|
# data_cache |
53
|
|
|
|
|
|
|
my $_cache = {}; |
54
|
|
|
|
|
|
|
my $_cache_methods = {}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# options |
57
|
|
|
|
|
|
|
# user: user to connect |
58
|
|
|
|
|
|
|
# password: password for user |
59
|
|
|
|
|
|
|
# url: the RPC url to use |
60
|
|
|
|
|
|
|
# log: optional log4perl-like instance |
61
|
|
|
|
|
|
|
# fail_on_rpc_fail: die on RPC error or not |
62
|
|
|
|
|
|
|
sub new { |
63
|
|
|
|
|
|
|
my $that = shift; |
64
|
|
|
|
|
|
|
my $proto = ref($that) || $that; |
65
|
|
|
|
|
|
|
my $self = { @_ }; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if (! exists($self->{log})) { |
68
|
|
|
|
|
|
|
$self->{log} = Net::OpenNebula::DummyLogger->new(); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# legacy behaviour |
72
|
|
|
|
|
|
|
if (! exists($self->{fail_on_rpc_fail})) { |
73
|
|
|
|
|
|
|
$self->{fail_on_rpc_fail} = 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
bless($self, $proto); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$self->{log}->debug(2, "Initialised with user $self->{user} and url $self->{url}"); |
79
|
|
|
|
|
|
|
$self->{log}->debug(2, ($has_libxml ? "U" : "Not u")."sing XML::LibXML(::Simple)"); |
80
|
|
|
|
|
|
|
$self->{log}->debug(2, "Using preferred XML::Simple parser $ENV{XML_SIMPLE_PREFERRED_PARSER}."); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Cache and test rpc |
83
|
|
|
|
|
|
|
$self->version(); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
return $self; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Enable the caching of all methods calls (cache is per method/args combo) |
89
|
|
|
|
|
|
|
sub add_cache_method { |
90
|
|
|
|
|
|
|
my ($self, $method) = @_; |
91
|
|
|
|
|
|
|
$_cache_methods->{$method} = 1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Remove the caching method and cache |
96
|
|
|
|
|
|
|
sub remove_cache_method { |
97
|
|
|
|
|
|
|
my ($self, $method) = @_; |
98
|
|
|
|
|
|
|
$_cache_methods->{$method} = 0; |
99
|
|
|
|
|
|
|
$_cache->{$method} = {}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _rpc_args_to_txt { |
104
|
|
|
|
|
|
|
my ($self, @args) = @_; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my @txt; |
107
|
|
|
|
|
|
|
foreach my $arg (@args) { |
108
|
|
|
|
|
|
|
push(@txt, join(", ", @$arg)); |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
my $args_txt = join("], [", @txt); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
return "[$args_txt]"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _rpc { |
116
|
|
|
|
|
|
|
my ($self, $meth, @params) = @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $req_txt = "method $meth args ".$self->_rpc_args_to_txt(@params); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$self->debug(4, "_rpc called with $req_txt"); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
if ($_cache_methods->{$meth}) { |
123
|
|
|
|
|
|
|
if ($_cache->{$meth} && exists($_cache->{$meth}->{$req_txt})) { |
124
|
|
|
|
|
|
|
$self->debug(1, "Returning cached data for $meth / $req_txt"); |
125
|
|
|
|
|
|
|
return $_cache->{$meth}->{$req_txt}; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $cli = $self->{__cli}; |
130
|
|
|
|
|
|
|
if (! $cli) { |
131
|
|
|
|
|
|
|
$self->{__cli} = RPC::XML::Client->new($self->{url}); |
132
|
|
|
|
|
|
|
$cli = $self->{__cli}; |
133
|
|
|
|
|
|
|
}; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my @params_o = (RPC::XML::string->new($self->{user} . ":" . $self->{password})); |
136
|
|
|
|
|
|
|
for my $p (@params) { |
137
|
|
|
|
|
|
|
my $klass = "RPC::XML::" . $p->[0]; |
138
|
|
|
|
|
|
|
push(@params_o, $klass->new($p->[1])); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $req = RPC::XML::request->new($meth, @params_o); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $reqstring = $req->as_string(); |
144
|
|
|
|
|
|
|
my $password = XMLout($self->{password}, rootname => "x"); |
145
|
|
|
|
|
|
|
if ($password =~ m!^\s*<x>(.*)</x>\s*$!) { |
146
|
|
|
|
|
|
|
$password = quotemeta $1; |
147
|
|
|
|
|
|
|
$reqstring =~ s/$password/PASSWORD/g; |
148
|
|
|
|
|
|
|
$self->debug(5, "_rpc RPC request $reqstring"); |
149
|
|
|
|
|
|
|
} else { |
150
|
|
|
|
|
|
|
$self->debug(5, "_rpc RPC request not shown, failed to convert and replace password"); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
my $resp = $cli->send_request($req); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
if(!ref($resp)) { |
156
|
|
|
|
|
|
|
$self->error("_rpc send_request failed with message: $resp"); |
157
|
|
|
|
|
|
|
return; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $ret = $resp->value; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
if(ref($ret) ne "ARRAY") { |
163
|
|
|
|
|
|
|
$self->error("_rpc failed to make request faultCode $ret->{faultCode} faultString $ret->{faultString} $req_txt"); |
164
|
|
|
|
|
|
|
return; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
elsif($ret->[0] == 1) { |
168
|
|
|
|
|
|
|
$self->debug(5, "_rpc RPC answer $ret->[1]"); |
169
|
|
|
|
|
|
|
if($ret->[1] =~ m/^(\d|\.)+$/) { |
170
|
|
|
|
|
|
|
my $parsed = $ret->[1]; |
171
|
|
|
|
|
|
|
if ($_cache_methods->{$meth}) { |
172
|
|
|
|
|
|
|
$_cache->{$meth}->{$req_txt} = $parsed; |
173
|
|
|
|
|
|
|
$self->debug(5, "Result for $meth / $req_txt cached"); |
174
|
|
|
|
|
|
|
}; |
175
|
|
|
|
|
|
|
return $parsed; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
else { |
178
|
|
|
|
|
|
|
my $opts = { |
179
|
|
|
|
|
|
|
ForceArray => $has_libxml ? ['ID', 'NAME', 'STATE', qr{.}] : 1, |
180
|
|
|
|
|
|
|
KeyAttr => [], |
181
|
|
|
|
|
|
|
}; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $parsed = XMLin($ret->[1], %$opts); |
184
|
|
|
|
|
|
|
if ($_cache_methods->{$meth}) { |
185
|
|
|
|
|
|
|
$_cache->{$meth}->{$req_txt} = $parsed; |
186
|
|
|
|
|
|
|
$self->debug(5, "Result for $meth / $req_txt cached"); |
187
|
|
|
|
|
|
|
}; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
return $parsed; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
else { |
194
|
|
|
|
|
|
|
$self->error("_rpc Error sending request $req_txt: $ret->[1] (code $ret->[2])"); |
195
|
|
|
|
|
|
|
if( $self->{fail_on_rpc_fail}) { |
196
|
|
|
|
|
|
|
die("error sending request."); |
197
|
|
|
|
|
|
|
} else { |
198
|
|
|
|
|
|
|
return; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub version { |
206
|
|
|
|
|
|
|
my ($self) = @_; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# cached value |
209
|
|
|
|
|
|
|
if(exists($self->{_version})) { |
210
|
|
|
|
|
|
|
return $self->{_version}; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
my $version = $self->_rpc("one.system.version"); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
if(defined($version)) { |
215
|
|
|
|
|
|
|
$self->verbose("Version $version found"); |
216
|
|
|
|
|
|
|
$self->{_version} = version->new($version); |
217
|
|
|
|
|
|
|
return $self->{_version}; |
218
|
|
|
|
|
|
|
} else { |
219
|
|
|
|
|
|
|
$self->error("Failed to retrieve version"); |
220
|
|
|
|
|
|
|
return; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# add logging shortcuts |
226
|
|
|
|
|
|
|
no strict 'refs'; ## no critic |
227
|
|
|
|
|
|
|
# The Log4Perl methods |
228
|
|
|
|
|
|
|
foreach my $i (qw(error warn info debug)) { |
229
|
|
|
|
|
|
|
*{$i} = sub { |
230
|
|
|
|
|
|
|
my ($self, @args) = @_; |
231
|
|
|
|
|
|
|
return $self->{log}->$i(@args); |
232
|
|
|
|
|
|
|
}; |
233
|
|
|
|
|
|
|
}; |
234
|
|
|
|
|
|
|
# verbose fallback for Log4Perl |
235
|
|
|
|
|
|
|
*{verbose} = sub { |
236
|
|
|
|
|
|
|
my ($self, @args) = @_; |
237
|
|
|
|
|
|
|
my $verbose = $self->{log}->can('verbose') ? 'verbose' : 'debug'; |
238
|
|
|
|
|
|
|
return $self->{log}->$verbose(@args); |
239
|
|
|
|
|
|
|
}; |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
use strict 'refs'; |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |