Skip to content

Commit d62f4c2

Browse files
committed
feat: add create-consumer-key.py helper, improve documentation
Signed-off-by: Jean-Tiare Le Bigot <[email protected]>
1 parent d5a65f5 commit d62f4c2

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

README.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ as required by your usage for a better security!
4646

4747
- `OVH Europe <https://eu.api.ovh.com/createToken/index.cgi?GET=/*&POST=/*&DELETE=/*&PUT=/*>`_
4848
- `OVH North America <https://ca.api.ovh.com/createToken/index.cgi?GET=/*&POST=/*&DELETE=/*&PUT=/*>`_
49-
- `Runabove <https://api.runabove.com/createToken/index.cgi?GET=/*&POST=/*&DELETE=/*&PUT=/*>`_
49+
- `Runabove <https://api.runabove.com/createApp>`_
50+
51+
For Runabove, you may use included ``create-consumer-key.py runabove-ca`` to generate your consumer key.
5052

5153
Then you should put your credentials in ``~/.ovh.conf`` like:
5254

create-consumer-key.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
'''
4+
Helper to generate OVH API consumer key. In a nutshell, the consumer key
5+
identifies a specific user in an application while application key and
6+
application secret identifies the application itself. In the case of ovh-cli
7+
each instance of the CLI must hav its own, dedicated, set of credentials.
8+
9+
To generate application secret and application key, please visit:
10+
- OVH Europe: https://eu.api.ovh.com/createApp/
11+
- OVH North America: https://ca.api.ovh.com/createApp/
12+
- Soyoustart Europe: https://eu.api.soyoustart.com/createApp/
13+
- Soyoustart North America: https://ca.api.soyoustart.com/createApp/
14+
- Kimsufi Europe: https://eu.api.kimsufi.com/createApp/
15+
- Kimsufi North America: https://ca.api.kimsufi.com/createApp/
16+
- Runabove North America: https://api.runabove.com/createApp/
17+
18+
You may then request a consumer key using this tool:
19+
20+
$ create-consumer-key.py [endpoint]
21+
22+
Where ``endpoint`` may be one of ``ovh-eu``, ``ovh-ca``, and so on.
23+
24+
Once generated, your application key, application secret and consumer key
25+
must be set in eiter:
26+
- ``./ovh.conf`` for an application specific configuration
27+
- ``$HOME/.ovh.conf`` for a user wide configuration
28+
- ``/etc/ovh.conf`` for a system wide / server configuration
29+
30+
This file will look like:
31+
32+
[default]
33+
endpoint=ovh-eu
34+
35+
[ovh-eu]
36+
application_key=my_app_key
37+
application_secret=my_application_secret
38+
;consumer_key=my_consumer_key
39+
40+
Alternatively, at runtime, configuration may be overloaded using environment
41+
variables. For more informations regarding available configuration options,
42+
please see https://github.com/ovh/python-ovh
43+
'''
44+
45+
import sys
46+
import ovh
47+
48+
# Load api endpoint from command line, if any
49+
if len(sys.argv) == 1:
50+
endpoint=None
51+
elif len(sys.argv) == 2:
52+
endpoint=sys.argv[1]
53+
else:
54+
print >>sys.stderr, __doc__
55+
sys.exit(1)
56+
57+
if endpoint in ['-h', '--help']:
58+
print >>sys.stderr, __doc__
59+
sys.exit(0)
60+
61+
# Create a client using configuration
62+
try:
63+
client = ovh.Client(endpoint)
64+
except Exception as e:
65+
print e
66+
print >>sys.stderr, __doc__
67+
sys.exit(1)
68+
69+
# Request full API access
70+
access_rules = [
71+
{'method': 'GET', 'path': '/*'},
72+
{'method': 'POST', 'path': '/*'},
73+
{'method': 'PUT', 'path': '/*'},
74+
{'method': 'DELETE', 'path': '/*'}
75+
]
76+
77+
# Request token
78+
validation = client.request_consumerkey(access_rules)
79+
80+
print "Please visit %s to authenticate" % validation['validationUrl']
81+
raw_input("and press Enter to continue...")
82+
83+
# Print nice welcome message
84+
print "Welcome", client.get('/me')['firstname']
85+
print "Here is your Consumer Key: '%s'" % validation['consumerKey']
86+

ovh-cli.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33
'''
4-
OVH CLI. This is a convenient Command Line Interface (CLI) built on top of
4+
OVH CLI is a convenient Command Line Interface (CLI) built on top of
55
``python-ovh`` and OVH's ReST APIs.
66
77
Available command list is generated at runtime based on automatically updated
88
json schemas of the API.
99
1010
The name of the API to use is determined by the executable name. For instance,
11-
if runing program is called 'ovh-eu', it will expose european OVH's API', it
12-
will expose european OVH's API's. Currently supported APIs includes:
11+
if runing program is called 'ovh-eu', it will expose european OVH's API'.
12+
Currently supported APIs includes:
1313
- ovh-eu
1414
- ovh-ca
1515
- kimsufi-eu
@@ -18,9 +18,6 @@
1818
- soyoustart-ca
1919
- runabove-ca
2020
21-
TODO:
22-
- list / complete 'enum' arguments
23-
2421
Usage: General: {cli} [--help|--refresh|--format (pretty|json)] your command and args --param value --param2 value2
2522
Get help on a specific path: {cli} your command --help
2623
Get help on a specific action: {cli} your command (list|show|update|create|delete) --help
@@ -33,8 +30,12 @@
3330
--help This message
3431
--refresh Rebuild available commands list and documentation
3532
--format Output format, can be 'pretty' or 'json'. (default='pretty')
33+
--debug Print verbose debugging informations. Use it when reporting a bug
3634
'''
3735

36+
# TODO:
37+
# - list / complete 'enum' arguments
38+
3839
from __future__ import absolute_import
3940

4041
import os
@@ -185,7 +186,7 @@ def do_usage():
185186
print >>sys.stderr, 'Calling %s %s' % (verb, method)
186187
formater.do_format(client, verb, method, arguments.__dict__)
187188
except Exception as e:
188-
# print noce error message
189+
# print nice error message
189190
print e
190191

191192
# when in debug mode, re-raise to see the full stack-trace

0 commit comments

Comments
 (0)