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.03';
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 and 6.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
220 # Each class will generate specific header. These are typically only for internal use but are compliant to the import format Intermapper uses.
221 print $user->header;
222 print $device->header;
223 print $map->header;
224 print $interface->header;
225 print $vertice->header;
226  
227 print $user->toTAB;
228 print $device->toXML; # This one is broken still!
229 print $map->toCSV;
230 # Works on ALL subclasses
231 # Produce human-readable output of each record in the formats Intermapper supports
232
233 my $user = Net::Intermapper::User->new(Name=>"testuser", Password=>"Test12345");
234 my $response = $intermapper->create($user);
235 # Create new user
236 # Return value is HTTP::Response object
237
238 my $device = Net::Intermapper::Device->new(Name=>"testDevice", MapName=>"TestMap", MapPath=>"/TestMap", Address=>"10.0.0.1");
239 my $response = $intermapper->create($device);
240 # Create new device
241 # Return value is HTTP::Response object
242  
243 $user->Password("Foobar123");
244 my $response = $intermapper->update($user);
245 # Update existing user
246 # Return value is HTTP::Response object
247  
248 my $user = $intermapper->users->{"bob"};
249 my $response = $intermapper->delete($user);
250 # Delete existing user
251 # Return value is HTTP::Response object
252  
253 my $device = $intermapper->devices->{"UniqueDeviceID"};
254 my $response = $intermapper->delete($device);
255 # Delete existing device
256 # Return value is HTTP::Response object
257  
258 my $users = { "Tom" => $tom_user, "Bob" => $bob_user };
259 $intermapper->users($users);
260 # At this point, there is no real reason to do this as update, create and delete work with explicit arguments.
261 # But it can be done with users, devices, interfaces, maps and vertices
262 # 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).
263
264 =head1 DESCRIPTION
265  
266 Net::Intermapper::User is a perl wrapper around the HelpSystems Intermapper API provided through HTTP/HTTPS for access to user information.
267  
268 All calls are handled through an instance of the L<Net::Intermapper> class.
269  
270 use Net::Intermapper;
271 my $intermapper = Net::Intermapper->new(hostname => '10.0.0.1', username => 'admin', password => 'nmsadmin');
272  
273 =head1 USAGE
274
275 =over 3
276  
277 =item new
278  
279 Class constructor. Returns object of Net::Intermapper::User on succes. Attributes are:
280  
281 =over 5
282  
283 =item Id (read-only)
284  
285 A unique, persistent identifier for this user.
286  
287 =item Name (read-write)
288  
289 Login name of the user. This value is used for lookups in the C<users> method in L<Net::Intermapper>.
290  
291 =item Password (read-write)
292  
293 If the user is to be validated locally, the user's password.
294  
295 =item Guest (read-write)
296  
297 The user's autologin properties.
298  
299 =item External (read-write)
300  
301 Indicates user is to be validated by an auth server.
302  
303 =item Groups (read-write)
304  
305 Comma-separated list of groups to which to add user. Values are ordered alphabetically.
306 Intermapper does not support removing an existing group but does support deleting a user.
307
308 my $user = $intermapper->users->{"bob"};
309 my $groups = $user->Groups;
310 my @groups = split(/\,/,$groups);
311 shift @groups; # Remove first, just as an example
312 $groups = join(",",@groups);
313 $intermapper->delete($user);
314 $user->groups($groups);
315 $intermapper->create($user);
316  
317 =back
318  
319 =over 3
320  
321 =item header
322  
323 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.
324  
325 =back
326  
327 =over 3
328  
329 =item toTAB
330  
331 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.
332  
333 =back
334  
335 =over 3
336  
337 =item toCSV
338  
339 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.
340  
341 =back
342  
343 =over 3
344  
345 =item toXML
346  
347 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.
348  
349 =back
350  
351 =over 3
352  
353 =item mode
354  
355 Internal method to properly format the data and header for CRUD actions. Typically not used.
356  
357 =back
358  
359 =item $ERROR
360  
361 NEEDS TO BE ADDED
362  
363 This variable will contain detailed error information.
364
365 =back
366  
367 =head1 REQUIREMENTS
368  
369 For this library to work, you need an instance with Intermapper (obviously) or a simulator like L<Net::Intermapper::Mock>.
370  
371 =over 3
372  
373 =item L<Moose>
374  
375 =item L<IO::Socket::SSL>
376  
377 =item L<LWP::UserAgent>
378  
379 =item L<XML::Simple>
380  
381 =item L<MIME::Base64>
382  
383 =item L<URI::Escape>
384  
385 =item L<Text::CSV_XS>
386  
387 =back
388
389 =head1 BUGS
390  
391 None so far
392  
393 =head1 SUPPORT
394  
395 None so far :)
396  
397 =head1 AUTHOR
398  
399 Hendrik Van Belleghem
400 CPAN ID: BEATNIK
401 hendrik.vanbelleghem@gmail.com
402  
403 =head1 COPYRIGHT
404  
405 This program is free software licensed under the...
406  
407 The General Public License (GPL)
408 Version 2, June 1991
409  
410 The full text of the license can be found in the
411 LICENSE file included with this module.
412  
413  
414 =head1 SEE ALSO
415  
416 L<http://download.intermapper.com/docs/UserGuide/Content/09-Reference/09-05-Advanced_Importing/the_directive_line.htm>
417 L<http://download.intermapper.com/schema/imserverschema.html>
418  
419 =cut
420  
421 #################### main pod documentation end ###################
422  
423 1 __PACKAGE__->meta->make_immutable();
424  
425 1 1;
426 # The preceding line will help the module return a true value
427  
« blib/lib/Net/Intermapper.pm ↑ Summary blib/lib/Net/Intermapper/Vertice.pm »