Post

Python venv OVH API

Python venv OVH API

Les API disponibles sur https://api.ovh.com/ vous permettent d’acheter, gérer, mettre à jour et configurer des produits OVHcloud sans utiliser une interface graphique comme l’espace client.

OVH API

Préalables

  1. Disposer d’un compte OVH
  2. Créer un token pour accès API: https://www.ovh.com/auth/api/createToken?GET=/*&PUT=/*&POST=/*&DELETE=/*

Exemple

1
curl -X GET "https://eu.api.ovh.com/v1/domain/icevps.xyz"  -H "accept: application/json" -H "authorization: Bearer eyJhbGciOiJFZERTQSIsImtpZCI6IkVGNThFMkUxMTFBODNCREFEMDE4OUUzMzZERTk3MDhFNjRDMDA4MDEiLCJraW5kIjoib2F1dGgyIiwidHlwIjoiSldUIn0.eyJBY2Nlc...ArEsT1X2_asA-dTNHvQTah5RTaSiZzDg" |jq

OVH API Python

Environnement virtuel python

Prérequis,venv pour votre version de python

1
sudo apt install python3-venv 

Créer un dossier ovh_api

1
2
3
sudo mkdir /home/leno/ovh_api
sudo chown $USER:$USER /home/leno/ovh_api
cd /home/leno/ovh_api

Pour créer un environnement, utilisez la commande python -m venv <environment name> Créer un environnement pour l’application

1
python3 -m venv ApiOvh

activer l’environnement virtuel

1
source ApiOvh/bin/activate

On arrive sur un prompt

Mettre à jour pip dans l’environnement

1
python -m pip install --upgrade pip

Installer ApiOVH

1
pip install ovh

API - Domaine

Get service information

Créer un script python pour lire les infos du domaine

1
nano domain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
First, install the latest release of Python wrapper: $ pip install ovh
'''
import json
import ovh
import sys
from datetime import datetime

# Instantiate an OVH Client.
# You can generate new credentials with full access to your account on
# the token creation page (https://api.ovh.com/createToken/index.cgi?GET=/*&PUT=/*&POST=/*&DELETE=/*)
client = ovh.Client(
	endpoint='ovh-eu',               # Endpoint of API OVH (List of available endpoints: https://github.com/ovh/python-ovh#2-configure-your-application)
	application_key='xxxxxxxxxx',    # Application Key
	application_secret='xxxxxxxxxx', # Application Secret
	consumer_key='xxxxxxxxxx',       # Consumer Key
)

#result = client.get("/domain")
# Pretty print
#print(json.dumps(result, indent=4))

for i in range(1, len(sys.argv)):
    #print('argument:', i, 'value:', sys.argv[i])
    domaine=str(sys.argv[i])
    result = client.get("/domain/" + domaine)
    # Pretty print
    #print(json.dumps(result, indent=4))
    resp = json.loads(json.dumps(result))
   
    # La date d'expiration (resp['expirationDate']) est au format ISO '2024-12-25T16:14:07Z'
    # Extraction des éléments
    dt_object = datetime.fromisoformat(resp['expirationDate'])
    print(f"{domaine} expire le {dt_object.day:02d}/{dt_object.month:02d}/{dt_object.year}")

Exécuter le script

1
python domain.py xoyize.xyz cinay.eu xoyaz.xyz ouestline.xyz rnmkcy.eu yanfi.net icevps.xyz xoyize.net iceyan.xyz

Cet article est sous licence CC BY 4.0 par l'auteur.