]> git.nbdom.net Git - nb.git/commitdiff
bin/ovh-api
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 4 Feb 2017 22:24:42 +0000 (22:24 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 4 Feb 2017 22:24:42 +0000 (22:24 +0000)
bin/ovh-api

index c7653fca173231df555c4f722b89955c55f60193..3056f0e5481cd8ffe751f442ddcf96a1355c02eb 100755 (executable)
@@ -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.