From: Nicolas Boisselier Date: Sat, 4 Feb 2017 22:24:42 +0000 (+0000) Subject: bin/ovh-api X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=9c2dd2df354d8a83bdf41409bbae33e326372c96;p=nb.git bin/ovh-api --- diff --git a/bin/ovh-api b/bin/ovh-api index c7653fca..3056f0e5 100755 --- a/bin/ovh-api +++ b/bin/ovh-api @@ -71,12 +71,7 @@ auth() if @KEY < 3; # Query query(); - -################################################################################# -# # END -# -################################################################################# exit 0; ################################################################################# @@ -102,26 +97,35 @@ sub auth { } sub query { -help() unless @ARGV; -my $URL = shift @ARGV or help(); -my $BODY = @ARGV ? shift(@ARGV) : {}; - -if ( -e $BODY ) { - $BODY = JSON::decode_json(`cat $BODY`); -} elsif (ref($BODY) eq '') { - $BODY = JSON::encode_json($BODY); -} -my $response = $Api->rawCall(path=>$URL,method=>$Opt{method},body=>$BODY); + help() unless @ARGV; -if ($response->isFailure()) { - die "$NAME: ERR: ".$response->error()."\n"; -} + my $URL = shift @ARGV or help(); + my $BODY = @ARGV ? shift(@ARGV) : {}; + + if ( -e $BODY ) { + $BODY = JSON::decode_json(`cat $BODY`); + } elsif (ref($BODY) eq '') { + $BODY = JSON::encode_json($BODY); + } + + my $response = $Api->rawCall(path=>$URL,method=>$Opt{method},body=>$BODY); + die "$NAME: ERR: ".$response->error()."\n" if $response->isFailure(); + + my $content = $response->content; + return unless defined $content; + + if ($Opt{rows}) { + for my $id (@$content) { + $response = $Api->rawCall(path=>$URL.'/'.$id,method=>$Opt{method},body=>{}); + die "$NAME: ERR: ".$response->error()."\n" if $response->isFailure(); + print JSON::encode_json($response->content)."\n"; + } + return + } -my $content = $response->content; -exit unless defined $content; -$content = JSON::encode_json($content) if $content and ref($content) ne ''; -#use Data::Dumper; die Dumper($content); -print "$content\n"; + $content = JSON::encode_json($content) if $content and ref($content) ne ''; + #use Data::Dumper; die Dumper($content); + print "$content\n"; } sub help { @@ -223,6 +227,7 @@ $NAME - Script to query ovh's api =head1 OPTIONS + option[rows|r] Print each rows of a table (eg /ip, /server, ...) option[method|m=s] Method (default: GET) option[verbose|v+] Verbose mode: increase the verbosity level. option[debug+] Debug mode: increase the verbosity level.