blib/lib/Net/Intermapper/User.pm

filter: Statementstmt 23.2% 27 / 116 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 33.3% 5 / 15 Podpod 100% 4 / 4 totaltotal 16.5% 36 / 218 CC 44 SCAR 49.5
CC
44
Cov
14%
toTAB
210.0
header
210.0
toCSV
156.0
CRAP 140.7
« blib/lib/Net/Intermapper.pm ↑ Summary blib/lib/Net/Intermapper/Vertice.pm »
1 package Net::Intermapper::User;
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 BEGIN {
6 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @HEADERS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ $VERSION = '0.02';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw(Exporter);
10 1 @EXPORT = qw();
11 1 @EXPORT_OK = qw();
12 1 %EXPORT_TAGS = ();
13
14 1 @HEADERS = qw(Id Name Password External Guest Groups);
15 };
16  
17 # MOOSE!
18
19 has 'Id' => (
20 is => 'rw',
21 isa => 'Str',
22 0 ▶ default => sub { ""; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 1 );
24  
25 has 'Groups' => (
26 is => 'rw',
27 isa => 'Str',
28 0 ▶ default => sub { "Read_Only"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 1 );
30  
31 1 has 'Name' => (
32 is => 'rw',
33 isa => 'Str',
34 );
35  
36 has 'Password' => (
37 is => 'rw',
38 isa => 'Str',
39 0 ▶ default => sub { "�"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 );
41  
42 has 'Guest' => (
43 is => 'rw',
44 isa => 'Str',
45 0 ▶ default => sub { "false"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 1 );
47  
48 has 'External' => (
49 is => 'rw',
50 isa => 'Str',
51 0 ▶ default => sub { ""; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 1 );
53  
54 has 'mode' => ( # create, update, delete
55 is => 'rw',
56 isa => 'Str',
57 0 ▶ default => sub { "create"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 1 );
59
60 # No Moose
61
62 sub toXML
63 0 ▶{ my $self = shift;
Subroutine toXML: not called CC=7 SCAR=40.3
Pod documented
64 0 my $id = $self->Id;
65 0 ▶ my $name = $self->Name || "";
Condition0%0/2
$self->Name || ''
l!l
00
MC/DC0%0/1
$self->Name || ''
$self->Name
Truth table0%0/2
A
A= $self->Name
Aresult
11
00
66 0 ▶ my $groups = $self->Groups || "Read_only";
Condition0%0/2
$self->Groups || 'Read_only'
l!l
00
MC/DC0%0/1
$self->Groups || 'Read_only'
$self->Groups
Truth table0%0/2
A
A= $self->Groups
Aresult
11
00
67 0 ▶ my $external = $self->External || "";
Condition0%0/2
$self->External || ''
l!l
00
MC/DC0%0/1
$self->External || ''
$self->External
Truth table0%0/2
A
A= $self->External
Aresult
11
00
68 0 ▶ my $guest = $self->Guest || "true";
Condition0%0/2
$self->Guest || 'true'
l!l
00
MC/DC0%0/1
$self->Guest || 'true'
$self->Guest
Truth table0%0/2
A
A= $self->Guest
Aresult
11
00
69 0 ▶ my $password = $self->Password || "�";
Condition0%0/2
$self->Password || '�'
l!l
00
MC/DC0%0/1
$self->Password || '�'
$self->Password
Truth table0%0/2
A
A= $self->Password
Aresult
11
00
70 0 my $result = "";
71 # Need to build the XML formatting!!
72 0 ▶ if ($id) { $result = " <id>$id</id>\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
73 0 return $result;
74 }
75  
76 sub toCSV
77 0 ▶{ my $self = shift;
Subroutine toCSV: not called CC=12 SCAR=50.5
Pod documented
78 0 my $id = $self->Id;
79 0 my $result = "";
80 0 my @attributes = $self->meta->get_all_attributes;
81 0 my %attributes = ();
82 0 for my $attribute (@attributes)
83 0 ▶ { $attributes{$attribute->name} = $attribute->get_value($self) || "";
Condition0%0/2
$attribute->get_value($self) || ''
l!l
00
MC/DC0%0/1
$attribute->get_value($self) || ''
$attribute->get_value($self)
Truth table0%0/2
A
A= $attribute->get_value($self)
Aresult
11
00
84 }
85 0 for my $key (@HEADERS)
86 0 ▶ { if ($self->mode eq "create")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "create") 0 0 0
87 0 ▶ { next if $key eq "Id";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Id" 0 0 0
88 0 ▶ next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
89 0 $result .= $attributes{$key}.",";
90 }
91 0 ▶ if ($self->mode eq "update")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "update") 0 0 0
92 0 ▶ { next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
93 0 $result .= $attributes{$key}.",";
94 }
95 0 ▶ if ($self->mode eq "delete")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "delete") 0 0 0
96 0 ▶ { next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
97 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
98 0 $result .= $attributes{$key}.",";
99 }
100
101 }
102 0 chop $result; # Remove the comma of the last field
103 0 $result =~ s/\s$//g;
104 0 $result .= "\r\n";
105 0 return $result;
106 }
107  
108 sub toTAB
109 0 ▶{ my $self = shift;
Subroutine toTAB: not called CC=14 SCAR=53.5
Pod documented
110 0 my $id = $self->Id;
111 0 my $result = "";
112 0 my @attributes = $self->meta->get_all_attributes;
113 0 my %attributes = ();
114 0 for my $attribute (@attributes)
115 0 ▶ { $attributes{$attribute->name} = $attribute->get_value($self) || "";
Condition0%0/2
$attribute->get_value($self) || ''
l!l
00
MC/DC0%0/1
$attribute->get_value($self) || ''
$attribute->get_value($self)
Truth table0%0/2
A
A= $attribute->get_value($self)
Aresult
11
00
116 }
117 0 for my $key (@HEADERS)
118 0 ▶ { if ($self->mode eq "create")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "create") 0 0 0
119 0 ▶ { next if $key eq "Id";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Id" 0 0 0
120 0 ▶ next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
121 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
122 0 $result .= $attributes{$key}."\t";
123 }
124 0 ▶ if ($self->mode eq "update")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "update") 0 0 0
125 0 ▶ { next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
126 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
127 0 $result .= $attributes{$key}."\t";
128 }
129 0 ▶ if ($self->mode eq "delete")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "delete") 0 0 0
130 0 ▶ { next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
131 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
132 0 $result .= $attributes{$key}."\t";
133 }
134  
135 }
136 0 chop $result; # Remove the comma of the last field
137 0 $result =~ s/\s$//g;
138 0 $result .= "\r\n";
139 0 return $result;
140 }
141  
142 sub header
143 0 ▶{ my $self = shift;
Subroutine header: not called CC=14 SCAR=53.5
Pod documented
144 0 ▶ my $format = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
145 0 my $header = "# format=$format table=users fields=";
146 0 my @attributes = $self->meta->get_all_attributes;
147 0 my %attributes = ();
148 0 for my $attribute (@attributes)
149 0 ▶ { $attributes{$attribute->name} = $attribute->get_value($self) || "";
Condition0%0/2
$attribute->get_value($self) || ''
l!l
00
MC/DC0%0/1
$attribute->get_value($self) || ''
$attribute->get_value($self)
Truth table0%0/2
A
A= $attribute->get_value($self)
Aresult
11
00
150 }
151 0 for my $key (@HEADERS)
152 0 ▶ { if ($self->mode eq "create")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "create") 0 0 0
153 0 ▶ { next if $key eq "Id";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Id" 0 0 0
154 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
155 0 $header .= $key.",";
156 }
157 0 ▶ if ($self->mode eq "update")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "update") 0 0 0
158 0 ▶ { next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
159 0 $header .= $key.",";
160 }
161 0 ▶ if ($self->mode eq "delete")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "delete") 0 0 0
162 0 ▶ { next if $key eq "mode";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mode" 0 0 0
163 0 ▶ next unless $attributes{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes{$key} 0 0 0
164 0 $header .= $key.",";
165 }
166 }
167 0 ▶ if ($self->mode eq "delete")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "delete") 0 0 0
168 0 { $header .= " delete=Id,Name "; } # These 3 fields are used for filtering
169 0 chop $header;
170 0 $header .= "\r\n";
171 0 return $header;
172 }
173
174 =pod
175  
176 =head1 NAME
177  
178 Net::Intermapper::User - Interface with the HelpSystems Intermapper HTTP API - Users
179  
180 =head1 SYNOPSIS
181  
182 use Net::Intermapper;
183 my $intermapper = Net::Intermapper->new(hostname=>"10.0.0.1", username=>"admin", password=>"nmsadmin");
184 # Options:
185 # hostname - IP or hostname of Intermapper 5.x server
186 # username - Username of Administrator user
187 # password - Password of user
188 # ssl - SSL enabled (1 - default) or disabled (0)
189 # port - TCP port for querying information. Defaults to 8181
190 # modifyport - TCP port for modifying information. Default to 443
191 # cache - Boolean to enable smart caching or force network queries
192  
193 my %users = $intermapper->users;
194 my $users_ref = $intermapper->users;
195 # Retrieve all users from Intermapper, Net::Intermapper::User instances
196 # Returns hash or hashref, depending on context
197
198 my %devices = $intermapper->devices;
199 my $devices_ref = $intermapper->devices;
200 # Retrieve all devices from Intermapper, Net::Intermapper::Device instances
201 # Returns hash or hashref, depending on context
202  
203 my %maps = $intermapper->maps;
204 my $maps_ref = $intermapper->maps;
205 # Retrieve all maps from Intermapper, Net::Intermapper::Map instances
206 # Returns hash or hashref, depending on context
207  
208 my %interfaces = $intermapper->interfaces;
209 my $interfaces_ref = $intermapper->interfaces;
210 # Retrieve all interfaces from Intermapper, Net::Intermapper::Interface instances
211 # Returns hash or hashref, depending on context
212  
213 my %vertices = $intermapper->vertices;
214 my $vertices_ref = $intermapper->vertices;
215 # Retrieve all vertices from Intermapper, Net::Intermapper::Vertice instances
216 # Returns hash or hashref, depending on context
217  
218 my $user = $intermapper->users->{"admin"};
219 print $user->header;
220 print $device->header;
221 print $map->header;
222 print $interface->header;
223 print $vertice->header;
224 # Generate 'directive' needed for manipulation. Mostly used internally.
225 # $user->mode("create"); # This changes the header fields
226 # $user->mode("update"); # This also changes the header fields
227 # Both are NOT needed when adding or updating a record
228 print $user->toTAB;
229 print $device->toXML;
230 print $map->toCSV;
231 # Works on ALL classes
232 # Produce human-readable output of each record.
233
234 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
235 my $response = $intermapper->create($user);
236 # Create new user
237 # Return value is HTTP::Response object
238
239 my $device = Net::Intermapper::Device->new(Name=>"testDevice", MapName=>"TestMap", MapPath=>"/TestMap", Address=>"10.0.0.1");
240 my $response = $intermapper->create($device);
241 # Create new device
242 # Return value is HTTP::Response object
243  
244 $user->Password("Foobar123");
245 my $response = $intermapper->update($user);
246 # Update existing user
247 # Return value is HTTP::Response object
248  
249 my $user = $intermapper->users->{"bob"};
250 my $response = $intermapper->delete($user);
251 # Delete existing user
252 # Return value is HTTP::Response object
253  
254 my $device = $intermapper->devices->{"UniqueDeviceID"};
255 my $response = $intermapper->delete($device);
256 # Delete existing device
257 # Return value is HTTP::Response object
258  
259 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
260 $intermapper->users($users);
261 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
262 # But it can be done with users, devices, interfaces, maps and vertices
263 # 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).
264
265 =head1 DESCRIPTION
266  
267 Net::Intermapper::User is a perl wrapper around the HelpSystems Intermapper API provided through HTTP/HTTPS for access to user information.
268  
269 All calls are handled through an instance of the L<Net::Intermapper> class.
270  
271 use Net::Intermapper;
272 my $intermapper = Net::Intermapper->new(hostname => '10.0.0.1', username => 'admin', password => 'nmsadmin');
273  
274 =head1 USAGE
275
276 =over 3
277  
278 =item new
279  
280 Class constructor. Returns object of Net::Intermapper::User on succes. Attributes are:
281  
282 =over 5
283  
284 =item Id (read-only)
285  
286 A unique, persistent identifier for this user.
287  
288 =item Name (read-write)
289  
290 Login name of the user. This value is used for lookups in the C<users> method in L<Net::Intermapper>.
291  
292 =item Password (read-write)
293  
294 If the user is to be validated locally, the user's password.
295  
296 =item Guest (read-write)
297  
298 The user's autologin properties.
299  
300 =item External (read-write)
301  
302 Indicates user is to be validated by an auth server.
303  
304 =item Groups (read-write)
305  
306 Comma-separated list of groups to which to add user. Values are ordered alphabetically.
307 Intermapper does not support removing an existing group but does support deleting a user.
308
309 my $user = $intermapper->users->{"bob"};
310 my $groups = $user->Groups;
311 my @groups = split(/\,/,$groups);
312 shift @groups; # Remove first, just as an example
313 $groups = join(",",@groups);
314 $intermapper->delete($user);
315 $user->groups($groups);
316 $intermapper->create($user);
317  
318 =back
319  
320 =over 3
321  
322 =item header
323  
324 Returns the C<directive> aka data header required by the Intermapper API to perform CRUD actions. This is handled through the C<create>, C<update> and C<delete> method and should not really be used.
325  
326 =back
327  
328 =over 3
329  
330 =item toTAB
331  
332 Returns the object data formatted in TAB delimited format. Used in combination with the C<header> and the C<format> method in L<Net::Intermapper> to perform CRUD actions. This is handled through the C<create>, C<update> and C<delete> method and should not really be used.
333  
334 =back
335  
336 =over 3
337  
338 =item toCSV
339  
340 Returns the object data formatted in Comma Separated delimited format. Used in combination with the C<header> and the C<format> method in L<Net::Intermapper> to perform CRUD actions. This is handled through the C<create>, C<update> and C<delete> method and should not really be used.
341  
342 =back
343  
344 =over 3
345  
346 =item toXML
347  
348 Returns the object data formatted in XML format. Used in combination with the C<header> and the C<format> method in L<Net::Intermapper> to perform CRUD actions. This is handled through the C<create>, C<update> and C<delete> method and should not really be used.
349  
350 =back
351  
352 =over 3
353  
354 =item mode
355  
356 Internal method to properly format the data and header for CRUD actions. Typically not used.
357  
358 =back
359  
360 =item $ERROR
361  
362 NEEDS TO BE ADDED
363  
364 This variable will contain detailed error information.
365
366 =back
367  
368 =head1 REQUIREMENTS
369  
370 For this library to work, you need an instance with Intermapper (obviously) or a simulator like L<Net::Intermapper::Mock>.
371  
372 =over 3
373  
374 =item L<Moose>
375  
376 =item L<IO::Socket::SSL>
377  
378 =item L<LWP::UserAgent>
379  
380 =item L<XML::Simple>
381  
382 =item L<MIME::Base64>
383  
384 =item L<URI::Escape>
385  
386 =back
387
388 =head1 BUGS
389  
390 None so far
391  
392 =head1 SUPPORT
393  
394 None so far :)
395  
396 =head1 AUTHOR
397  
398 Hendrik Van Belleghem
399 CPAN ID: BEATNIK
400 hendrik.vanbelleghem@gmail.com
401  
402 =head1 COPYRIGHT
403  
404 This program is free software licensed under the...
405  
406 The General Public License (GPL)
407 Version 2, June 1991
408  
409 The full text of the license can be found in the
410 LICENSE file included with this module.
411  
412  
413 =head1 SEE ALSO
414  
415 L<http://download.intermapper.com/docs/UserGuide/Content/09-Reference/09-05-Advanced_Importing/the_directive_line.htm>
416 L<http://download.intermapper.com/schema/imserverschema.html>
417  
418 =cut
419  
420 #################### main pod documentation end ###################
421  
422 1 __PACKAGE__->meta->make_immutable();
423  
424 1 1;
425 # The preceding line will help the module return a true value
426  
« blib/lib/Net/Intermapper.pm ↑ Summary blib/lib/Net/Intermapper/Vertice.pm »