File Coverage

blib/lib/OCS/Client.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             ## no critic (Modules::RequireExplicitPackage)
2              
3             package OCS::Client;
4             {
5             $OCS::Client::VERSION = '0.011';
6             }
7             # ABSTRACT: simple interface to OCS's SOAP API
8              
9 1     1   18502 use utf8;
  1         8  
  1         4  
10 1     1   31 use strict;
  1         2  
  1         57  
11 1     1   4 use warnings;
  1         5  
  1         25  
12 1     1   3 use Carp;
  1         1  
  1         111  
13 1     1   556 use URI;
  1         5954  
  1         32  
14 1     1   224 use SOAP::Lite;
  0            
  0            
15             use XML::Entities;
16             use XML::Simple;
17              
18              
19             sub new {
20             my ($class, $url, $user, $pass, @args) = @_;
21              
22             my $uri = URI->new($url);
23             $uri->path("/Apache/Ocsinventory/Interface");
24              
25             my $proxy = URI->new($url);
26              
27             my $userinfo;
28             $userinfo = $user if $user;
29             $userinfo .= ':' if $user && $pass;
30             $userinfo .= $pass if $pass;
31             $proxy->userinfo($userinfo) if $userinfo;
32              
33             $proxy->path("/ocsinterface");
34              
35             my $self = { soap => SOAP::Lite->uri($uri->as_string)->proxy($proxy->as_string, @args) };
36              
37             return bless $self, $class;
38             }
39              
40              
41             sub get_computers_V1 {
42             my ($self, @args) = @_;
43             my %request = (
44             engine => 'FIRST',
45             asking_for => 'INVENTORY',
46             checksum => 0x01FFFF,
47             wanted => 0x000003,
48             offset => 0,
49             @args,
50             );
51              
52             my $request = "\n";
53             while (my ($tag, $value) = each %request) {
54             $request .= " <\U$tag\E>$value\n";
55             }
56             $request .= "\n";
57              
58             my $som = $self->{soap}->get_computers_V1($request);
59              
60             croak "ERROR: ", XML::Entities::decode('all', $som->fault->{faultstring})
61             if $som->fault;
62              
63             my @computers = $som->paramsall;
64              
65             # peel of the tag of @computers
66             shift @computers;
67             pop @computers;
68              
69             return map {XMLin($_, ForceArray => [qw/DRIVES NETWORKS PRINTERS SOFTWARES STORAGES VIDEOS/])} @computers;
70             }
71              
72              
73             sub computer_iterator {
74             my ($self, %request) = @_;
75             my @computers;
76             my $offset = 0;
77             return sub {
78             unless (@computers) {
79             @computers = $self->get_computers_V1(%request, offset => $offset);
80             ++$offset;
81             }
82             return shift @computers;
83             };
84             }
85              
86             # This hash is used to map OCS custom field ids (in the form
87             # "fields_N") into their names.
88             my %fields = (
89             3 => 'UA',
90             4 => 'Sala',
91             5 => 'Nome do Usuário',
92             6 => 'Atividade',
93             7 => 'Nome da Empresa',
94             8 => 'Ponto de Rede',
95             9 => 'Switch',
96             10 => 'Porta',
97             11 => 'Status',
98             13 => 'Observações',
99             14 => 'Local do Ponto',
100             15 => 'Asset Number',
101             16 => 'Responsável',
102             17 => 'Tipo',
103             18 => 'Padrão de HW',
104             19 => 'Data de Aquisição',
105             20 => 'UA Username',
106             21 => 'Office',
107             22 => 'Office Tag',
108             23 => 'PA',
109             26 => 'Diretoria',
110             27 => 'Nota Fiscal',
111             28 => 'HW Guidelines',
112             );
113              
114              
115             sub prune {
116             my ($computer) = @_;
117              
118             foreach (my ($key, $accountinfo) = each %{$computer->{ACCOUNTINFO}}) {
119             my %myinfo;
120             foreach my $info (grep {exists $_->{content}} @$accountinfo) {
121             if ($info->{Name} =~ /^fields_(\d+)$/) {
122             if (exists $fields{$1}) {
123             $myinfo{$fields{$1}} = $info->{content};
124             } else {
125             carp "Skipping unknown ACCOUNTINFO field id: $1";
126             }
127             } else {
128             $myinfo{$info->{Name}} = $info->{content};
129             }
130             }
131              
132             delete $myinfo{'UA Username'};
133              
134             $computer->{ACCOUNTINFO}{$key} = \%myinfo;
135             }
136              
137             if (exists $computer->{DRIVES}) {
138             foreach my $drive (@{$computer->{DRIVES}}) {
139             $drive->{ORDER} = (ref $drive->{VOLUMN} ? '' : $drive->{VOLUMN}) . (ref $drive->{LETTER} ? '' : $drive->{LETTER});
140             $drive->{ORDER} =~ s@:/$@:@;
141             delete @{$drive}{qw/CREATEDATE FREE LETTER NUMFILES VOLUMN/};
142             }
143             $computer->{DRIVES} = [sort {$a->{ORDER} cmp $b->{ORDER}} grep {$_->{TYPE} !~ /removable/i} @{$computer->{DRIVES}}];
144             }
145              
146             if (exists $computer->{HARDWARE}) {
147             delete @{$computer->{HARDWARE}}{qw/FIDELITY LASTCOME IPADDR IPSRC LASTDATE PROCESSORS QUALITY USERID SWAP/};
148             $computer->{HARDWARE}{DESCRIPTION} =~ s@^([^/]+)/\d\d-\d\d-\d\d \d\d:\d\d:\d\d$@$1@;
149             }
150              
151             if (exists $computer->{NETWORKS}) {
152             foreach my $net (@{$computer->{NETWORKS}}) {
153             delete @{$net}{qw/SPEED STATUS/};
154             }
155             }
156              
157             if (exists $computer->{PRINTERS}) {
158             $computer->{PRINTERS} = [sort {$a->{NAME} cmp $b->{NAME}} @{$computer->{PRINTERS}}];
159             }
160              
161             # Of the software we only keep the name and the version
162             if (exists $computer->{SOFTWARES}) {
163             $computer->{SOFTWARES} = {map {($_->{NAME} => $_->{VERSION})} @{$computer->{SOFTWARES}}};
164             }
165              
166             if (exists $computer->{STORAGES}) {
167             $computer->{STORAGES} = [grep {$_->{TYPE} !~ /removable/i} @{$computer->{STORAGES}}];
168             }
169              
170             if (exists $computer->{VIDEOS}) {
171             foreach my $video (@{$computer->{VIDEOS}}) {
172             delete @{$video}{qw/RESOLUTION/};
173             }
174             }
175              
176             return $computer;
177             }
178              
179              
180             use constant { ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
181             # CHECKSUM constants
182             'HARDWARE' => 0x00001,
183             'BIOS' => 0x00002,
184             'MEMORY_SLOTS' => 0x00004,
185             'SYSTEM_SLOTS' => 0x00008,
186             'REGISTRY' => 0x00010,
187             'SYSTEM_CONTROLLERS' => 0x00020,
188             'MONITORS' => 0x00040,
189             'SYSTEM_PORTS' => 0x00080,
190             'STORAGE_PERIPHERALS' => 0x00100,
191             'LOGICAL_DRIVES' => 0x00200,
192             'INPUT_DEVICES' => 0x00400,
193             'MODEMS' => 0x00800,
194             'NETWORK_ADAPTERS' => 0x01000,
195             'PRINTERS' => 0x02000,
196             'SOUND_ADAPTERS' => 0x04000,
197             'VIDEO_ADAPTERS' => 0x08000,
198             'SOFTWARE' => 0x10000,
199              
200             # WANTED constants
201             'ACOUNTINFO' => 0x00001,
202             'DICO_SOFT' => 0x00002,
203             };
204              
205             1; # End of OCS::Client
206              
207             __END__