better response handling of azure apis

This commit is contained in:
Colin Gagnaire 2019-03-19 09:39:10 +01:00
parent 86b997f3b4
commit 5cbce02f28
2 changed files with 37 additions and 22 deletions

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();
}