#!/usr/bin/env perl
# vim: set sw=4 ts=4 et ai si:
#
use strict;
use warnings;
use 5.010;

use Getopt::Long;
use Pod::Usage;
use WebService::IdoitAPI;

use version; our $VERSION = '0.4.2'; # VERSION

sub initialize {
    my $config = {};
    my %opt = ();
    my @opt_def = qw(
        config=s
        help|?
        man
        version
    );
    GetOptions(\%opt, @opt_def);

    pod2usage(-exitstatus => 0, -input => \*DATA)
        if $opt{help};
    pod2usage(-exitstatus => 0, -input => \*DATA, -verbose => 2)
        if $opt{man};
    pod2usage(-exitstatus => 0, -input => \*DATA, -verbose => 99, -sections => 'VERSION')
        if $opt{version};

    $config = WebService::IdoitAPI::read_config($opt{config});

    $config->{opt} = \%opt;

    my $api = WebService::IdoitAPI->new( $config );
    
    $api->login();

    $config->{api} = $api;

    return $config;
} # initialize()

sub main {

    my ($config);

    $config = initialize();

    test_cmdb_objects_read($config);

    $config->{api}->logout();

} # main()

sub print_finding {
    my ($config,$msg) = @_;
    print $msg,"\n";
} # print_finding()

sub test_cmdb_objects_read {
    my ($config) = @_;
    my ($api,$cres,$res);

    $api = $config->{api};
    $res = $api->request( {
        method => 'cmdb.objects.read',
        params => {
            categories => [ "C__CATG__GLOBAL", "C__CATS__NET" ],
            filter => {
                type => "C__OBJTYPE__LAYER3_NET",
                status => "C__RECORD_STATUS__NORMAL",
            },
            limit => "1",
        },
    } );
    return unless ($res);
    return unless ($res->{is_success});
    $cres = scalar @{$res->{content}->{result}};
    unless (1 == $cres) {
        print_finding($config,"*** cmdb.objects.read: param 'limit' does not work (1 <=> $cres)");
    }
    unless (exists $res->{content}->{result}->[0]->{categories}) {
        print_finding($config,"*** cmdb.objects.read: missing categories in result");
    }
    return;
} # test_cmdb_objects_read()

main() if not caller();

1;

__END__

=encoding utf8

=head1 NAME

idoit-test-api - test the i-doit RPC API

=head1 VERSION

version 0.4.2

=head1 SYNOPSIS

  idoit-test-api [ options ]

=head1 OPTIONS

=head2 --help | -h | -?

Show a short description and exit.

=head2 --man

Show the full man page and exit.

=head2 --config path

Read configuration values from a file found at the given path.

=head3 --version | -V

Show the version and exit.

=head1 CONFIGURATION FILE FORMAT

The configuration file should contain lines with a key and a value
separated by equal sign (I<=>) or colon (I<:>).
The key and the value may be enclosed
in single (I<'>) or double (I<">) quotation marks.
Leading and trailing white space is removed
as well as a comma (I<,>) at the the end of the line.

The program needs the following keys in the file:

=over 4

=item key

The API key for i-doit.

=item url

The URL of the i-doit instance.

=item username

The username for the login (optional).

=item password

The password for the login (optional).

=back

=head1 AUTHOR

Mathias Weidner C<< <mamawe@cpan.org> >>
