Lightweight wrapper around OVHcloud's APIs. Handles all the hard work including
credential creation and requests signing.
import ovh
# Instantiate. Visit https://api.ovh.com/createToken/?GET=/me
# to get your credentials
client = ovh.Client(
endpoint='ovh-eu',
application_key='<application key>',
application_secret='<application secret>',
consumer_key='<consumer key>',
)
# Print nice welcome message
print("Welcome", client.get('/me')['firstname'])
Installation
The python wrapper works with Python 3.7+.
The easiest way to get the latest stable release is to grab it from pypi using pip.
Alternatively, you may get latest development version directly from Git.
pip install -e git+https://github.com/ovh/python-ovh.git#egg=ovh
People looking for Python 2 compatibility should use 0.6.x version.
Example Usage
Use the API on behalf of a user
1. Create an application
To interact with the APIs, the SDK needs to identify itself using an
application_key and an application_secret. To get them, you need
to register your application. Depending the API you plan to use, visit:
Once created, you will obtain an application key (AK) and an application
secret (AS).
2. Configure your application
The easiest and safest way to use your application's credentials is to create an
ovh.conf configuration file in application's working directory. Here is how
it looks like:
[default]
; general configuration: default endpoint
endpoint=ovh-eu
[ovh-eu]
; configuration specific to 'ovh-eu' endpoint
application_key=my_app_key
application_secret=my_application_secret
; uncomment following line when writing a script application
; with a single consumer key.
;consumer_key=my_consumer_key
; uncomment to enable oauth2 authentication
;client_id=my_client_id
;client_secret=my_client_secret
Depending on the API you want to use, you may set the endpoint to:
ovh-eu for OVHcloud Europe API
ovh-us for OVHcloud US API
ovh-ca for OVHcloud North-America API
soyoustart-eu for So you Start Europe API
soyoustart-ca for So you Start North America API
kimsufi-eu for Kimsufi Europe API
kimsufi-ca for Kimsufi North America API
See Configuration for more information on available configuration mechanisms.
Note
When using a versioning system, make sure to add ovh.conf to ignored
files. It contains confidential/security-sensitive information!
3. Authorize your application to access a customer account using OAuth2
python-ovh supports two forms of authentication:
- OAuth2, using scopped service accounts, and compatible with OVHcloud IAM
- application key & application secret & consumer key (covered in the next chapter)
For OAuth2, first, you need to generate a pair of valid client_id and client_secret: you
can proceed by [following this documentation](https://help.ovhcloud.com/csm/en-manage-service-account?id=kb_article_view&sysparm_article=KB0059343)
Once you have retrieved your client_id and client_secret, you can create and edit
a configuration file that will be used by python-ovh.
4. Authorize your application to access a customer account using custom OVHcloud authentication
To allow your application to access a customer account using the API on your
behalf, you need a consumer key (CK).
Here is a sample code you can use to allow your application to access a
customer's information:
import ovh
# create a client using configuration
client = ovh.Client()
# Request RO, /me API access
ck = client.new_consumer_key_request()
ck.add_rules(ovh.API_READ_ONLY, "/me")
# Request token
validation = ck.request()
print("Please visit %s to authenticate" % validation['validationUrl'])
input("and press Enter to continue...")
# Print nice welcome message
print("Welcome", client.get('/me')['firstname'])
print("Btw, your 'consumerKey' is '%s'" % validation['consumerKey'])
Returned consumerKey should then be kept to avoid re-authenticating your
end-user on each use.
Note
To request full and unlimited access to the API, you may use add_recursive_rules:
# Allow all GET, POST, PUT, DELETE on /* (full API)
ck.add_recursive_rules(ovh.API_READ_WRITE, '/')
Install a new mail redirection
e-mail redirections may be freely configured on domains and DNS zones hosted by
OVHcloud to an arbitrary destination e-mail using API call
POST /email/domain/{domain}/redirection.
For this call, the api specifies that the source address shall be given under the
from keyword. Which is a problem as this is also a reserved Python keyword.
In this case, simply prefix it with a '_', the wrapper will automatically detect
it as being a prefixed reserved keyword and will substitute it. Such aliasing
is only supported with reserved keywords.
import ovh
DOMAIN = "example.com"
SOURCE = "sales@example.com"
DESTINATION = "contact@example.com"
# create a client
client = ovh.Client()
# Create a new alias
client.post('/email/domain/%s/redirection' % DOMAIN,
_from=SOURCE,
to=DESTINATION,
localCopy=False
)
print("Installed new mail redirection from %s to %s" % (SOURCE, DESTINATION))
Grab bill list
Let's say you want to integrate OVHcloud bills into your own billing system, you
could just script around the /me/bills endpoints and even get the details
of each bill lines using /me/bill/{billId}/details/{billDetailId}.
This example assumes an existing Configuration with valid application_key,
application_secret and consumer_key.
import ovh
# create a client
client = ovh.Client()
# Grab bill list
bills = client.get('/me/bill')
for bill in bills:
details = client.get('/me/bill/%s' % bill)
print("%12s (%s): %10s --> %s" % (
bill,
details['date'],
details['priceWithTax']['text'],
details['pdfUrl'],
))
Enable network burst in SBG1
'Network burst' is a free service but is opt-in. What if you have, say, 10
servers in SBG-1 datacenter? You certainly don't want to activate it
manually for each servers. You could take advantage of a code like this.
This example assumes an existing Configuration with valid application_key,
application_secret and consumer_key.
import ovh
# create a client
client = ovh.Client()
# get list of all server names
servers = client.get('/dedicated/server/')
# find all servers in SBG-1 datacenter
for server in servers:
details = client.get('/dedicated/server/%s' % server)
if details['datacenter'] == 'sbg1':
# enable burst on server
client.put('/dedicated/server/%s/burst' % server, status='active')
print("Enabled burst for %s server located in SBG-1" % server)
List application authorized to access your account
Thanks to the application key / consumer key mechanism, it is possible to
finely track applications having access to your data and revoke this access.
This examples lists validated applications. It could easily be adapted to
manage revocation too.
This example assumes an existing Configuration with valid application_key,
application_secret and consumer_key.
import ovh
from tabulate import tabulate
# create a client
client = ovh.Client()
credentials = client.get('/me/api/credential', status='validated')
# pretty print credentials status
table = []
for credential_id in credentials:
credential_method = '/me/api/credential/'+str(credential_id)
credential = client.get(credential_method)
application = client.get(credential_method+'/application')
table.append([
credential_id,
'[%s] %s' % (application['status'], application['name']),
application['description'],
credential['creation'],
credential['expiration'],
credential['lastUse'],
])
print(tabulate(table, headers=['ID', 'App Name', 'Description',
'Token Creation', 'Token Expiration', 'Token Last Use']))
Before running this example, make sure you have the
tabulate library installed. It's a
pretty cool library to pretty print tabular data in a clean and easy way.
Open a KVM (remote screen) on a dedicated server
Recent dedicated servers come with an IPMI interface. A lightweight control board embedded
on the server. Using IPMI, it is possible to get a remote screen on a server. This is
particularly useful to tweak the BIOS or troubleshoot boot issues.
Hopefully, this can easily be automated using a simple script. It assumes Java Web Start is
fully installed on the machine and a consumer key allowed on the server exists.