JBoss.pm

filter: Statementstmt 34.4% 31 / 90 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.8% 7 / 19 Podpod 100% 5 / 5 totaltotal 29.8% 43 / 144 CC 16 SCAR 24.7
CC
16
Cov
26%
get_api_response
42.0
__ANON__
12.0
base_url
12.0
CRAP 11.9
« JBoss/Management.pm ↑ Summary
1 package Net::JBoss;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::JBoss - Bindings for JBoss Management API
13  
14 =head1 VERSION
15  
16 Version 0.04
17  
18 =cut
19  
20 1 our $VERSION = '0.04';
21  
22  
23 =head1 SYNOPSIS
24  
25 use Net::JBoss::Management;
26  
27 my %con = (
28 username => 'admin',
29 password => 'password',
30 server => 'jboss1.example.com',
31 port => 9443, #optional, default is 9990
32 ssl => 'on', #optional, default is 'off'
33 ssl_verify => 'yes', #optional, default is 'no'
34 realm => 'ManagementRealmHTTPS' #optional, default is 'ManagementRealm'
35 );
36  
37 my $jboss = Net::JBoss::Management->new(%con);
38
39 my $state = $jboss->get_state();
40 my $jvm_usage = $jboss->get_jvm_usage();
41 my $runtime_stats = $jboss->get_runtime_stats();
42 my $deploy_info = $jboss->get_deployment_info();
43 my $app_runtime_stats = $jboss->get_app_runtime_stats('hawtio.war');
44 my $runtime_details = $jboss->get_runtime_details();
45 my $app_status = $jboss->get_app_status('hawtio.war');
46 my $active_session = $jboss->get_active_sessions('hawtio.war');
47 my $server_env = $jboss->get_server_env();
48 my $datasources = $jboss->get_datasources();
49 my $test = $jboss->test_con_pool('ExampleDS');
50 my $pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool');
51 my $enable_pool_stats = $jboss->set_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'true');
52 my $disable_pool_stats = $jboss->set_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'false');
53 my $min_pool_size = $jboss->set_ds_pool_size('min', 'java:jboss/datasources/jboss_Pool', 20);
54 my $max_pool_size = $jboss->set_ds_pool_size('max', 'java:jboss/datasources/jboss_Pool', 50);
55 my $jndi = $jboss->get_jndi();
56 my $loglevel = $jboss->get_log_level('CONSOLE');
57 my $loglevel = $jboss->set_log_level('CONSOLE', 'ERROR');
58 my $reload = $jboss->reload();
59 my $shutdown = $jboss->shutdown();
60 my $restart = $jboss->restart();
61  
62 =head1 Attributes
63  
64 notes :
65 ro = read only, can be specified during initialization
66 rw = read write, user can set this attribute
67 rwp = read write protected, for internal class
68  
69 username = (ro, required) store management user username
70 password = (ro, required) store management user password
71 server = (ro, required) store managemenet address, ip address / hostname only
72 port = (ro) store Ovirt Manager's port (must be number)
73 log_severity = (ro) store log severity level, valid value ERROR|OFF|FATAL|INFO|DEBUG|TRACE|ALL|WARN
74 (default is INFO)
75 realm = (ro) store realm, default to 'ManagementRealm'
76 resource_url = (rwp) store resource url for each method
77 url = (rwp) store final url to be requested
78 log = (rwp) store log from log4perl
79 http_post = (rwp) if true, use http post method instead of get
80 post_json = (rwp) set json content to be post
81 ssl = (ro) if 'on', use https (default is 'off')
82 ssl_verify = (ro) disable host verification, yes/no (default is 'no')
83 is_xa = (ro) define if it's xa datasource, yes/no (default is 'no')
84  
85 =cut
86  
87 1 has [qw/url log json_data resource_url http_post post_json /] => ( is => 'rwp' );
88 1 has [qw/username password/] => ( is => 'ro', required => 1 );
89 has [qw/server/] => ( is => 'ro',
90 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
91 0 ▶ croak "server can't contain http protocol, use ip / hostname only"
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /http/i 0 0 0
92 if $_[0] =~ /http/i;
93 },
94 1 required => 1 )
95 ;
96  
97 has 'port' => ( is => 'ro', default => 9990,
98 isa =>
99 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
100 0 ▶ croak "$_[0] is not a number!" unless looks_like_number $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($_[0]) 0 0 0
101 }
102 1 );
103  
104 has 'log_severity' => ( is => 'ro',
105 0 ▶ isa => sub { croak "log severity value not valid\n"
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\b(ERROR|OFF|FATAL|INFO|DEBUG|TRACE|ALL|WARN)\b/ 0 0 0
106 unless $_[0] =~ /\b(ERROR|OFF|FATAL|INFO|DEBUG|TRACE|ALL|WARN)\b/;
107 },
108 1 default => 'INFO'
109 );
110  
111 1 has 'realm' => ( is => 'ro', default => 'ManagementRealm' );
112 has 'ssl' => ( is => 'ro', default => 'off',
113 isa =>
114 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
115 0 ▶ croak "valid ssl value is on or off"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\b(on|off)\b/ 0 0 0
116 unless $_[0] =~ /\b(on|off)\b/;
117 }
118 1 );
119 has 'ssl_verify' => ( is => 'ro',
120 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
121 0 my $ssl_verify = $_[0];
122 0 $ssl_verify = lc ($ssl_verify);
123
124 0 ▶ if ($ssl_verify eq 'yes') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ssl_verify eq 'yes') { } 0 0 0
elsif ($ssl_verify eq 'no') { } 0 0 0
125 0 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 1;
126 }
127 elsif ($ssl_verify eq 'no') {
128 0 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
129 }
130 else {
131 0 croak "ssl_verify valid argument is yes/no";
132 }
133 },
134 0 ▶ default => sub { $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; return 'no'; } );
Subroutine __ANON__: not called CC=1 SCAR=2.6
135  
136 has 'is_xa' => ( is => 'ro' => default => 'no',
137 isa =>
138 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
139 0 ▶ croak "valid is_xa value is yes or no"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\b(yes|no)\b/ 0 0 0
140 unless $_[0] =~ /\b(yes|no)\b/;
141 }
142 1 );
143  
144 =head1 SUBROUTINES/METHODS
145  
146 You may want to check :
147 - perldoc Net::JBoss::Management
148  
149 =head2 BUILD
150  
151 The Constructor, build logging, call pass_log_obj method
152 =cut
153  
154 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
155 0 my $self = shift;
156
157 0 $self->pass_log_obj();
158 }
159  
160 =head2 pass_log_obj
161  
162 it will build the log which stored to $self->log
163 you can assign the severity level by assigning the log_severity
164
165 # output to console / screen
166 # format :
167 # %d = current date with yyyy/MM/dd hh:mm:ss format
168 # %p = Log Severity
169 # %P = pid of the current process
170 # %L = Line number within the file where the log statement was issued
171 # %M = Method or function where the logging request was issued
172 # %m = The message to be logged
173 # %n = Newline (OS-independent)
174
175 =cut
176  
177 0 ▶sub pass_log_obj {
Subroutine pass_log_obj: not called CC=2 SCAR=13.0
Pod documented
178 0 my $self = shift;
179
180 # skip if already set
181 0 ▶ return if $self->log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
182
183 0 my $severity = $self->log_severity;
184 0 my $log_conf =
185 qq /
186 log4perl.logger = $severity, Screen
187 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
188 log4perl.appender.Screen.stderr = 0
189 log4perl.appender.Screen.layout = PatternLayout
190 log4perl.appender.Screen.layout.ConversionPattern = %d || %p || %P || %L || %M || %m%n
191 /;
192
193 1 ▶ use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 0 Log::Log4perl::init(\$log_conf);
195 0 my $log = Log::Log4perl->get_logger();
196 0 $self->_set_log($log);
197 }
198  
199 =head2 base_url
200  
201 return the base url
202 =cut
203  
204 0 ▶sub base_url {
Subroutine base_url: not called CC=3 SCAR=24.8
Pod documented
205 0 my $self = shift;
206
207 0 my $url = $self->server;
208
209 0 ▶ if ($self->port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->port) 0 0 0
210 0 $url = $self->server . ":" . $self->port;
211 }
212
213 # http or https
214 0 ▶ if ($self->ssl eq 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl eq 'off') { } 0 0 0
215 0 $url = "http://" . $url;
216 }
217 else {
218 0 $url = "https://" . $url;
219 }
220
221 0 $self->log->debug($url);
222 0 return $url;
223 }
224  
225 =head2 get_api_response
226  
227 return http api response
228 =cut
229  
230 0 ▶sub get_api_response {
Subroutine get_api_response: not called CC=6 SCAR=37.4
Pod documented
231 0 my $self = shift;
232
233 0 ▶ croak "url required" unless $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->url 0 0 0
234
235 0 $self->log->debug("username = " . $self->username);
236 0 $self->log->debug("password = " . $self->password);
237 0 $self->log->debug("port = " . $self->port);
238 0 $self->log->debug("realm = " . $self->realm);
239
240 0 my $ua = LWP::UserAgent->new();
241 0 $ua->credentials( $self->server . ":" . $self->port,
242 $self->realm ,
243 $self->username ,
244 $self->password ,
245 );
246
247 # write operation require post method
248 0 my $res;
249 0 ▶ if ($self->http_post) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->http_post) { } 0 0 0
250 0 ▶ croak "post_json not set"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->post_json 0 0 0
251 unless $self->post_json;
252
253 0 my $req = HTTP::Request->new(POST => $self->url);
254 0 $req->content_type ('application/json');
255 0 $req->content ($self->post_json);
256
257 0 $res = $ua->request ($req);
258 }
259 else {
260 0 ▶ croak "resource url required" unless $self->resource_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_url 0 0 0
261
262 # set final url
263 0 $self->_set_url($self->url . $self->resource_url);
264
265 0 $self->log->debug($self->url);
266 0 $res = $ua->get($self->url);
267 }
268  
269 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
270 0 $self->log->debug($res->decoded_content);
271 0 return $res->decoded_content;
272 }
273 else {
274 0 my $err = $res->status_line;
275 0 $self->log->debug("LWP Error : " . $err);
276 0 return $res->decoded_content;
277 }
278 }
279  
280 =head2 trim
281  
282 trim function to remove whitespace from the start and end of the string
283 =cut
284  
285 sub trim()
286 0 ▶{
Subroutine trim: not called CC=1 SCAR=6.9
Pod documented
287 0 my ($self, $string) = @_;
288 0 $string =~ s/^\s+|\s+$//g;
289 0 return $string;
290 }
291  
292 =head1 AUTHOR
293  
294 "Heince Kurniawan", C<< <"heince at cpan.org"> >>
295  
296 =head1 BUGS
297  
298 Please report any bugs or feature requests to "heince at cpan.org", or through
299 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net::JBoss>. I will be notified, and then you'll
300 automatically be notified of progress on your bug as I make changes.
301  
302  
303 =head1 SUPPORT
304  
305 You can find documentation for this module with the perldoc command.
306  
307 perldoc Net::JBoss
308  
309  
310 You can also look for information at:
311  
312 =over 4
313  
314 =item * RT: CPAN's request tracker (report bugs here)
315  
316 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net::JBoss>
317  
318 =item * AnnoCPAN: Annotated CPAN documentation
319  
320 L<http://annocpan.org/dist/Net::JBoss>
321  
322 =item * CPAN Ratings
323  
324 L<http://cpanratings.perl.org/d/Net::JBoss>
325  
326 =item * Search CPAN
327  
328 L<http://search.cpan.org/dist/Net::JBoss/>
329  
330 =back
331  
332  
333 =head1 ACKNOWLEDGEMENTS
334  
335  
336 =head1 LICENSE AND COPYRIGHT
337  
338 Copyright 2015 "Heince Kurniawan".
339  
340 This program is free software; you can redistribute it and/or modify it
341 under the terms of the the Artistic License (2.0). You may obtain a
342 copy of the full license at:
343  
344 L<http://www.perlfoundation.org/artistic_license_2_0>
345  
346 Any use, modification, and distribution of the Standard or Modified
347 Versions is governed by this Artistic License. By using, modifying or
348 distributing the Package, you accept this license. Do not use, modify,
349 or distribute the Package, if you do not accept this license.
350  
351 If your Modified Version has been derived from a Modified Version made
352 by someone other than you, you are nevertheless required to ensure that
353 your Modified Version complies with the requirements of this license.
354  
355 This license does not grant you the right to use any trademark, service
356 mark, tradename, or logo of the Copyright Holder.
357  
358 This license includes the non-exclusive, worldwide, free-of-charge
359 patent license to make, have made, use, offer to sell, sell, import and
360 otherwise transfer the Package with respect to any patent claims
361 licensable by the Copyright Holder that are necessarily infringed by the
362 Package. If you institute patent litigation (including a cross-claim or
363 counterclaim) against any party alleging that the Package constitutes
364 direct or contributory patent infringement, then this Artistic License
365 to you shall terminate on the date that such litigation is filed.
366  
367 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
368 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
369 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
370 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
371 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
372 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
373 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
374 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
375  
376  
377 =cut
378  
379 1 1; # End of JBoss
« JBoss/Management.pm ↑ Summary