Skip to content

Commit

Permalink
enh(code): other improvements after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
omercier committed Nov 8, 2024
1 parent 30377ae commit 2148490
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
22 changes: 8 additions & 14 deletions src/apps/vmware/vsphere8/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use MIME::Base64;
use Digest::MD5 qw(md5_hex);

Expand Down Expand Up @@ -80,7 +79,6 @@ sub check_options {
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : '';
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
#$self->{http_backend} = (defined($self->{option_results}->{http_backend})) ? $self->{option_results}->{http_backend} : 'curl';

if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
Expand Down Expand Up @@ -120,7 +118,7 @@ sub settings {

return 1 if (defined($self->{settings_done}));
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');

$self->{http}->set_options(%{$self->{option_results}});
$self->{settings_done} = 1;

Expand All @@ -144,17 +142,19 @@ sub get_token {
|| $options{force_authentication}
) {
my $auth_string = MIME::Base64::encode_base64($self->{username} . ':' . $self->{password});
chomp $auth_string;

$self->settings();
my $content = $self->{http}->request(
method => 'POST',
url_path => '/api/session',
query_form_post => '',
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status},
header => [
'Content-Type: application/json',
'Authorization: Basic ' . $auth_string
'Authorization: Basic ' . $auth_string,
'Content-Type: application/x-www-form-urlencoded'
]
);

Expand All @@ -181,7 +181,6 @@ sub try_request_api {
header => [ 'vmware-api-session-id: ' . $token ],
unknown_status => '',
insecure => (defined($self->{option_results}->{insecure}) ? 1 : 0)

);

if (!defined($content) || $content eq '') {
Expand All @@ -191,14 +190,7 @@ sub try_request_api {
$self->{output}->option_exit();
}

my $decoded;
eval {
$decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
my $decoded = centreon::plugins::misc::json_decode($content);

return $decoded;
}
Expand Down Expand Up @@ -355,6 +347,8 @@ Calls try_request_api and recalls it forcing authentication if the first call fa
=over 8
=item * C<method> - The HTTP method to use (examples: GET, POST).
=item * C<endpoint> - The API endpoint to request.
=item * C<get_param> - Additional GET parameters for the request.
Expand Down
3 changes: 1 addition & 2 deletions src/centreon/plugins/misc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,7 @@ sub json_encode {
$encoded = encode_json($object);
};
if ($@) {
use Data::Dumper;
print STDERR 'Cannot encode object to JSON' . Dumper($object);
print STDERR 'Cannot encode object to JSON. Error message: ' . $@;
return undef;
}

Expand Down
1 change: 1 addition & 0 deletions tests/apps/vmware/vsphere8/esx/discovery.robot
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resour
Suite Setup Start Mockoon ${MOCKOON_JSON}
Suite Teardown Stop Mockoon
Test Timeout 120s
Test Setup Ctn Cleanup Cache


*** Variables ***
Expand Down
2 changes: 1 addition & 1 deletion tests/apps/vmware/vsphere8/esx/host-status.robot
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resour
Suite Setup Start Mockoon ${MOCKOON_JSON}
Suite Teardown Stop Mockoon
Test Timeout 120s

Test Setup Ctn Cleanup Cache

*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
Expand Down

0 comments on commit 2148490

Please sign in to comment.