]> git.nbdom.net Git - nb.git/commitdiff
bin/ovh-api
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 4 Feb 2017 21:33:18 +0000 (21:33 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 4 Feb 2017 21:33:18 +0000 (21:33 +0000)
bin/ovh-api
etc/vim/templates/perl.tt

index 004cef2e6c8fd8832fa426a585edbb1327acf6fb..e67687f640e42374fb8840712c6ba25a6e7fe61b 100755 (executable)
@@ -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/<DOMAIN>/export
+  /dedicated/server
+  /dedicated/server/<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 <http://www.gnu.org/licenses/>.
+  Copyright (C) 2016 Nicolas Boisselier
+  See <http://www.gnu.org/licenses/>.
 
 =head1 AUTHOR
 
-Nicolas Boisselier nicolas.boisselier@gmail.com
+Nicolas Boisselier <nicolas.boisselier@gmail.com>
 
 =cut
index 5715cd0fe4c49c7692b008292edaefd23b52bbd6..34aff7d2c0b61d9f5b947bb9b6cc584c7dfc32ac 100755 (executable)
@@ -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