#!/usr/bin/perl -w

# Lists the available hosts

use Pod::Usage;
use Getopt::Long;
use SOAP::Lite;
use strict;

# Global options
our $opt_version;
our $opt_help;
our $opt_man;
our $opt_resource = 'http://www.osdl.org/WebService/TestSystem';
our $opt_server   = 'http://www.osdl.org:8081/';

# Handle commandline options
Getopt::Long::Configure ('bundling', 'no_ignore_case');
GetOptions(
           'version|V'    => \$opt_version,
           'help|h'       => \$opt_help,
           'man'          => \$opt_man,
           'resource|r=s' => \$opt_resource,
           'server|s=s'   => \$opt_server,
           ) or pod2usage(-verbose => 1, -exitstatus => 0);

# Handle -V or --version
if ($opt_version) {
    print '$0: $Revision: 1.4 $', "\n";
    exit 0;
}

# Usage
pod2usage(-verbose => 2, -exitstatus => 0) if ($opt_man);
pod2usage(-verbose => 1, -exitstatus => 0) if ($opt_help);

sub main {
    # Establish connection to server
    my $soap = SOAP::Lite
        -> uri($opt_resource)
        -> proxy($opt_server,
                 options => {compress_threshold => 10000});

    # Create the testsys object
    my $testsys = $soap
        -> call(new => ('stpdb_dbi'=>'dbi:mysql:STPDB:air.osdl.org'))
        -> result;

    # Retrieve hosts
    my $result = $soap->get_hosts($testsys);

    # Handle error conditions
    if ($result->fault) {
        print join ', ', 
        $result->faultcode,
        $result->faultstring;
        exit -1;
    }

    if (! $result->result) {
        return 0;
    }

    # Display header
    my $format ="%-5s %-10s %-15s %-15s\n";
    printf($format, "ID", "Name", "Host Type ID", "Host State ID");

    # Print results, row by row
    foreach my $row (@{$result->result}) {
        next unless ($row && $row->{host_type_uid} > 0);
        printf($format,
               $row->{uid},
               $row->{descriptor},
               $row->{host_type_uid},
               $row->{host_state_uid}
               );
    }
    return 1;
}

exit main();

__END__

=head1 NAME

stp-lshost - lists available test hosts

=head1 SYNOPSIS

stp-lshost [-s server_url] [-u resource_uri]

=head1 DESCRIPTION

This tool lists the available hosts from the test system using SOAP
calls to the server at 'server_url' providing the 'resource_uri'
service.  

=head1 OPTIONS

=over 8

=item B<-V>, B<--version>

Displays the version number of the script and exits.

=item B<-h>, B<--help>

Displays a brief usage message

=item B<--man>

Displays the man page

=item B<s> I<server_url>, B<--server>=I<server_url>

The URL of the WebService::TestSystem server to connect to.  By default,
it uses 'http://www.osdl.org:8081'.

=item B<r> I<resource_uri>, B<--resource>=I<resource_uri>

The URI of the service provided by the server.  By default, it uses
'http://www.osdl.org/WebService/TestSystem'.  Users should not typically
need to alter this setting.

=back

=head1 PREREQUISITES

B<SOAP::Lite>,
B<Pod::Host>,
B<Getopt::Long>

=head1 AUTHOR

Bryce Harrington E<lt>bryce@osdl.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 2004 Open Source Development Labs
All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 REVISION

Revision: $Revision: 1.4 $

=cut
