blib/lib/OCS/Client.pm

filter: Statementstmt 26.3% 30 / 114 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 64.2% 9 / 14 Podpod 100% 4 / 4 totaltotal 24.8% 43 / 173 CC 25 SCAR 49.8
CC
25
Cov
20%
prune
306.0
new
42.0
get_computers_V1
12.0
CRAP 145.1
↑ Summary
1 ## no critic (Modules::RequireExplicitPackage)
2  
3 package OCS::Client;
4 # ABSTRACT: simple interface to OCS's SOAP API
5 1 $OCS::Client::VERSION = '0.013';
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use SOAP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use XML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 our %ACCOUNT_INFO;
17  
18  
19 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
20 0 my ($class, $url, $user, $pass, @args) = @_;
21  
22 0 my $uri = URI->new($url);
23 0 $uri->path("/Apache/Ocsinventory/Interface");
24  
25 0 my $proxy = URI->new($url);
26  
27 0 my $userinfo;
28 0 ▶ $userinfo = $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
29 0 ▶ $userinfo .= ':' if $user && $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $user and $pass 0 0 0
Condition0%0/3
$user and $pass
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $pass
$user $pass
Truth table0%0/3
A and B
A= $userB= $pass
ABresult
0X0
100
111
30 0 ▶ $userinfo .= $pass if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 0 0 0
31 0 ▶ $proxy->userinfo($userinfo) if $userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if $userinfo 0 0 0
32  
33 0 $proxy->path("/ocsinterface");
34  
35 0 my $self = { soap => SOAP::Lite->uri($uri->as_string)->proxy($proxy->as_string, @args) };
36  
37 0 return bless $self, $class;
38 }
39  
40  
41 0 ▶sub get_computers_V1 {
Subroutine get_computers_V1: not called CC=3 SCAR=24.8
Pod documented
42 0 my ($self, @args) = @_;
43 0 my %request = (
44 engine => 'FIRST',
45 asking_for => 'INVENTORY',
46 checksum => 0x01FFFF,
47 wanted => 0x000003,
48 offset => 0,
49 @args,
50 );
51  
52 0 my $request = "<REQUEST>\n";
53 0 ▶ while (my ($tag, $value) = each %request) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %request) 0 0 0
54 0 $request .= " <\U$tag\E>$value</\U$tag\E>\n";
55 }
56 0 $request .= "</REQUEST>\n";
57  
58 0 my $som = $self->{soap}->get_computers_V1($request);
59  
60 croak "ERROR: ", XML::Entities::decode('all', $som->fault->{faultstring})
61 0 ▶ if $som->fault;
Branch: 1 branch
BranchTrueFalseTotal
if $som->fault 0 0 0
62  
63 0 my @computers = $som->paramsall;
64  
65 # peel of the <COMPUTERS> tag of @computers
66 0 shift @computers;
67 0 pop @computers;
68  
69 0 return map {XMLin($_, ForceArray => [qw/DRIVES NETWORKS PRINTERS SOFTWARES STORAGES VIDEOS/])} @computers;
70 }
71  
72  
73 0 ▶sub computer_iterator {
Subroutine computer_iterator: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, %request) = @_;
75 0 my @computers;
76 0 my $offset = 0;
77 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 ▶ unless (@computers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@computers) 0 0 0
79 0 @computers = $self->get_computers_V1(%request, offset => $offset);
80 0 ++$offset;
81 }
82 0 return shift @computers;
83 0 };
84 }
85  
86  
87 0 ▶sub prune {
Subroutine prune: not called CC=17 SCAR=57.2
Pod documented
88 0 my ($computer) = @_;
89  
90 0 foreach (my ($key, $accountinfo) = each %{$computer->{ACCOUNTINFO}}) {
91 0 my %myinfo;
92 0 foreach my $info (grep {exists $_->{content}} @$accountinfo) {
93 0 ▶ if ($info->{Name} =~ /^fields_(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'Name'} =~ /^fields_(\d+)$/) { } 0 0 0
94 0 ▶ if (exists $ACCOUNT_INFO{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $OCS::Client::ACCOUNT_INFO{$1}) { } 0 0 0
95 0 $myinfo{$ACCOUNT_INFO{$1}} = $info->{content};
96 } else {
97 0 carp "Skipping unknown ACCOUNTINFO field for $key: ($info->{Name} => $info->{content})";
98 }
99 } else {
100 0 $myinfo{$info->{Name}} = $info->{content};
101 }
102 }
103  
104 0 delete $myinfo{'UA Username'};
105  
106 0 $computer->{ACCOUNTINFO}{$key} = \%myinfo;
107 }
108  
109 0 ▶ if (exists $computer->{DRIVES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"DRIVES"}) 0 0 0
110 0 foreach my $drive (@{$computer->{DRIVES}}) {
111 0 ▶ $drive->{ORDER} = (ref $drive->{VOLUMN} ? '' : $drive->{VOLUMN}) . (ref $drive->{LETTER} ? '' : $drive->{LETTER});
Branch: 2 branches
BranchTrueFalseTotal
ref $drive->{'VOLUMN'} ? : 0 0 0
ref $drive->{'LETTER'} ? : 0 0 0
112 0 $drive->{ORDER} =~ s@:/$@:@;
113 0 delete @{$drive}{qw/CREATEDATE FREE LETTER NUMFILES VOLUMN/};
114 }
115 0 $computer->{DRIVES} = [sort {$a->{ORDER} cmp $b->{ORDER}} grep {$_->{TYPE} !~ /removable/i} @{$computer->{DRIVES}}];
116 }
117  
118 0 ▶ if (exists $computer->{HARDWARE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"HARDWARE"}) 0 0 0
119 0 delete @{$computer->{HARDWARE}}{qw/FIDELITY LASTCOME IPADDR IPSRC LASTDATE PROCESSORS QUALITY USERID SWAP/};
120 0 $computer->{HARDWARE}{DESCRIPTION} =~ s@^([^/]+)/\d\d-\d\d-\d\d \d\d:\d\d:\d\d$@$1@;
121 }
122  
123 0 ▶ if (exists $computer->{NETWORKS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"NETWORKS"}) 0 0 0
124 0 foreach my $net (@{$computer->{NETWORKS}}) {
125 0 delete @{$net}{qw/SPEED STATUS/};
126 }
127 }
128  
129 0 ▶ if (exists $computer->{PRINTERS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"PRINTERS"}) 0 0 0
130 0 $computer->{PRINTERS} = [sort {$a->{NAME} cmp $b->{NAME}} @{$computer->{PRINTERS}}];
131 }
132  
133 # Of the software we only keep the name and the version
134 0 ▶ if (exists $computer->{SOFTWARES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"SOFTWARES"}) 0 0 0
135 0 $computer->{SOFTWARES} = {map {($_->{NAME} => $_->{VERSION})} @{$computer->{SOFTWARES}}};
136 }
137  
138 0 ▶ if (exists $computer->{STORAGES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"STORAGES"}) 0 0 0
139 0 $computer->{STORAGES} = [grep {$_->{TYPE} !~ /removable/i} @{$computer->{STORAGES}}];
140 }
141  
142 0 ▶ if (exists $computer->{VIDEOS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $computer->{"VIDEOS"}) 0 0 0
143 0 foreach my $video (@{$computer->{VIDEOS}}) {
144 0 delete @{$video}{qw/RESOLUTION/};
145 }
146 }
147  
148 0 return $computer;
149 }
150  
151  
152 1 ▶use constant { ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
Subroutine BEGIN: called CC=1 SCAR=0.0
153 # CHECKSUM constants
154 1 'HARDWARE' => 0x00001,
155 'BIOS' => 0x00002,
156 'MEMORY_SLOTS' => 0x00004,
157 'SYSTEM_SLOTS' => 0x00008,
158 'REGISTRY' => 0x00010,
159 'SYSTEM_CONTROLLERS' => 0x00020,
160 'MONITORS' => 0x00040,
161 'SYSTEM_PORTS' => 0x00080,
162 'STORAGE_PERIPHERALS' => 0x00100,
163 'LOGICAL_DRIVES' => 0x00200,
164 'INPUT_DEVICES' => 0x00400,
165 'MODEMS' => 0x00800,
166 'NETWORK_ADAPTERS' => 0x01000,
167 'PRINTERS' => 0x02000,
168 'SOUND_ADAPTERS' => 0x04000,
169 'VIDEO_ADAPTERS' => 0x08000,
170 'SOFTWARE' => 0x10000,
171  
172 # WANTED constants
173 'ACOUNTINFO' => 0x00001,
174 'DICO_SOFT' => 0x00002,
175 1 };
176  
177 1 1; # End of OCS::Client
178  
179 __END__
↑ Summary