File Coverage

File:/home/mik/work/module/Tivoli/AccessManager/Admin/Server.pm
Coverage:98.8%

linestmtbrancondsubpodtimecode
1package Tivoli::AccessManager::Admin::Server;
2
15
15
15
165
51
245
use strict;
3
15
15
15
209
67
220
use warnings;
4
15
15
15
208
54
279
use Carp;
5
15
15
15
209
51
251
use Data::Dumper;
6
15
15
15
188
52
238
use Tivoli::AccessManager::Admin::Response;
7
8#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
9# $Id: Server.pm 328 2006-11-20 19:00:06Z mik $
10#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
11$Tivoli::AccessManager::Admin::Server::VERSION = '0.00';
12
15
223
use Inline(C => 'DATA',
13                INC => '-I/opt/PolicyDirector/include',
14                LIBS => ' -lpthread -lpdadminapi -lpdmgrapi -lstdc++',
15                CCFLAGS => '-Wall',
16                NAME => 'Tivoli::AccessManager::Admin::Server',
17
15
15
189
61
           );
18
19sub new {
20
9
1
55
    my $class = shift;
21
9
35
    my $cont = shift;
22
9
42
    my $name = "";
23
9
189
    unless (defined($cont) and UNIVERSAL::isa($cont,'Tivoli::AccessManager::Admin::Context')) {
24
2
424
        warn "Incorrect syntax -- did you forget the context?\n";
25
2
34
        return undef;
26    }
27
28
7
60
    if (@_ == 1) {
29
2
10
        $name = shift;
30    }
31    elsif (@_ % 2) {
32
1
23
        warn "Invalid syntax for new\n";
33
1
9
        return undef;
34    }
35    elsif (@_) {
36
2
18
        my %opts = @_;
37
2
36
        $name = $opts{name} || '';
38    }
39
40
6
51
    my $self = bless {}, $class;
41
42
6
29
    $self->{context} = $cont;
43
6
32
    $self->{name} = $name;
44
45
6
37
    return $self;
46}
47
48sub tasklist {
49
3
1
29
    my $self = shift;
50
3
43
    my $resp = Tivoli::AccessManager::Admin::Response->new;
51
52
3
29
    unless ($self->{name}) {
53
1
7
        $resp->set_message("Unnamed servers cannot list tasks");
54
1
29
        $resp->set_isok(0);
55
1
9
        return $resp;
56    }
57
58
2
131321
    my $rc = $self->server_gettasklist($resp);
59
2
53
    $resp->isok() && $resp->set_value($rc);
60
61
2
23
    return $resp;
62}
63
64sub task {
65
7
1
54
    my $self = shift;
66
7
72
    my $resp = Tivoli::AccessManager::Admin::Response->new;
67
7
36
    my $task = '';
68
69
7
48
    unless ($self->{name}) {
70
1
11
        $resp->set_message("Unnamed servers cannot perform tasks");
71
1
10
        $resp->set_isok(0);
72
1
4
        return $resp;
73    }
74
75
6
63
    if (@_ == 1) {
76
2
11
        $task = shift;
77    }
78    elsif (@_ % 2) {
79
1
11
        $resp->set_message("Invalid syntax");
80
1
12
        $resp->set_isok(0);
81
1
9
        return $resp;
82    }
83    elsif (@_) {
84
2
17
        my %opts = @_;
85
2
43
        $task = $opts{task} || '';
86    }
87
88
5
22
    if ($task) {
89
3
252276
        my $rc = $self->server_performtask($resp,$task);
90
3
82
        if ($resp->isok) {
91
2
31
            my @temp = split("\n",$rc);
92
2
16
            for (@temp) {
93
8
26
                chomp;
94
8
42
                s/^\s*//;
95
8
82
                s/\s*$//;
96            }
97
2
21
            $resp->set_value(\@temp);
98        }
99    }
100    else {
101
2
20
        $resp->set_isok(0);
102
2
18
        $resp->set_message("Cannot perform a nameless task");
103    }
104
5
50
    return $resp;
105}
106
107# This is a kludge, because IBM won't expose the real calls via the C API.
108# They did for java, but not C. Bastards
109sub list {
110
4
1
26
    my $class = shift;
111
4
21
    my ($tam,$grp,@list);
112
4
46
    my $resp = Tivoli::AccessManager::Admin::Response->new();
113
114    # I want this to be called as either Tivoli::AccessManager::Admin::User->list or
115    # $self->list
116
4
31
    if ( ref($class) ) {
117
1
9
        $tam = $class->{context};
118    }
119    else {
120
3
11
        $tam = shift;
121
3
59
        unless (defined($tam) and UNIVERSAL::isa($tam,'Tivoli::AccessManager::Admin::Context' ) ) {
122
2
16
            $resp->set_message("Incorrect syntax -- did you forget the context?");
123
2
12
            $resp->set_isok(0);
124
2
10
            return $resp;
125        }
126    }
127
128
2
34
    $grp = Tivoli::AccessManager::Admin::Group->new($tam,name => 'remote-acl-users');
129
2
54
    $resp = $grp->members;
130
2
32
    return $resp unless $resp->isok;
131
132
2
28
    for ( $resp->value ) {
133
10
111
        next unless s#/#-#;
134
10
76
        push @list, $_;
135    }
136
137
2
34
    $resp = Tivoli::AccessManager::Admin::Response->new;
138
2
13
    $resp->set_value(\@list);
139
140
2
18
    return $resp;
141}
142
143sub name {
144
5
1
22
    my $self = shift;
145
5
45
    my $resp = Tivoli::AccessManager::Admin::Response->new;
146
5
20
    my $name = "";
147
148
5
52
    if (@_ == 1) {
149
1
8
        $name = shift;
150    }
151    elsif (@_ % 2) {
152
1
7
        $resp->set_message("Invalid syntax for name");
153
1
12
        $resp->set_isok(0);
154
1
9
        return $resp;
155    }
156    elsif (@_) {
157
2
26
        my %opts = @_;
158
2
39
        $name = $opts{name} || '';
159    }
160
161
4
33
    $self->{name} = $name if $name;
162
163
4
34
    $resp->set_value($self->{name});
164
4
22
    return $resp;
165}
166
167
1681;
169
170 - 336
=head1 NAME

