File Coverage

blib/lib/Net/Intermapper/User.pm
Criterion Covered Total %
statement 18 104 17.3
branch 0 62 0.0
condition 0 18 0.0
subroutine 5 9 55.5
pod 4 4 100.0
total 27 197 13.7


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