From: Nicolas Boisselier Date: Sat, 4 Feb 2017 21:33:18 +0000 (+0000) Subject: bin/ovh-api X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=5a1ccce02c8bec24c51dbb1cde3791b86649a53b;p=nb.git bin/ovh-api --- diff --git a/bin/ovh-api b/bin/ovh-api index 004cef2e..e67687f6 100755 --- a/bin/ovh-api +++ b/bin/ovh-api @@ -32,28 +32,28 @@ my %Opt = ( 'method' => 'GET', ); get_options(\%Opt); -# Auth -if ($_ = $Opt{activate}) { - my @keys = /:/ ? split(':',$_) : ($_,''); - my $Api = OvhApi->new(type => OvhApi::OVH_API_EU, applicationKey => $keys[0], applicationSecret => $keys[1]); - my $Answer = $Api->requestCredentials(accessRules => [ { - method => 'ALL', path => '/*', -# NB 04.02.17 method => 'GET', path => '/*', -# NB 04.02.17 method => 'POST', path => '/*', -# NB 04.02.17 method => 'PUT', path => '/*', -# NB 04.02.17 method => 'DELETE', path => '/*', - }]); - - if ($Answer) { - #my ($consumerKey, $validationUrl) = @{ $Answer->content}{qw{ consumerKey validationUrl }}; - print map {"$_\n"} @{ $Answer->content}{qw{ consumerKey validationUrl }}; - # $consumerKey contains the newly created Consumer Key - # $validationUrl contains a link to OVH website in order to login an OVH account and link it to the credential - } - exit; - exec qq|curl -s -XPOST -H"X-Ovh-Application: $_" -H "Content-type: application/json" --data '{"accessRules":[{"method":"GET","path":"/*"},{"method":"POST","path":"/*"},{"method":"PUT","path":"/*"},{"method":"DELETE","path":"/*"}]}' https://api.ovh.com/1.0/auth/credential|; - exit 0; -} +# NB 04.02.17 # Auth +# NB 04.02.17 if ($_ = $Opt{activate}) { +# NB 04.02.17 my @keys = /:/ ? split(':',$_) : ($_,''); +# NB 04.02.17 my $Api = OvhApi->new(type => OvhApi::OVH_API_EU, applicationKey => $keys[0], applicationSecret => $keys[1]); +# NB 04.02.17 my $Answer = $Api->requestCredentials(accessRules => [ { +# NB 04.02.17 method => 'ALL', path => '/*', + method => 'GET', path => '/*', + method => 'POST', path => '/*', + method => 'PUT', path => '/*', + method => 'DELETE', path => '/*', +# NB 04.02.17 }]); +# NB 04.02.17 +# NB 04.02.17 if ($Answer) { +# NB 04.02.17 #my ($consumerKey, $validationUrl) = @{ $Answer->content}{qw{ consumerKey validationUrl }}; +# NB 04.02.17 print map {"$_\n"} @{ $Answer->content}{qw{ consumerKey validationUrl }}; +# NB 04.02.17 # $consumerKey contains the newly created Consumer Key +# NB 04.02.17 # $validationUrl contains a link to OVH website in order to login an OVH account and link it to the credential +# NB 04.02.17 } +# NB 04.02.17 exit; +# NB 04.02.17 exec qq|curl -s -XPOST -H"X-Ovh-Application: $_" -H "Content-type: application/json" --data '{"accessRules":[{"method":"GET","path":"/*"},{"method":"POST","path":"/*"},{"method":"PUT","path":"/*"},{"method":"DELETE","path":"/*"}]}' https://api.ovh.com/1.0/auth/credential|; +# NB 04.02.17 exit 0; +# NB 04.02.17 } $main::_DATA_ = ''; @@ -66,7 +66,7 @@ $main::_DATA_ = ''; my $config = "$ENV{HOME}/.$NAME"; my @KEY = (); -if (@ARGV and $ARGV[0] =~ /^(\w+):(\w+):(\w+)$/) { +if (@ARGV and $ARGV[0] =~ /^(\w+):(\w+)(?::(\w+))?$/) { @KEY = split(":",$ARGV[0]); shift @ARGV; @@ -82,8 +82,28 @@ if (@ARGV and $ARGV[0] =~ /^(\w+):(\w+):(\w+)$/) { @KEY or help(); help() unless @ARGV; -my $o = OvhApi->new(type => OvhApi::OVH_API_EU, applicationKey => $KEY[0], applicationSecret => $KEY[1], consumerKey => $KEY[2]); +my $Api = OvhApi->new(type => OvhApi::OVH_API_EU, applicationKey => $KEY[0], applicationSecret => $KEY[1], consumerKey => $KEY[2]); +# Auth +if (@KEY < 3) { + my $Answer = $Api->requestCredentials(accessRules => [ { + method => 'ALL', path => '/*', + }]); + + if ($Answer) { + my ($consumerKey, $validationUrl) = @{ $Answer->content}{qw{ consumerKey validationUrl }}; + push @KEY, $consumerKey; + print "Your consumerKey is $consumerKey\n"; + print "You need to validate in your browser at $validationUrl\n"; + print "Create ~/.ovh.api with this content ".join(':',@KEY)."\n"; + #print map {"$_\n"} @{ $Answer->content}{qw{ consumerKey validationUrl }}; + # $consumerKey contains the newly created Consumer Key + # $validationUrl contains a link to OVH website in order to login an OVH account and link it to the credential + } + exit +} + +# Query my $URL = shift @ARGV or help(); my $BODY = @ARGV ? shift(@ARGV) : {}; #use Data::Dumper; die Dumper(JSON::encode_json($BODY)); @@ -100,7 +120,7 @@ if ( -e $BODY ) { } #$BODY = -e $BODY ? JSON::decode_json(`cat $BODY`) : JSON::encode_json($BODY);# if $BODY; #use Data::Dumper; die Dumper($BODY); -my $response = $o->rawCall(path=>$URL,method=>$Opt{method},body=>$BODY); +my $response = $Api->rawCall(path=>$URL,method=>$Opt{method},body=>$BODY); if ($response->isFailure()) { die "$NAME: ERR: ".$response->error()."\n"; @@ -201,28 +221,28 @@ sub get_options { $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; } +# option[activate=s] [KEY] Activate a key (first bit before ":" or KEY[0]) __DATA__ =head1 NAME -$NAME - Script to query ovh +$NAME - Script to query ovh's api =head1 SYNOPSIS -Quick usage: - - $NAME k1:k2:k3 /PATH - $NAME -activate JSHFDlkhaKJHGSF... - $NAME /PATH - Eg: - /domain/zone/DOM/export - /dedicated/server - /ip + 1. Get your consumerKey: + $NAME applicationKey:applicationSecret + 2. Store your complete key + echo 'applicationKey:applicationSecret:consumerKey' > ~/.ovh-api && chmod 600 ~/.ovh-api + 3. Activate your key + Ex: https://eu.api.ovh.com/auth/?credentialToken=fXOwsn9y2qYHiIclcxChmAAYQLZLEPQEENAPz2EeAYDWj9W0CY9yVQR35bLi2540 + 4. Use the api + $NAME applicationKey:applicationSecret:consumerKey /PATH + $NAME /PATH =head1 OPTIONS - option[activate=s] [KEY] Activate a key (first bit before ":" or KEY[0]) option[method|m=s] Method (default: GET) option[verbose|v+] Verbose mode: increase the verbosity level. option[debug+] Debug mode: increase the verbosity level. @@ -234,28 +254,24 @@ Quick usage: =head1 DESCRIPTION - $NAME k1:k2:k3 /domain - $NAME /domain - $NAME -activate MNBaskfjh +From https://api.ovh.com/console: - { - "validationUrl": "https://eu.api.ovh.com/auth/?credentialToken=KJHDSFLAF...", - "consumerKey": "KJHDSF...", - "state": "pendingValidation" - } + /domain/zone//export + /dedicated/server + /dedicated/server/ =head1 REQUIRES -Getopt::Std, Pod::Usage, OvhApi, URI::Escape, JSON + Getopt::Std, Pod::Usage, OvhApi, URI::Escape, JSON + apt-get install -y libjson-perl libwww-perl =head1 COPYRIGHT AND LICENSE -Copyright (C) 2016 Nicolas Boisselier - -See . + Copyright (C) 2016 Nicolas Boisselier + See . =head1 AUTHOR -Nicolas Boisselier nicolas.boisselier@gmail.com +Nicolas Boisselier =cut diff --git a/etc/vim/templates/perl.tt b/etc/vim/templates/perl.tt index 5715cd0f..34aff7d2 100755 --- a/etc/vim/templates/perl.tt +++ b/etc/vim/templates/perl.tt @@ -136,7 +136,7 @@ $NAME - Script to do ... =head1 SYNOPSIS -Quick usage: +Quick usage! =over @@ -148,7 +148,7 @@ Quick usage: =head1 DESCRIPTION -... +Description! =head1 OPTIONS