This commit is contained in:
garnier-quentin 2019-03-19 09:57:29 +01:00
commit 4738f53b8b
5 changed files with 59 additions and 27 deletions

View File

@ -359,10 +359,10 @@ sub request {
eval "$options{request}->{critical_status}") {
$status = 'critical';
} elsif (defined($options{request}->{warning_status}) && $options{request}->{warning_status} ne '' &&
eval "$options{request}->{warning_status}") {
eval "$options{request}->{warning_status}") {
$status = 'warning';
} elsif (defined($options{request}->{unknown_status}) && $options{request}->{unknown_status} ne '' &&
eval "$options{request}->{unknown_status}") {
eval "$options{request}->{unknown_status}") {
$status = 'unknown';
}
};
@ -398,8 +398,13 @@ sub get_code {
return $self->{response_code};
}
1;
sub get_message {
my ($self, %options) = @_;
return $http_code_explained->{$self->{response_code}};
}
1;
__END__

View File

@ -208,10 +208,10 @@ sub request {
eval "$request_options->{critical_status}") {
$status = 'critical';
} elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' &&
eval "$request_options->{warning_status}") {
eval "$request_options->{warning_status}") {
$status = 'warning';
} elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' &&
eval "$request_options->{unknown_status}") {
eval "$request_options->{unknown_status}") {
$status = 'unknown';
}
};
@ -250,6 +250,12 @@ sub get_code {
return $self->{response}->code();
}
sub get_message {
my ($self, %options) = @_;
return $self->{response}->message();
}
1;
__END__

View File

@ -190,6 +190,12 @@ sub get_code {
return $self->{'backend_' . $self->{http_backend}}->get_code();
}
sub get_message {
my ($self, %options) = @_;
return $self->{'backend_' . $self->{http_backend}}->get_message();
}
1;
__END__

View File

@ -181,18 +181,23 @@ sub get_access_token {
full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/token',
hostname => '');
if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) {
$self->{output}->add_option_msg(short_msg => "Login endpoint API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
if (defined($decoded->{error})) {
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error_description}, debug => 1);
$self->{output}->add_option_msg(short_msg => "Login endpoint API return error code '" . $decoded->{error} . "' (add --debug option for detailed message)");
$self->{output}->add_option_msg(short_msg => "Login endpoint API returns error code '" . $decoded->{error} . "' (add --debug option for detailed message)");
$self->{output}->option_exit();
}
@ -217,18 +222,28 @@ sub request_api {
my $content = $self->{http}->request(%options);
if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) {
$self->{output}->add_option_msg(short_msg => "Management endpoint API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
if (defined($decoded->{error})) {
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1);
$self->{output}->add_option_msg(short_msg => "Management endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)");
$self->{output}->add_option_msg(short_msg => "Management endpoint API returns error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)");
$self->{output}->option_exit();
}
if (defined($decoded->{code})) {
$self->{output}->output_add(long_msg => "Message : " . $decoded->{message}, debug => 1);
$self->{output}->add_option_msg(short_msg => "Management endpoint API returns code '" . $decoded->{code} . "' (add --debug option for detailed message)");
$self->{output}->option_exit();
}

View File

@ -40,23 +40,22 @@ sub new {
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments =>
{
"subscription:s" => { name => 'subscription' },
"tenant:s" => { name => 'tenant' },
"client-id:s" => { name => 'client_id' },
"client-secret:s" => { name => 'client_secret' },
"timeframe:s" => { name => 'timeframe' },
"interval:s" => { name => 'interval' },
"aggregation:s@" => { name => 'aggregation' },
"zeroed" => { name => 'zeroed' },
"timeout:s" => { name => 'timeout', default => 50 },
"sudo" => { name => 'sudo' },
"command:s" => { name => 'command', default => 'az' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options', default => '' },
"proxyurl:s" => { name => 'proxyurl' },
});
$options{options}->add_options(arguments => {
"subscription:s" => { name => 'subscription' },
"tenant:s" => { name => 'tenant' },
"client-id:s" => { name => 'client_id' },
"client-secret:s" => { name => 'client_secret' },
"timeframe:s" => { name => 'timeframe' },
"interval:s" => { name => 'interval' },
"aggregation:s@" => { name => 'aggregation' },
"zeroed" => { name => 'zeroed' },
"timeout:s" => { name => 'timeout', default => 50 },
"sudo" => { name => 'sudo' },
"command:s" => { name => 'command', default => 'az' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options', default => '' },
"proxyurl:s" => { name => 'proxyurl' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'AZCLI OPTIONS', once => 1);
@ -129,7 +128,8 @@ sub execute {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->output_add(long_msg => $response, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}