From: Nicolas Boisselier Date: Sat, 25 Feb 2017 23:14:00 +0000 (+0000) Subject: bin/ovh-api X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=4efe8d2a3d68dc872ff02b9f3f76bb7e38d5ed07;p=nb.git bin/ovh-api --- diff --git a/bin/ovh-api b/bin/ovh-api index 3c6348a9..1ff8c458 100755 --- a/bin/ovh-api +++ b/bin/ovh-api @@ -16,6 +16,10 @@ my $VERSION = '0.0.1'; # ################################################################################# my ($NAME) = $0 =~ m,([^/]+)$,; +my %ALIASES = ( + '/server' => '/dedicated/server', + '/dns' => '/domain/zone', +); ################################################################################# # @@ -28,8 +32,6 @@ my $DEBUG = $main::DEBUG = 0; my %Opt = ( 'method' => 'GET', ); -get_options(\%Opt); -$main::_DATA_ = ''; ################################################################################# # @@ -39,31 +41,14 @@ $main::_DATA_ = ''; use JSON; use LWP::UserAgent; use OvhApi; -my $config = $ENV{'OVH_API_CFG'} ? $ENV{'OVH_API_CFG'} : "$ENV{HOME}/.$NAME"; -my @KEY = (); - -if (@ARGV and $ARGV[0] =~ /^(\w+):(\w+)(?::(\w+))?$/) { - @KEY = split(":",$ARGV[0]); - shift @ARGV; -} elsif ($ENV{'OVH_API_KEY'}) { - @KEY = split(':',$ENV{'OVH_API_KEY'}); - -} elsif (-e $config) { - open(CONFIG,"$config") or die "$NAME: can't read $config"; - @KEY = split(":",chomp($_=) && $_); - close CONFIG; - -} +my @KEY = (); +init(); @KEY or help(); open STDOUT,'| (which jq > /dev/null && jq . || cat)' if $Opt{jq}; my $Api = OvhApi->new(type => OvhApi::OVH_API_EU, applicationKey => $KEY[0], applicationSecret => $KEY[1], consumerKey => $KEY[2]); -use Data::Dumper; -#@_=_query('/domain');die Dumper(\@_); -#$_=_query('/domain');die Dumper($_); - # Auth auth() if @KEY < 3; @@ -72,7 +57,8 @@ sql() if $Opt{sql}; ip_move($Opt{'ip-move'}) if $Opt{'ip-move'}; # Query -query(); +help() unless @ARGV; +query(@ARGV); # END exit 0; @@ -115,11 +101,7 @@ sub ip_move { sub _query { my ($url,$method,$body) = @_; - my %aliases = ( - '/server' => '/dedicated/server', - '/dns' => '/domain/zone', - ); - exists $aliases{$url} and $url = $aliases{$url}; + exists $ALIASES{$url} and $url = $ALIASES{$url}; my $response = $Api->rawCall(path=>$url,method=>($method||'GET'),body=>$body); die "$NAME: ERR: ".$response->error() if $response->isFailure(); @@ -127,7 +109,7 @@ sub _query { my $content = $response->content; return unless defined $content; unless (wantarray) { - #$content = check_row($url,$content); + $content = check_row($url,$content); return $content; } @@ -149,37 +131,64 @@ sub _query { } sub query { - help() unless @ARGV; - my $URL = shift @ARGV or help(); + my ($url,@body) = @_; + + # Body + my $body; + unless (@body) { + $body = {}; + + } elsif (@body == 1 and $body[0] =~ /^\s*\{/) { + $body = $body[0]; + if ( -e $body ) { + $body = JSON::decode_json(`cat "$body"`); + } else { + $body = JSON::decode_json($body); + } + + } else { + %$body = grep { defined $_} map {/^([^=\s]+)\s*=\s*(.*?)$/ ? ($1=>$2) : undef } @body; + + } + + my $content; + my $response; + my $method = $Opt{method}; + +if (1) { + if ($Opt{rows}) { + local @_ = _query($url,$method,$body); + print JSON::encode_json(\@_); + } else { + local $_ = _query($url,$method,$body); + print ref($_) eq '' ? $_ : JSON::encode_json($_); + #print JSON::encode_json(_query($url,$method,$body)); + } + print "\n"; + exit; +} + my %aliases = ( '/server' => '/dedicated/server', '/dns' => '/domain/zone', ); - exists $aliases{$URL} and $URL = $aliases{$URL}; - my $BODY = @ARGV ? shift(@ARGV) : {}; - - if ( -e $BODY ) { - $BODY = JSON::decode_json(`cat $BODY`); - } elsif (ref($BODY) eq '') { - $BODY = JSON::encode_json($BODY); - } + exists $aliases{$url} and $url = $aliases{$url}; - my $response = $Api->rawCall(path=>$URL,method=>$Opt{method},body=>$BODY); + $response = $Api->rawCall(path=>$url,method=>$method,body=>$body); die "$NAME: ERR: ".$response->error() if $response->isFailure(); + $content = $response->content; - my $content = $response->content; return unless defined $content; if ($Opt{rows}) { print "[\n" if @$content>0; my $count = 0; for my $id (@$content) { - $id = check_row($URL,$id); + $id = check_row($url,$id); next unless defined $id; - $id =~ s,/.*$,,; - $response = $Api->rawCall(path=>$URL.'/'.$id,method=>$Opt{method},body=>{}); + $response = $Api->rawCall(path=>$url.'/'.$id,method=>$Opt{method},body=>{}); die "$NAME: ERR: ".$response->error() if $response->isFailure(); @@ -322,6 +331,22 @@ my @tbs = ( sub check_row { my ($url,$id) = @_; + + # Array + if (ref $id eq 'ARRAY') { + for (my $i=0; $i<@$id; $i++) { + $id->[$i] = check_row($url,$id->[$i]); + } + @$id = grep {defined $_} @$id; + return $id; + } + + # Hash + if (ref $id eq 'HASH') { + return $id; + } + + # Scalar return undef if $url eq '/ip' and $id !~ /\d+\./; $id =~ s,/.*$,,; return $id; @@ -344,6 +369,28 @@ sub auth { exit } +sub init { + get_options(\%Opt); + $main::_DATA_ = ''; + + my $config = $ENV{'OVH_API_CFG'} ? $ENV{'OVH_API_CFG'} : "$ENV{HOME}/.$NAME"; + $Opt{conf} and $config = $Opt{conf}; + + if (@ARGV and $ARGV[0] =~ /^(\w+):(\w+)(?::(\w+))?$/) { + @KEY = split(":",$ARGV[0]); + shift @ARGV; + + } elsif ($ENV{'OVH_API_KEY'}) { + @KEY = split(':',$ENV{'OVH_API_KEY'}); + + } elsif (-e $config) { + open(CONFIG,"$config") or die "$NAME: can't read $config"; + @KEY = split(":",chomp($_=) && $_); + close CONFIG; + + } +} + sub help { #------------------------------------------------------------------------------ # Print help and exit @@ -447,12 +494,13 @@ $NAME - Script to query ovh's api =head1 OPTIONS - option[jq] Pipe stdout to | jq . option[rows|r] Print each rows of a table (eg /ip, /server, ...) option[method|m=s] Method (default: GET) + option[conf=s] Use another config file instaed of ~/.ovh-api + option[jq] Pipe stdout to | jq . - option[sql] Produce sql to update a database option[ip-move=s] Move IP to local + option[sql] Produce sql to update a database option[verbose|v+] Verbose mode: increase the verbosity level. option[debug+] Debug mode: increase the verbosity level. @@ -476,8 +524,7 @@ See for more examples: https://api.ovh.com/console Getopt::Std, Pod::Usage, OvhApi, URI::Escape, JSON * apt-get install -y libjson-perl libwww-perl - * port install p5-json p5-lwp p5-libwww-perl - p5.24-libwww-perl + * port install p5-json p5-lwp p5-libwww-perl p5.24-libwww-perl =head1 COPYRIGHT AND LICENSE