#
#################################################################################
my ($NAME) = $0 =~ m,([^/]+)$,;
+my %ALIASES = (
+ '/server' => '/dedicated/server',
+ '/dns' => '/domain/zone',
+);
#################################################################################
#
my %Opt = (
'method' => 'GET',
);
-get_options(\%Opt);
-$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($_=<CONFIG>) && $_);
- 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;
ip_move($Opt{'ip-move'}) if $Opt{'ip-move'};
# Query
-query();
+help() unless @ARGV;
+query(@ARGV);
# END
exit 0;
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();
my $content = $response->content;
return unless defined $content;
unless (wantarray) {
- #$content = check_row($url,$content);
+ $content = check_row($url,$content);
return $content;
}
}
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();
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;
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($_=<CONFIG>) && $_);
+ close CONFIG;
+
+ }
+}
+
sub help {
#------------------------------------------------------------------------------
# Print help and exit
=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.
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