My notepad

Chris' IT notes…
← Back

OVH API notes

Create App: https://eu.api.ovh.com/createApp/ Python wrapper project: https://github.com/ovh/python-ovh Web API control panel: https://eu.api.ovh.com/console/#/

sh
# Where to find the ID of your application created from the portal

/me/api/application

# If you use the python script to get the customer_key
# You can find the ID here, filtering by application ID

/me/api/credential

# Here you can find your serviceName (project ID)
# - I got mad to understand what it was before!

/cloud/project

Example of ovh.conf file

ini
[default]
endpoint=ovh-eu

[ovh-eu]
application_key=my_app_key
application_secret=my_application_secret
;consumer_key=my_consumer_key

;consumer_key needs to be uncommented once you have got it

Custom python script to allow access only to a specific project under my Cloud OVH account

python
# -*- encoding: utf-8 -*-

import ovh

# create a client using configuration
client = ovh.Client()

# Request full access to /cloud/project/<PROJECT_ID>/
ck = client.new_consumer_key_request()
ck.add_recursive_rules(ovh.API_READ_WRITE, '/cloud/project/<PROJECT_ID>/')

## Request full access to ALL
#ck = client.new_consumer_key_request()
# ck.add_recursive_rules(ovh.API_READ_WRITE, '/')

# Request token
validation = ck.request()

print "Please visit %s to authenticate" % validation['validationUrl']
raw_input("and press Enter to continue...")

# Print customerKey
print "Btw, your 'consumerKey' is '%s'" % validation['consumerKey']

How to create a script

  1. Create the app from the link above
  2. Get the keys and store them safely
  3. Install the OVH python wrapper
  4. Create ovh.conf file and use the keys from your app
  5. Use the python example (or mine) to get the customerKey
  6. Update ovh.conf with the customKey
  7. Create your script and have fun! :)

Script example to get a list of snapshots:

get_snaps
python
# -*- encoding: utf-8 -*-
import json
import ovh

serviceName="<PROJECT_ID>"
region="GRA3"

# Auth
client = ovh.Client()

result = client.get("/cloud/project/%s/snapshot" % serviceName,
    flavorType=None,
    region="%s" % region,
)

# Pretty print
print json.dumps(result, indent=4)