#!/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.4'; # 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();

    print_components($config);
    test_cmdb_objects_read($config);

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

} # main()

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

    $api = $config->{api};
    $res = $api->request( {
        method => 'idoit.version',
    } );
    if ($res and $res->{is_success}) {
        my %result = %{$res->{content}->{result}};
        my ($iv,$is,$it) = @result{qw(version step type)};
        my $step = $is ? " step: $is," : '';
        print "i-doit version: $iv,$step type $it\n";
    }
    $res = $api->request( {
        method => 'idoit.license',
    } );
    if ($res and $res->{is_success}) {
        my %result = %{$res->{content}->{result}};
        for my $license (@{$result{licenses}}) {
            printf(" %s, %d objects, registered %s - %s\n"
                  ,$license->{licenseType}
                  ,$license->{objects}
                  ,$license->{registrationDate}
                  ,$license->{validUntil}
                  );
            next;
        }
        printf(" %d objects in use out of %d\n"
              ,$result{objectCapacity}->{inUse}
              ,$result{objectCapacity}->{total}
              );
        print " Add ons (licensed)\n";
        for my $key (sort keys %{$result{addons}}) {
            my $addon = $result{addons}->{$key};
            printf("  %s (%s)\n"
                  ,$addon->{label}
                  ,$addon->{licensed}
                  );
            next;
        }
    }
    $res = $api->request( {
        method => 'idoit.addons',
    } );
    if ($res and $res->{is_success}) {
        my %result = %{$res->{content}->{result}};
        print " Add ons installed\n";
        for my $addon (@{$result{result}}) {
            printf("  %s (%s) version %s\n"
                  ,$addon->{title}
                  ,$addon->{author}->{name}
                  ,$addon->{version}
                  );
            next;
        }
    }
} # print_components()

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",
        },
    } );
    if (not $res) {
        print_finding($config,"*** cmdb.objects.read: dit not return anything");
        return;
    }
    if (not $res->{is_success}) {
        print_finding($config,"*** cmdb.objects.read: dit not return success");
        return;
    }
    $cres = scalar @{$res->{content}->{result}};
    if (0 == $cres) {
        print_finding($config,"*** cmdb.objects.read: with param 'limit' returned an empty list");
        return;
    }
    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.4

=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> >>
