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.03';
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 and 6.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
530 # Each class will generate specific header. These are typically only for internal use but are compliant to the import format Intermapper uses.
531 print $user->header;
532 print $device->header;
533 print $map->header;
534 print $interface->header;
535 print $vertice->header;
536  
537 print $user->toTAB;
538 print $device->toXML; # This one is broken still!
539 print $map->toCSV;
540 # Works on ALL subclasses
541 # Produce human-readable output of each record in the formats Intermapper supports
542
543 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
544 my $response = $intermapper->create($user);
545 # Create new user
546 # Return value is HTTP::Response object
547
548 my $device = Net::Intermapper::Device->new(Name=>"testDevice", MapName=>"TestMap", MapPath=>"/TestMap", Address=>"10.0.0.1");
549 my $response = $intermapper->create($device);
550 # Create new device
551 # Return value is HTTP::Response object
552  
553 $user->Password("Foobar123");
554 my $response = $intermapper->update($user);
555 # Update existing user
556 # Return value is HTTP::Response object
557  
558 my $user = $intermapper->users->{"bob"};
559 my $response = $intermapper->delete($user);
560 # Delete existing user
561 # Return value is HTTP::Response object
562  
563 my $device = $intermapper->devices->{"UniqueDeviceID"};
564 my $response = $intermapper->delete($device);
565 # Delete existing device
566 # Return value is HTTP::Response object
567  
568 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
569 $intermapper->users($users);
570 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
571 # But it can be done with users, devices, interfaces, maps and vertices
572 # 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).
573
574 =head1 DESCRIPTION
575  
576 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.
577  
578 All calls are handled through an instance of the L<Net::Intermapper> class.
579  
580 use Net::Intermapper;
581 my $intermapper = Net::Intermapper->new(hostname => '10.0.0.1', username => 'admin', password => 'nmsadmin');
582  
583 =over 3
584  
585 =item new
586  
587 Class constructor. Returns object of Net::Intermapper on succes. Required fields are:
588  
589 =over 5
590  
591 =item hostname
592  
593 =item username
594  
595 =item password
596  
597 =back
598  
599 Optional fields are
600  
601 =over 5
602  
603 =item ssl
604  
605 =item ssl_options
606  
607 =item port
608  
609 =item modifyport
610  
611 =item cache
612  
613 =back
614  
615 =item hostname
616  
617 IP or hostname of Intermapper server. This is a required value in the constructor but can be redefined afterwards.
618  
619 =item username
620  
621 Username of Administrator user. This is a required value in the constructor but can be redefined afterwards.
622 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.
623 This value is not automatically changed when running C<create> or C<update>.
624  
625 =item password
626  
627 Password of user. This is a required value in the constructor but can be redefined afterwards.
628 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.
629 This value is not automatically changed when running C<create> or C<update>.
630  
631 =item ssl
632  
633 SSL enabled (1 - default) or disabled (0).
634  
635 =item ssl_options
636  
637 Value is passed directly to L<LWP::UserAgent> as C<ssl_opt>. Default value (hash-ref) is
638  
639 { 'SSL_verify_mode' => SSL_VERIFY_NONE, 'verify_hostname' => '0' }
640  
641 This is an optional value in the constructor and can be redefined afterwards.
642
643 =item port
644  
645 TCP port used for queries. This is an optional value in the constructor and can be redefined afterwards. By default, this is set to 8181.
646 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.
647  
648 =item modifyport
649  
650 TCP port used for modifying values. This is an optional value in the constructor and can be redefined afterwards. By default, this is set to 443.
651 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.
652  
653 =item cache
654  
655 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.
656  
657 $intermapper->cache(0);
658 $intermapper->update($user);
659 # Users have changed.
660 my $users = $intermapper->users;
661 # This will trigger network traffic
662 my $devices = $intermapper->devices
663 # This will NOT trigger network traffic
664  
665 =back
666  
667 From the class instance, call the different methods for retrieving values.
668  
669 =over 3
670  
671 =item users
672  
673 Returns all users
674  
675 my %users = $intermapper->users();
676 my $user = $users{"Bob"};
677 print $user->Password;
678
679 The returned hash contains instances of L<Net::Intermapper::User>, using the username as the hash key. In scalar context, will return hashref. This method will typically trigger a network connection, depending on caching.
680  
681 Modify the in-memory users dataset:
682  
683 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
684 $intermapper->users($users);
685 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
686 # But it can be done with users, devices, interfaces, maps and vertices
687 # 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).
688
689 =item devices
690  
691 returns all devices
692
693 my %devices = $intermapper->devices();
694 my $device = $devices{"UniqueDeviceID"};
695 print $device->Address;
696  
697 The returned hash contains instances of L<Net::Intermapper::Device>, using the device ID as the hash key. In scalar context, will return hashref. This method will typically trigger a network connection, depending on caching.
698  
699 Modify the in-memory users dataset:
700  
701 my $devices = { "MainRouter1" => $main1, "MainRouter2" => $main2 };
702 $intermapper->devices($devices);
703 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
704 # But it can be done with users, devices, interfaces, maps and vertices
705 # 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).
706
707 =item maps
708  
709 returns all maps
710
711 my %maps = $intermapper->maps();
712 my $map = $maps{"MainMap"};
713 print $map->Name;
714  
715 The returned hash contains instances of L<Net::Intermapper::Map>, using the map name as the hash key. In scalar context, will return hashref. This method will typically trigger a network connection, depending on caching.
716  
717 Modify the in-memory users dataset:
718  
719 my $maps = { "MainMap" => $main1, "Layer2" => $main2 };
720 $intermapper->maps($maps);
721 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
722 # But it can be done with users, devices, interfaces, maps and vertices
723 # 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).
724
725 =item interfaces
726  
727 returns all interfaces
728
729 my %interfaces = $intermapper->interfaces();
730 my $interface = $devices{"UniqueInterfaceID"};
731 print $interface->Address;
732  
733 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. This method will typically trigger a network connection, depending on caching.
734  
735 Modify the in-memory users dataset:
736  
737 my $interfaces = { "UniqueKey1" => $iface1, "UniqueKey2" => $iface2 };
738 $intermapper->interfaces($interfaces);
739 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
740 # But it can be done with users, devices, interfaces, maps and vertices
741 # 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).
742
743 =item vertices
744  
745 returns all vertices
746
747 my %vertices = $intermapper->vertices();
748 my $vertice = $vertices{"ID"}; # Unique Vertex ID
749 print $vertice->Shape;
750  
751 The returned hash contains instances of L<Net::Intermapper::Vertice>, using the vertex ID as the hash key. In scalar context, will return hashref. This method will typically trigger a network connection, depending on caching.
752  
753 Modify the in-memory users dataset:
754  
755 my $vertices = { "UniqueVertexID1" => $vid1, "UniqueVertexID2" => $vid2 };
756 $intermapper->vertices($vertices);
757 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
758 # But it can be done with users, devices, interfaces, maps and vertices
759 # 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).
760
761 =item create
762  
763 This method created a new entry in Intermapper, depending on the argument passed. Record type is detected automatically.
764  
765 $intermapper->username("admin");
766 $intermapper->password("nmsadmin");
767 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
768 my $response = $intermapper->create($user);
769 # Error checking needs to be added
770 # print $Net::Intermapper::ERROR unless $id;
771 # $Net::Intermapper::ERROR contains details about failure
772  
773 # Add more examples
774 # Interfaces and maps cannot be explicitly created!
775
776 =item update
777  
778 This method updates an existing entry in Intermapper, depending on the argument passed. Record type is detected automatically.
779  
780 my $user = $intermapper->users->{"testuser"};
781 $user->Password("TopSecret"); # Change password. Password policies will be enforced!
782 my $response = $intermapper->update($user);
783 # Error checking needs to be added
784 # Update user based on Net::Intermapper::User instance
785 # print $Net::Intermapper::ERROR unless $id;
786 # $Net::Intermapper::ERROR contains details about failure
787  
788 =item delete
789  
790 This method deletes an existing entry in Intermapper, depending on the argument passed. Record type is detected automatically.
791  
792 my $user = $intermapper->users->{"bob"};
793 my $response = $intermapper->delete($user);
794 # Delete existing user
795  
796 my $device = $intermapper->devices->{"UniqueDeviceID"}; # This key is generated by Intermapper
797 $intermapper->delete($device);
798 # Delete existing device
799
800 =item $ERROR
801  
802 NEEDS TO BE ADDED
803  
804 This variable will contain detailed error information.
805
806 =back
807  
808 =head1 REQUIREMENTS
809  
810 For this library to work, you need an instance with Intermapper (obviously) or a simulator like L<Net::Intermapper::Mock>.
811  
812 =over 3
813  
814 =item L<Moose>
815  
816 =item L<IO::Socket::SSL>
817  
818 =item L<LWP::UserAgent>
819  
820 =item L<XML::Simple>
821  
822 =item L<MIME::Base64>
823  
824 =item L<URI::Escape>
825  
826 =item L<Text::CSV_XS>
827  
828 =back
829
830 =head1 BUGS
831  
832 None so far
833  
834 =head1 TODO
835  
836 =over 3
837  
838 =item Filtering should be added (match= keyword in Intermapper documentation)
839  
840 =item XML input and output needs to be completed!
841  
842 =item $ERROR variable needs to actually contain error message!
843  
844 =back
845  
846 =head1 SUPPORT
847  
848 None so far :)
849  
850 =head1 AUTHOR
851  
852 Hendrik Van Belleghem
853 CPAN ID: BEATNIK
854 hendrik.vanbelleghem@gmail.com
855  
856 =head1 COPYRIGHT
857  
858 This program is free software licensed under the...
859  
860 The General Public License (GPL)
861 Version 2, June 1991
862  
863 The full text of the license can be found in the
864 LICENSE file included with this module.
865  
866  
867 =head1 SEE ALSO
868  
869 L<http://download.intermapper.com/docs/UserGuide/Content/09-Reference/09-05-Advanced_Importing/the_directive_line.htm>
870 L<http://download.intermapper.com/schema/imserverschema.html>
871  
872 =cut
873  
874 #################### main pod documentation end ###################
875  
876 1 __PACKAGE__->meta->make_immutable();
877  
878 1 1;
879 # The preceding line will help the module return a true value
880  
« blib/lib/Net/Intermapper/Device.pm ↑ Summary blib/lib/Net/Intermapper/User.pm »