I'm using the NetMRI Perl API (version 2.4) to create some external automation scripts and I think I'm finding that the state of the croak_if_error attribute of NetMRI::API seems to be changing the way the API returns results. Specifically, it is causing calls to the API to not return objects when it should.
Consider this script (based on the sample sync_external.pl included in the docs):
- use strict;
- use warnings;
- use Data::Dumper;
- use NetMRI::API;
- # Connect to the NetMRI.
- my $client = new NetMRI::API({
- username => "admin",
- password => "mypassword",
- url => "<a href="http://192.168.1.1",
- ">http://192.168.1.1",
- </a> api_version => '2.4',
- croak_if_error => "1"
- });
- # run the script against the devices specified on the
- # command line.
- my($job_id) = $client->broker->script->run({
- name => 'Ad Hoc Command Batch',
- device_ids => \@ARGV,
- '$CommandBatch' => 'show version',
- })->{JobID};
- # fetch the Job object
- my $job = $client->broker->job->show({ JobID => $job_id })->{job};
- print Dumper($job);
- print "Status: ", $job->wait, "\n";
This script runs just fine if croak_if_error is set to 1, but it fails if croak is set to 0. The error posted is:
Can't call method "wait" on an undefined value at jobtest.pl
It looks like this is happening because we expect $job to come back as an object, but it only does so if croak is set to 1.
The API docs say:
By default an exception is thrown using die when an error occurs communicating with the NetMRI appliance. If this is set to true, then you will instead get a NetMRI::API::Response object returned back to you.
I read that as saying - "If you set croak to 1 and send an invalid API call, the script fails with a call to Perl's die function. If you set croak to 0 and send an invalid API call, the call returns an object of type NetMRI::API::Response." I prefer to set croak to 0 because I can then test the Response object and more gracefully handle errors. However, I can't do this because croak seems to be telling the API never to return objects, rather than never to return them on invalid API calls.
Is what I'm seeing here right? Is this "by design" in the API? If so how can I more elegantly handle errors? If not, has it been fixed?
Thanks.
