Comment utiliser l'API d’Ikoula

De Ikoula Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

en:How to use the Ikoula API es:Cómo utilizar la API de Ikoula nl:Hoe de Ikoula API te gebruiken? zh:如何使用Ikoula的API it:Come utilizzare l'API Ikoula

Introduction

Ikoula dispose d'une API permettant de réaliser des actions sur les produits associés à votre compte client. Voici l'URL de l'API: https://api.ikoula.com

La documentation est disponible directement pour chaque produit.

Explications

Pour des raisons évidentes de sécurité, l'API Ikoula exige une authentification. Celle-ci est basée sur un identifiant, un mot de passe et une signature :

  • L'identifiant est l'adresse e-mail utilisée pour la connexion à votre compte Ikoula ou à l'Extranet. Le nom du paramètre pour le transmettre est toujours login ;
  • Le mot de passe quant à lui doit être chiffré via une fonction spécifique utilisant une clé publique fournie par Ikoula (paramètre crypted_password) et base64_encode ;
  • La signature est générée en fonction des paramètres fournis lors de l'appel à l'API.
  • Ces paramètres doivent toujours être passés en GET à l'API !


ATTENTION :

Pour vos tests de l'API, vous pouvez, par exemple, utiliser un utilisateur temporaire dédié à ces tests. L'utilisation du chiffrement de mot de passe avec la clé publique Ikoula est indispensable dans tout contexte de production ou non-court terme.
Si les appels API sont voués à être utilisés via un script ou un programme, nous vous recommandons la création d'un utilisateur dédié à cet effet plutôt que d'utiliser votre utilisateur extranet classique.
Deux possibilités s'offrent à vous :

Attention à ne pas oublier de lui mettre les droits sur les prestations souhaitées.

Clé de chiffrement

La clé publique de chiffrement du mot de passe est disponible à l'adresse suivante: https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem

Wrapper PHP

<?php

// #################################################
// ####    ..:: Ikoula Hosting Services ::..     ###
// ####	   Wrapper for https://api.ikoula.com	 ###
// #################################################

class IkoulaAPI {

    /**
     * Email of Ikoula account
     * @var string Email account
     */
    private static $email = "EMAIL_ACCOUNT_IKOULA";

    /**
     * Password of Ikoula account
     * @var string Password account
     */
    private static $password = "PASSWORD_ACCOUNT_IKOULA";

    /**
     * Ikoula API URI
     * @var string Password account
     */ 
    private static $urlApi = "https://api.ikoula.com/";

    /** Public key path for encrypt data
     * @var string Path of public key
     * @see https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem
     */ 
    private static $publicKeyPath = "/path/to/ikoula/public/key/Ikoula.API.RSAKeyPub.pem";
    
    /** Fonction for request Ikoula API
     * @param string $webservice Webservice for data
     * @param string $format JSON or XML
     * @param string $type HTTP Type (GET/POST)
     * @param array $params Params to add for request
	 * @param array|null $body
	 * @param bool $jsonBody
     */
    public static function requestApi($webservice, $format, $method, $params = [], $body = null, $jsonBody = false)
	{
		// Add connexion information
		$params['login'] = self::$email;
		$params['crypted_password'] = base64_encode(self::opensslEncryptPublic(self::$password));
		$params['format'] = $format;

		// Fix params to lowercase for generate signature correctly
		$params = array_change_key_case($params);

		// Curl init
		$ch = curl_init();

		if($ch)
	   	{
			// Create API URI
			$url = self::$urlApi.$webservice;
			$url .= (str_contains($url, '?') ? '&' : '?') . http_build_query($params);
	
			// Add type request
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

			// If we use post, fix params
			if(in_array($method, ['POST', 'PUT', 'DELETE'], true))
			{
				if($body !== null)
				{
					if(!$jsonBody)
					{
						curl_setopt($ch, CURLOPT_POST, true);
						curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
					}
					else
					{
						curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
						curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
					}
				}
			}
   
			// Create curl info
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_HEADER, false);
	
			// Exec request
			$data = curl_exec($ch);
	
			// Close curl object
			curl_close($ch);
	
			// Return response
			return $data;
	   }

	   return null;
   }
    
    /** Fonction for crypt Ikoula password
     * @param string $password Ikoula account password
    * @return mixed Ikoula password encrypted, null if error
    */
    private static function opensslEncryptPublic($password)
    {
        // Verify if key file exist
        if(file_exists(self::$publicKeyPath))
        {
            // Verify if password is not empty
            if(!empty($password))
            {
                // Get file content
                $publicKey = openssl_pkey_get_public('file://'.realpath(self::$publicKeyPath));
    
                // If we get file content without error
                if ($publicKey !== FALSE)      
                {
                    // Encrypt password
                    if(openssl_public_encrypt($password, $crypted, $publicKey) === TRUE)
                        return $crypted;
                    else
                        return NULL;
                }
                else
                    return NULL;
            }
            else
                return NULL;
        }
        else
            return NULL;
    }
}

Exemples d'utilisation

// Fix JSON header
header("Content-type: application/json");

// Get Exch schema for prestation
echo IkoulaAPI::requestApi("wsexch/schema-subscr", "json", "GET", ['subscr_id' => 999999999]);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Platform list
echo IkoulaAPI::requestApi("wsplatform/list", "json", "GET", []);

// Get Platform dossiers
echo IkoulaAPI::requestApi("wsplatform/dossiers", "json", "GET", ['platform_id' => 999999999]);

// Get Billing conso for CloudStack prestation
echo IkoulaAPI::requestApi("wscs/conso-for-billing", "json", "GET", ['subscr_id' => '999999999', 'billing_id' => '1']);

// Reboot server
echo IkoulaAPI::requestApi("wsds/reboot-apc-request", "json", "GET", ['server_ip' => 'XXX.XXX.XXX.XXX', 'tempo' => '3']);

Ajout de fonctionnalités

En cas de demande de fonctionnalité, contactez le support technique.