Tivoli::AccessManager::Admin::Server

=head1 SYNOPSIS

  my $tam = Tivoli::AccessManager::Admin->new(password => 'N3ew0nk');
  my($server, $resp);

  # Lets see what servers are defined
  $resp = Tivoli::AccessManager::Admin::Server->list($tam);

  # Lets find a webSEAL
  my $wseal;
  for ($resp->value) {
      if (/webseal/) {
          $wseal = $_;
	  last;
      }
  }

  $server = Tivoli::AccessManager::Admin::Server->new($tam,$wseal);

  # Get a list of tasks from the webSEAL
  $resp = $server->tasklist;

  # Execute a task
  $resp = $server->task("list");

=head1 DESCRIPTION

L<Tivoli::AccessManager::Admin::Server> implements the server access portion
of the TAM API.  This basically means any pdadmin command that starts with the
word "server".

=head1 CONSTRUCTOR

=head2 new(PDADMIN[, NAME])

Creates a blessed L<Tivoli::AccessManager::Admin::Server> object.  As you may
well expect, you will need to destroy the object if you want to change the
context.

=head3 Parameters

=over 4

=item PDADMIN

An initialized L<Tivoli::AccessManager::Admin::Context> object.  This is the only required
parameter.

=item NAME

The servers's name.  This is technically not required, but you need to define
the name before you can use L</"tasklist"> or L</"task">.

=back

=head3 Returns

A blessed L<Tivoli::AccessManager::Admin::Server> as long as you provide a
valid context.  It will warn and return undef otherwise.

=head1 CLASS METHODS

=head2 list

Lists all servers.  This method is something of a hack.  TAM does not expose a
server list function to the C API.  This method actually uses the membership
list of the remote-acl-users group.  It isn't great, but it should work.

=head3 Parameters

None.

=head3 Returns

Hopefully, a list of all the defined servers buried in a
L<Tivoli::AccessManager::Admin::Response> object.  There may be some extra
values in there, but you shouldn't be adding your own stuff to
remove-acl-users anyway.  

=head1 METHODS

All methods return a L<Tivoli::AccessManager::Admin::Response> object.  See
the documentation for that module to get the actual values out.

=head2 task(COMMAND)

Executes the named command on the server.

=head3 Parameters

=over 4

=item COMMAND

The command to execute.  This parameter is required.

=back

=head3 Returns

An array containing the results of the command.  The API is a little weird in
this, but it makes some sense.  The API actually returns everything as one
string, separated by newlines.  I split this string on newlines to generate an
array.  It isn't pretty, but that is the way it works.

An invalid or missing command will generate an error.

=head2 tasklist

Gets a list of all the tasks defined on the server.

=head3 Parameters

None.

=head3 Returns

An array containing the output.  See the discussion in L</"task"> for more
information.

=head2 name([NAME])

Gets or sets the server's name.

=head3 Parameters

=over 4

=item NAME

The new name.  This parameter is optional.

=back

=head3 Returns

The name of the server, buried in a L<Tivoli::AccessManager::Admin::Response>
object.

=head1 ACKNOWLEDGEMENTS

See L<Tivoli::AccessManager::Admin> for the list.  This was not possible without the help of a
bunch of people smarter than I.

=head1 BUGS

I would really like a server list and server info function exposed to the C
API.  Like what Java has.  This isn't my bug.

=head1 AUTHOR

Mik Firestone E<lt>mikfire@gmail.comE<gt>

=head1 COPYRIGHT

Copyright (c) 2006-2013 Mik Firestone.  All rights reserved.  This program is
free software; you can redistibute it and/or modify it under the same terms as
Perl itself.

All references to TAM, Tivoli Access Manager, etc are copyrighted, trademarked
and otherwise patented by IBM.

=cut
337