blib/lib/Net/Intermapper.pm

filter: Statementstmt 19.8% 71 / 358 Branchbran 0.0% 0 / 158 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.1% 22 / 36 Podpod 66.6% 8 / 12 totaltotal 17.9% 101 / 564 CC 89 SCAR 50.6
CC
89
Cov
14%
parse_csv
380.0
parse_tab
380.0
query
156.0
CRAP 156.8
« blib/lib/Net/Intermapper/Device.pm ↑ Summary blib/lib/Net/Intermapper/User.pm »
1 package Net::Intermapper;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # REST IO stuff here
6 1 ▶use IO::Socket::SSL qw( SSL_VERIFY_NONE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Generics
10 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Net::Intermapper::*
17 1 ▶use Net::Intermapper::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Net::Intermapper::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Net::Intermapper::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Net::Intermapper::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Net::Intermapper::Vertice;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 BEGIN {
24 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $ERROR %_CHANGED);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶ $VERSION = '0.02';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 @ISA = qw(Exporter);
28 1 @EXPORT = qw();
29 1 @EXPORT_OK = qw();
30 1 %EXPORT_TAGS = ();
31 1 %_CHANGED = ();
32 1 $ERROR = ""; # TODO: Document error properly!
33 }
34  
35 # Moose!
36  
37 has 'ssl_options' => (
38 is => 'rw',
39 isa => 'HashRef',
40 1 ▶ default => sub { { 'SSL_verify_mode' => SSL_VERIFY_NONE, 'verify_hostname' => '0' } }
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 );
42  
43 1 has 'ssl' => (
44 is => 'rw',
45 isa => 'Str',
46 default => '1',
47 );
48  
49 1 has 'hostname' => (
50 is => 'rw',
51 isa => 'Str',
52 required => '1',
53 );
54  
55 has 'port' => (
56 is => 'rw',
57 isa => 'Int',
58 1 ▶ default => sub { 8181 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 );
60  
61 has 'modifyport' => (
62 is => 'rw',
63 isa => 'Int',
64 1 ▶ default => sub { 443 },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 );
66
67 has 'username' => (
68 is => 'rw',
69 isa => 'Str',
70 0 ▶ default => sub { "admin" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 1 );
72  
73 has 'password' => (
74 is => 'rw',
75 isa => 'Str',
76 0 ▶ default => sub { "nmsadmin" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 1 );
78
79 has 'format' => (
80 is => 'rw',
81 isa => 'Str',
82 1 ▶ default => sub { "csv" },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 );
84  
85 has 'cache' => (
86 is => 'rw',
87 isa => 'Int',
88 1 ▶ default => sub { "1" }, # Cache any request
Subroutine __ANON__: called CC=1 SCAR=0.0
89 1 );
90  
91 sub users # No Moose here :(
92 0 ▶{ my $self = shift;
Subroutine users: not called CC=6 SCAR=37.4
Pod documented
93 0 $ERROR = "";
94 0 ▶ $_CHANGED{"users"} = !$self->{"Users"} ? 1 : $_CHANGED{"users"};
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'Users'} ? : 0 0 0
95 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
96 { #my %args = @_;
97 #$self->{"Users"} = $args{"users"};
98 0 $self->{"Users"} = $_[0]; # Expect a hash ref to be passed
99 # Yes, this code is slightly based of the Net::Cisco::ACS code so may be some of it is weird
100 # Are you really reading the comments now?
101 } else
102 0 ▶ { if (!$self->cache) # No caching? Always reload
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->cache) { } 0 0 0
103 0 { $self->{"Users"} = $self->query("users"); }
104 else
105 0 ▶ { $self->{"Users"} = $self->query("users") if $_CHANGED{"users"}; # Only reload if changed
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Intermapper::_CHANGED{"users"} 0 0 0
106 }
107 }
108 0 ▶ return wantarray ? % { $self->{"Users"} } : $self->{"Users"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
109 }
110  
111 sub devices # No Moose here :(
112 0 ▶{ my $self = shift;
Subroutine devices: not called CC=6 SCAR=37.4
Pod documented
113 0 $ERROR = "";
114 0 ▶ $_CHANGED{"devices"} = !$self->{"Devices"} ? 1 : $_CHANGED{"devices"};
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'Devices'} ? : 0 0 0
115 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
116 { #my %args = @_;
117 #$self->{"Devices"} = $args{"devices"};
118 0 $self->{"Devices"} = $_[0]; # Expect a hash ref to be passed
119 } else
120 0 ▶ { if (!$self->cache) # No caching? Always reload
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->cache) { } 0 0 0
121 0 { $self->{"Devices"} = $self->query("devices"); }
122 else
123 0 ▶ { $self->{"Devices"} = $self->query("devices") if $_CHANGED{"devices"}; # Only reload if changed
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Intermapper::_CHANGED{"devices"} 0 0 0
124 }
125 }
126 0 ▶ return wantarray ? % { $self->{"Devices"} } : $self->{"Devices"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
127 }
128  
129 sub interfaces # No Moose here :(
130 0 ▶{ my $self = shift;
Subroutine interfaces: not called CC=6 SCAR=37.4
Pod documented
131 0 $ERROR = "";
132 0 ▶ $_CHANGED{"interfaces"} = !$self->{"Interfaces"} ? 1 : $_CHANGED{"interfaces"};
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'Interfaces'} ? : 0 0 0
133 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
134 { #my %args = @_;
135 #$self->{"Interfaces"} = $args{"interfaces"};
136 0 $self->{"Interfaces"} = $_[0]; # Expect a hash ref to be passed
137 } else
138 0 ▶ { if (!$self->cache) # No caching? Always reload
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->cache) { } 0 0 0
139 0 { $self->{"Interfaces"} = $self->query("interfaces"); }
140 else
141 0 ▶ { $self->{"Interfaces"} = $self->query("interfaces") if $_CHANGED{"interfaces"}; # Only reload if changed
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Intermapper::_CHANGED{"interfaces"} 0 0 0
142 }
143 }
144 0 ▶ return wantarray ? % { $self->{"Interfaces"} } : $self->{"Interfaces"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
145 }
146  
147 sub maps # No Moose here :(
148 0 ▶{ my $self = shift;
Subroutine maps: not called CC=6 SCAR=37.4
Pod documented
149 0 $ERROR = "";
150 0 ▶ $_CHANGED{"maps"} = !$self->{"Maps"} ? 1 : $_CHANGED{"maps"};
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'Maps'} ? : 0 0 0
151 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
152 { #my %args = @_;
153 #$self->{"Maps"} = $args{"maps"};
154 0 $self->{"Maps"} = $_[0]; # Expect a hash ref to be passed
155 } else
156 0 ▶ { if (!$self->cache) # No caching? Always reload
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->cache) { } 0 0 0
157 0 { $self->{"Maps"} = $self->query("maps"); }
158 else
159 0 ▶ { $self->{"Maps"} = $self->query("maps") if $_CHANGED{"maps"}; # Only reload if changed
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Intermapper::_CHANGED{"maps"} 0 0 0
160 }
161 }
162 0 ▶ return wantarray ? % { $self->{"Maps"} } : $self->{"Maps"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
163 }
164  
165 sub vertices # No Moose here :(
166 0 ▶{ my $self = shift;
Subroutine vertices: not called CC=6 SCAR=37.4
Pod documented
167 0 $ERROR = "";
168 0 ▶ $_CHANGED{"vertices"} = !$self->{"Vertices"} ? 1 : $_CHANGED{"vertices"};
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'Vertices'} ? : 0 0 0
169 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
170 { #my %args = @_;
171 #$self->{"Vertices"} = $args{"vertices"};
172 0 $self->{"Vertices"} = $_[0]; # Expect a hash ref to be passed
173 } else
174 0 ▶ { if (!$self->cache) # No caching? Always reload
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->cache) { } 0 0 0
175 0 { $self->{"Vertices"} = $self->query("vertices"); }
176 else
177 0 ▶ { $self->{"Vertices"} = $self->query("vertices") if $_CHANGED{"vertices"}; # Only reload if changed
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Intermapper::_CHANGED{"vertices"} 0 0 0
178 }
179 }
180 0 ▶ return wantarray ? % { $self->{"Vertices"} } : $self->{"Vertices"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
181 }
182  
183 sub query
184 0 ▶{ my ($self, $type) = @_;
Subroutine query: not called CC=12 SCAR=50.5
Pod undocumented
185 0 my $hostname = $self->hostname;
186 0 my $username = $self->username;
187 0 my $password = $self->password;
188 0 my $port = $self->port;
189 0 my $format = $self->format;
190 0 ▶ if ($self->ssl)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl) { } 0 0 0
191 0 { $hostname = "https://$username:$password\@$hostname:$port"; } else
192 0 { $hostname = "http://$username:$password\@$hostname:$port"; }
193 0 $hostname .= "/~export/$type.$format";
194 0 my $request = HTTP::Request->new("GET" => $hostname);
195 0 my $useragent = LWP::UserAgent->new (ssl_opts => $self->ssl_options);
196  
197 0 my $result = $useragent->request($request);
198 0 ▶ if ($result->code eq "400") { $ERROR = "Bad Request - HTTP Status: 400"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($result->code eq 400) 0 0 0
199 0 ▶ if ($result->code eq "410") { $ERROR = "Unknown $type queried by name or ID - HTTP Status: 410"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($result->code eq 410) 0 0 0
200 0 ▶ if ($self->format eq "csv")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq "csv") 0 0 0
201 0 { $self->parse_csv($type, $result->decoded_content);
202 }
203 0 ▶ if ($self->format eq "tab")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq "tab") 0 0 0
204 0 { $self->parse_tab($type, $result->decoded_content);
205 }
206  
207 0 ▶ if ($self->format eq "xml")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq "xml") 0 0 0
208 { #$self->parse_xml($type, $result->content); # XML seems to be broken?!?
209 #warn Dumper $result->content;
210 #This needs work!!
211 }
212 0 $_CHANGED{$type} = 0;
213 0 ▶ return $self->{"Users"} if $type eq "users";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "users" 0 0 0
214 0 ▶ return $self->{"Devices"} if $type eq "devices";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "devices" 0 0 0
215 0 ▶ return $self->{"Interfaces"} if $type eq "interfaces";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "interfaces" 0 0 0
216 0 ▶ return $self->{"Maps"} if $type eq "maps";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "maps" 0 0 0
217 0 ▶ return $self->{"Vertices"} if $type eq "vertices";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "vertices" 0 0 0
218 }
219  
220 # It seems that Maps and Interfaces cannot be created?
221 sub create
222 0 ▶{ my $self = shift;
Subroutine create: not called CC=7 SCAR=40.3
Pod documented
223 0 my @entries = @_;
224 0 ▶ return unless @entries;
Branch: 1 branch
BranchTrueFalseTotal
unless @entries 0 0 0
225 0 my $username = $self->username;
226 0 my $password = $self->password;
227 0 my $format = $self->format; # csv or xml (unsupported at this point)
228 0 my $type = "";
229
230 0 my $hostname = $self->hostname;
231 0 my $port = $self->modifyport;
232 0 ▶ if ($port ne "") { $port = ":$port"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($port ne "") 0 0 0
233 0 ▶ if ($self->ssl)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl) { } 0 0 0
234 0 { $hostname = "https://$username:$password\@$hostname$port/~import/file"; } else
235 0 { $hostname = "http://$username:$password\@$hostname$port/~import/file"; }
236  
237 0 my $data = $entries[0]->header($format);
238 0 $type = lc(ref($entries[0]));
239 0 $type =~ s/^net\:\:intermapper\:\://; # Needed for the change flags@
240 0 ▶ while(@entries)
Branch: 1 branch
BranchTrueFalseTotal
if (@entries) 0 0 0
241 0 { my $entry = shift @entries;
242 0 ▶ if ($format eq "csv")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "csv") 0 0 0
243 0 { $data .= $entry->toCSV; }
244 0 ▶ if ($format eq "tab")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "tab") 0 0 0
245 0 { $data .= $entry->toTAB; }
246 0 $data =~ s/^\s*//g;
247 }
248  
249 0 my $request = HTTP::Request->new(POST => "$hostname");
250 0 my $useragent = LWP::UserAgent->new("ssl_opts" => $self->ssl_options);
251  
252 0 $request->content_type('application/x-www-form-urlencoded');
253 0 $request->content($data);
254 0 $request->header('Accept' => 'text/html');
255 0 my $result = $useragent->request($request);
256 0 $_CHANGED{$type} = 0; # Flag smart caching to force reload
257 0 return $result;
258 }
259  
260 # This SHOULD be sufficient?
261 sub update
262 0 ▶{ my $self = shift;
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
263 0 my $entry = shift;
264 0 $entry->mode("update");
265 0 return $self->create($entry);
266 }
267  
268 # This SHOULD be sufficient?
269 sub delete
270 0 ▶{ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
271 0 my $entry = shift;
272 0 $entry->mode("delete");
273 0 return $self->create($entry);
274 }
275  
276 sub parse_xml # Broken!
277 0 ▶{ my $self = shift;
Subroutine parse_xml: not called CC=11 SCAR=48.8
Pod undocumented
278 0 my $type = shift;
279 0 my $xml_ref = shift;
280 0 my $xmlsimple = XML::Simple->new();
281 0 my $xmlout = $xmlsimple->XMLin($xml_ref);
282 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
283 0 { my $users_ref = $xmlout->{"users"};
284 0 my %users = ();
285 0 for my $key (@ {$users_ref})
286 0 { my $user = Net::Intermapper::User->new( @{ $key } );
287 0 $users{$key} = $user;
288 }
289 0 $self->{"Users"} = \%users; #Not sure if this works with the
290 0 return $self->{"Users"};
291 }
292
293 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
294 0 { my $devices_ref = $xmlout->{"devices"};
295 0 my %devices = ();
296 0 for my $key (@ {$devices_ref})
297 0 { my $device = Net::Intermapper::Device->new( @{ $key } );
298 0 $devices{$key} = $device;
299 }
300 0 $self->{"Devices"} = \%devices;
301 0 return $self->{"Devices"};
302 }
303
304 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
305 0 { my $interfaces_ref = $xmlout->{"interfaces"};
306 0 my %interfaces = ();
307 0 for my $key (@ {$interfaces_ref})
308 0 { my $interface = Net::Intermapper::Interface->new( @{ $key } );
309 0 $interfaces{$key} = $interface;
310 }
311 0 $self->{"Interfaces"} = \%interfaces;
312 0 return $self->{"Interfaces"};
313 }
314
315 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
316 0 { my $maps_ref = $xmlout->{"maps"};
317 0 my %maps = ();
318 0 for my $key (@ {$maps_ref})
319 0 { my $map = Net::Intermapper::Map->new( @{ $key } );
320 0 $maps{$key} = $map;
321 }
322 0 $self->{"Maps"} = \%maps;
323 0 return $self->{"Maps"};
324 }
325  
326 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
327 0 { my $vertices_ref = $xmlout->{"vertices"};
328 0 my %vertices = ();
329 0 for my $key (@ {$vertices_ref})
330 0 { my $vertice = Net::Intermapper::Vertice->new( @{ $key } );
331 0 $vertices{$key} = $vertice;
332 }
333 0 $self->{"Vertices"} = \%vertices;
334 0 return $self->{"Vertices"};
335 }
336
337 }
338  
339 sub parse_csv
340 0 ▶{ my $self = shift;
Subroutine parse_csv: not called CC=19 SCAR=59.4
Pod undocumented
341 0 my $type = shift;
342 0 my $csv_ref = shift;
343 0 my @header = ();
344 0 my %data = ();
345 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
346 0 { @header = @Net::Intermapper::User::HEADERS; }
347 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
348 0 { @header = @Net::Intermapper::Device::HEADERS; }
349 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
350 0 { @header = @Net::Intermapper::Interface::HEADERS; }
351 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
352 0 { @header = @Net::Intermapper::Map::HEADERS; }
353 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
354 0 { @header = @Net::Intermapper::Vertice::HEADERS; }
355
356 0 my @lines = split(/\r\n/,$csv_ref);
357 0 my $csv = Text::CSV_XS->new ({ "auto_diag" => "1", "binary" => "1" });
358 0 for my $line (@lines)
359 0 { my $i = 0;
360 0 my %fields = ();
361 0 ▶ if ($csv->parse ($line))
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($line)) 0 0 0
362 0 { my @fields = $csv->fields;
363 0 for my $field (@fields)
364 0 { $fields{$header[$i]} = $field;
365 0 $i++;
366 }
367 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
368 0 { my $user = Net::Intermapper::User->new( %fields );
369 0 $data{$user->Name} = $user;
370 }
371 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
372 0 { my $device = Net::Intermapper::Device->new( %fields );
373 0 $data{$device->Id} = $device;
374 }
375 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
376 0 { my $interface = Net::Intermapper::Interface->new( %fields );
377 0 $data{$interface->InterfaceID} = $interface;
378 }
379 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
380 0 { my $map = Net::Intermapper::Map->new( %fields );
381 0 $data{$map->MapName} = $map;
382 }
383 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
384 0 { my $vertice = Net::Intermapper::Vertice->new( %fields );
385 0 $data{$vertice->Id} = $vertice;
386 }
387  
388 }
389 }
390 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
391 0 { $self->{"Users"} = \%data;
392 0 return $self->{"Users"};
393 }
394 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
395 0 { $self->{"Devices"} = \%data;
396 0 return $self->{"Devices"};
397 }
398 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
399 0 { $self->{"Interfaces"} = \%data;
400 0 return $self->{"Interfaces"};
401 }
402 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
403 0 { $self->{"Maps"} = \%data;
404 0 return $self->{"Maps"};
405 }
406 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
407 0 { $self->{"Vertices"} = \%data;
408 0 return $self->{"Vertices"};
409 }
410  
411 }
412  
413 sub parse_tab
414 0 ▶{ my $self = shift;
Subroutine parse_tab: not called CC=19 SCAR=59.4
Pod undocumented
415 0 my $type = shift;
416 0 my $tab_ref = shift;
417 0 my @header = ();
418 0 my %data = ();
419 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
420 0 { @header = @Net::Intermapper::User::HEADERS; }
421 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
422 0 { @header = @Net::Intermapper::Device::HEADERS; }
423 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
424 0 { @header = @Net::Intermapper::Interface::HEADERS; }
425 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
426 0 { @header = @Net::Intermapper::Map::HEADERS; }
427 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
428 0 { @header = @Net::Intermapper::Vertice::HEADERS; }
429 0 my $linecount = 0;
430 0 my @lines = split(/[\r\n]{1,2}/,$tab_ref);
431 0 for my $line (@lines)
432 0 { my $i = 0;
433 0 my %fields = ();
434 0 my @fields = split(/\t/,$line);
435 0 ▶ if (!$linecount) { $linecount++; next; } # Skip header line - KNOWN VALUES
Branch: 1 branch
BranchTrueFalseTotal
unless ($linecount) 0 0 0
436 0 for my $field (@fields)
437 0 { $fields{$header[$i]} = $field;
438 0 $i++;
439 }
440 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
441 0 { my $user = Net::Intermapper::User->new( %fields );
442 0 $data{$user->Name} = $user;
443 }
444 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
445 0 { my $device = Net::Intermapper::Device->new( %fields );
446 0 $data{$device->Id} = $device;
447 }
448 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
449 0 { my $interface = Net::Intermapper::Interface->new( %fields );
450 0 $data{$interface->InterfaceID} = $interface;
451 }
452 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
453 0 { my $map = Net::Intermapper::Map->new( %fields );
454 0 $data{$map->MapName} = $map;
455 }
456 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
457 0 { my $vertice = Net::Intermapper::Vertice->new( %fields );
458 0 $data{$vertice->Id} = $vertice;
459 }
460 }
461 0 ▶ if ($type eq "users")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "users") 0 0 0
462 0 { $self->{"Users"} = \%data;
463 0 return $self->{"Users"};
464 }
465 0 ▶ if ($type eq "devices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "devices") 0 0 0
466 0 { $self->{"Devices"} = \%data;
467 0 return $self->{"Devices"};
468 }
469 0 ▶ if ($type eq "interfaces")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "interfaces") 0 0 0
470 0 { $self->{"Interfaces"} = \%data;
471 0 return $self->{"Interfaces"};
472 }
473 0 ▶ if ($type eq "maps")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "maps") 0 0 0
474 0 { $self->{"Maps"} = \%data;
475 0 return $self->{"Maps"};
476 }
477 0 ▶ if ($type eq "vertices")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "vertices") 0 0 0
478 0 { $self->{"Vertices"} = \%data;
479 0 return $self->{"Vertices"};
480 }
481  
482 }
483  
484 =pod
485  
486 =head1 NAME
487  
488 Net::Intermapper - Interface with the HelpSystems Intermapper HTTP API
489  
490 =head1 SYNOPSIS
491  
492 use Net::Intermapper;
493 my $intermapper = Net::Intermapper->new(hostname=>"10.0.0.1", username=>"admin", password=>"nmsadmin");
494 # Options:
495 # hostname - IP or hostname of Intermapper 5.x server
496 # username - Username of Administrator user
497 # password - Password of user
498 # ssl - SSL enabled (1 - default) or disabled (0)
499 # port - TCP port for querying information. Defaults to 8181
500 # modifyport - TCP port for modifying information. Default to 443
501 # cache - Boolean to enable smart caching or force network queries
502  
503 my %users = $intermapper->users;
504 my $users_ref = $intermapper->users;
505 # Retrieve all users from Intermapper, Net::Intermapper::User instances
506 # Returns hash or hashref, depending on context
507
508 my %devices = $intermapper->devices;
509 my $devices_ref = $intermapper->devices;
510 # Retrieve all devices from Intermapper, Net::Intermapper::Device instances
511 # Returns hash or hashref, depending on context
512  
513 my %maps = $intermapper->maps;
514 my $maps_ref = $intermapper->maps;
515 # Retrieve all maps from Intermapper, Net::Intermapper::Map instances
516 # Returns hash or hashref, depending on context
517  
518 my %interfaces = $intermapper->interfaces;
519 my $interfaces_ref = $intermapper->interfaces;
520 # Retrieve all interfaces from Intermapper, Net::Intermapper::Interface instances
521 # Returns hash or hashref, depending on context
522  
523 my %vertices = $intermapper->vertices;
524 my $vertices_ref = $intermapper->vertices;
525 # Retrieve all vertices from Intermapper, Net::Intermapper::Vertice instances
526 # Returns hash or hashref, depending on context
527  
528 my $user = $intermapper->users->{"admin"};
529 print $user->header;
530 print $device->header;
531 print $map->header;
532 print $interface->header;
533 print $vertice->header;
534 # Generate 'directive' needed for manipulation. Mostly used internally.
535 # $user->mode("create"); # This changes the header fields
536 # $user->mode("update"); # This also changes the header fields
537 # Both are NOT needed when adding or updating a record
538 print $user->toTAB;
539 print $device->toXML;
540 print $map->toCSV;
541 # Works on ALL classes
542 # Produce human-readable output of each record.
543
544 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
545 my $response = $intermapper->create($user);
546 # Create new user
547 # Return value is HTTP::Response object
548
549 my $device = Net::Intermapper::Device->new(Name=>"testDevice", MapName=>"TestMap", MapPath=>"/TestMap", Address=>"10.0.0.1");
550 my $response = $intermapper->create($device);
551 # Create new device
552 # Return value is HTTP::Response object
553  
554 $user->Password("Foobar123");
555 my $response = $intermapper->update($user);
556 # Update existing user
557 # Return value is HTTP::Response object
558  
559 my $user = $intermapper->users->{"bob"};
560 my $response = $intermapper->delete($user);
561 # Delete existing user
562 # Return value is HTTP::Response object
563  
564 my $device = $intermapper->devices->{"UniqueDeviceID"};
565 my $response = $intermapper->delete($device);
566 # Delete existing device
567 # Return value is HTTP::Response object
568  
569 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
570 $intermapper->users($users);
571 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
572 # But it can be done with users, devices, interfaces, maps and vertices
573 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
574
575 =head1 DESCRIPTION
576  
577 Net::Intermapper is a perl wrapper around the HelpSystems Intermapper API provided through HTTP/HTTPS for access to user accounts, device information, maps, interfaces and graphical elements.
578  
579 All calls are handled through an instance of the L<Net::Intermapper> class.
580  
581 use Net::Intermapper;
582 my $intermapper = Net::Intermapper->new(hostname => '10.0.0.1', username => 'admin', password => 'nmsadmin');
583  
584 =over 3
585  
586 =item new
587  
588 Class constructor. Returns object of Net::Intermapper on succes. Required fields are:
589  
590 =over 5
591  
592 =item hostname
593  
594 =item username
595  
596 =item password
597  
598 =back
599  
600 Optional fields are
601  
602 =over 5
603  
604 =item ssl
605  
606 =item ssl_options
607  
608 =item port
609  
610 =item modifyport
611  
612 =item cache
613  
614 =back
615  
616 =item hostname
617  
618 IP or hostname of Intermapper server. This is a required value in the constructor but can be redefined afterwards.
619  
620 =item username
621  
622 Username of Administrator user. This is a required value in the constructor but can be redefined afterwards.
623 As the API is used a different mechanism to query information than it uses to update, this needs to be changed when switching. Typically, the built-in C<admin> user is used for modifying a record.
624 This value is not automatically changed when running C<create> or C<update>.
625  
626 =item password
627  
628 Password of user. This is a required value in the constructor but can be redefined afterwards.
629 As the API is used a different mechanism to query information than it uses to update, this needs to be changed when switching. Typically, the built-in C<admin> user is used for modifying a record.
630 This value is not automatically changed when running C<create> or C<update>.
631  
632 =item ssl
633  
634 SSL enabled (1 - default) or disabled (0).
635  
636 =item ssl_options
637  
638 Value is passed directly to LWP::UserAGent as ssl_opt. Default value (hash-ref) is
639  
640 { 'SSL_verify_mode' => SSL_VERIFY_NONE, 'verify_hostname' => '0' }
641  
642 This is a required value in the constructor but can be redefined afterwards.
643
644 =item port
645  
646 TCP port used for queries. This is an optional value in the constructor but can be redefined afterwards. By default, this is set to 8181.
647 As the API is used a different mechanism to query information than it uses to update, the C<port> value is used for queries only and is automatically switched. Set this ONLY if you have customized Intermapper to listen to a different port.
648  
649 =item modifyport
650  
651 TCP port used for modifying values. This is an optional value in the constructor but can be redefined afterwards. By default, this is set to 443.
652 As the API is used a different mechanism to modify (create and update) information than it uses to query, the C<modifyport> value is used for modifying only and is automatically switched. Set this ONLY if you have customized Intermapper to listen to a different port.
653  
654 =item cache
655  
656 Set to true (default) to use in-memory dataset to avoid unnecessary queries. Dataset are always queries when changes are made (after delete, create or update), per type. Changes to users dataset will not affect the devices dataset. This is a required value in the constructor but can be redefined afterwards.
657  
658 $intermapper->cache(0);
659 $intermapper->update($user);
660 # Users have changed.
661 my $users = $intermapper->users;
662 # This will trigger network traffic
663 my $devices = $intermapper->devices
664 # This will NOT trigger network traffic
665  
666 =back
667  
668 From the class instance, call the different methods for retrieving values.
669  
670 =over 3
671  
672 =item users
673  
674 Returns all users
675  
676 my %users = $intermapper->users();
677 my $user = $users{"Bob"};
678 print $user->Password;
679
680 The returned hash contains instances of L<Net::Intermapper::User>, using the username as the hash key. In scalar context, will return hashref. Modify the in-memory users dataset:
681  
682 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
683 $intermapper->users($users);
684 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
685 # But it can be done with users, devices, interfaces, maps and vertices
686 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
687  
688 This method will typically trigger a network connection, depending on caching.
689
690 =item devices
691  
692 returns all devices
693
694 my %devices = $intermapper->devices();
695 my $device = $devices{"MainRouter"};
696 print $device->Address;
697  
698 The returned hash contains instances of L<Net::Intermapper::Device>, using the device name as the hash key. In scalar context, will return hashref. Modify the in-memory users dataset:
699  
700 my $devices = { "MainRouter1" => $main1, "MainRouter2" => $main2 };
701 $intermapper->devices($devices);
702 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
703 # But it can be done with users, devices, interfaces, maps and vertices
704 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
705  
706 This method will typically trigger a network connection, depending on caching.
707
708 =item maps
709  
710 returns all maps
711
712 my %maps = $intermapper->devices();
713 my $map = $maps{"MainMap"};
714 print $map->Name;
715  
716 The returned hash contains instances of L<Net::Intermapper::Map>, using the map name as the hash key. In scalar context, will return hashref. Modify the in-memory users dataset:
717  
718 my $maps = { "MainMap" => $main1, "Layer2" => $main2 };
719 $intermapper->maps($maps);
720 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
721 # But it can be done with users, devices, interfaces, maps and vertices
722 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
723  
724 This method will typically trigger a network connection, depending on caching.
725
726 =item interfaces
727  
728 returns all interfaces
729
730 my %interfaces = $intermapper->interfaces();
731 my $interface = $devices{"UniqueInterfaceID"};
732 print $interface->Address;
733  
734 The returned hash contains instances of L<Net::Intermapper::Interface>, using the interface ID (generated by Intermapper) as the hash key. In scalar context, will return hashref. Modify the in-memory users dataset:
735  
736 my $interfaces = { "UniqueKey1" => $iface1, "UniqueKey2" => $iface2 };
737 $intermapper->interfaces($interfaces);
738 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
739 # But it can be done with users, devices, interfaces, maps and vertices
740 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
741  
742 This method will typically trigger a network connection, depending on caching.
743
744 =item vertices
745  
746 returns all vertices
747
748 my %vertices = $intermapper->vertices();
749 my $vertice = $vertices{"ID"}; # Unique Vertex ID
750 print $vertice->Shape;
751  
752 The returned hash contains instances of L<Net::Intermapper::Vertice>, using the vertex ID as the hash key. In scalar context, will return hashref. Modify the in-memory users dataset:
753  
754 my $vertices = { "UniqueVertexID1" => $vid1, "UniqueVertexID2" => $vid2 };
755 $intermapper->vertices($vertices);
756 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
757 # But it can be done with users, devices, interfaces, maps and vertices
758 # Pass a hashref to each method. This will NOT affect the smart-caching (only explicit calls to create, update and delete do this - for now).
759  
760 This method will typically trigger a network connection, depending on caching.
761
762 =item create
763  
764 This method created a new entry in Intermapper, depending on the argument passed. Record type is detected automatically.
765  
766 $intermapper->username("admin");
767 $intermapper->password("nmsadmin");
768 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
769 my $response = $intermapper->create($user);
770 # Error checking needs to be added
771 # print $Net::Intermapper::ERROR unless $id;
772 # $Net::Intermapper::ERROR contains details about failure
773  
774 # Add more examples
775 # Interfaces and maps cannot be explicitly created!
776
777 =item update
778  
779 This method updates an existing entry in Intermapper, depending on the argument passed. Record type is detected automatically.
780  
781 my $user = $intermapper->users->{"testuser"};
782 $user->Password("TopSecret"); # Change password. Password policies will be enforced!
783 my $response = $intermapper->update($user);
784 # Error checking needs to be added
785 # Update user based on Net::Intermapper::User instance
786 print $Net::Intermapper::ERROR unless $id;
787 # $Net::Intermapper::ERROR contains details about failure
788  
789 =item delete
790  
791 This method deletes an existing entry in Intermapper, depending on the argument passed. Record type is detected automatically.
792  
793 my $user = $intermapper->users->{"bob"};
794 my $response = $intermapper->delete($user);
795 # Delete existing user
796  
797 my $device = $intermapper->devices->{"MAIN_Router"}; # Key in this hash needs to be fixed! Non-unique!!
798 $intermapper->delete($device);
799 # Delete existing device
800
801 =item $ERROR
802  
803 NEEDS TO BE ADDED
804  
805 This variable will contain detailed error information.
806
807 =back
808  
809 =head1 REQUIREMENTS
810  
811 For this library to work, you need an instance with Intermapper (obviously) or a simulator like L<Net::Intermapper::Mock>.
812  
813 =over 3
814  
815 =item L<Moose>
816  
817 =item L<IO::Socket::SSL>
818  
819 =item L<LWP::UserAgent>
820  
821 =item L<XML::Simple>
822  
823 =item L<MIME::Base64>
824  
825 =item L<URI::Escape>
826  
827 =back
828
829 =head1 BUGS
830  
831 None so far
832  
833 =head1 TODO
834  
835 =over 3
836  
837 =item Filtering should be added (match= keyword in Intermapper documentation)
838  
839 =item XML input and output needs to be completed!
840  
841 =item $ERROR variable needs to actually contain error message!
842  
843 =back
844  
845 =head1 SUPPORT
846  
847 None so far :)
848  
849 =head1 AUTHOR
850  
851 Hendrik Van Belleghem
852 CPAN ID: BEATNIK
853 hendrik.vanbelleghem@gmail.com
854  
855 =head1 COPYRIGHT
856  
857 This program is free software licensed under the...
858  
859 The General Public License (GPL)
860 Version 2, June 1991
861  
862 The full text of the license can be found in the
863 LICENSE file included with this module.
864  
865  
866 =head1 SEE ALSO
867  
868 L<http://download.intermapper.com/docs/UserGuide/Content/09-Reference/09-05-Advanced_Importing/the_directive_line.htm>
869 L<http://download.intermapper.com/schema/imserverschema.html>
870  
871 =cut
872  
873 #################### main pod documentation end ###################
874  
875 1 __PACKAGE__->meta->make_immutable();
876  
877 1 1;
878 # The preceding line will help the module return a true value
879  
« blib/lib/Net/Intermapper/Device.pm ↑ Summary blib/lib/Net/Intermapper/User.pm »