619 lines
21 KiB
PHP
Executable File
619 lines
21 KiB
PHP
Executable File
<?php declare(strict_types=1); namespace IR\App\Webservices; if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
|
|
/**
|
|
* @framework iResponse Framework
|
|
* @version 1.0
|
|
* @author Amine Idrissi <contact@iresponse.tech>
|
|
* @date 2019
|
|
* @name Ovh.php
|
|
*/
|
|
|
|
# core
|
|
use IR\Core\Base as Base;
|
|
use IR\Core\Application as Application;
|
|
|
|
# models
|
|
use IR\App\Models\Admin\OvhAccount as OvhAccount;
|
|
use IR\App\Models\Admin\OvhProcess as OvhProcess;
|
|
use IR\App\Models\Admin\Domain as Domain;
|
|
use \Ovh\Api as OvhApi;
|
|
|
|
# helpers
|
|
use IR\App\Helpers\Authentication as Authentication;
|
|
use IR\App\Helpers\Permissions as Permissions;
|
|
use IR\App\Helpers\Page as Page;
|
|
use IR\App\Helpers\Api as Api;
|
|
|
|
/**
|
|
* @name Ovh
|
|
* @description Ovh WebService
|
|
*/
|
|
class Ovh extends Base
|
|
{
|
|
/**
|
|
* @app
|
|
* @readwrite
|
|
*/
|
|
protected $app;
|
|
|
|
/**
|
|
* @name init
|
|
* @description initializing process before the action method executed
|
|
* @once
|
|
* @protected
|
|
*/
|
|
public function init()
|
|
{
|
|
# set the current application to a local variable
|
|
$this->app = Application::getCurrent();
|
|
}
|
|
|
|
/**
|
|
* @name createInstances
|
|
* @description create instances
|
|
* @before init
|
|
*/
|
|
|
|
private function getOvhApi($account)
|
|
{
|
|
# fetch account
|
|
# todo:- check account if it exists
|
|
# $account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?', $accountId]);
|
|
|
|
# create api instance
|
|
return new OvhApi(
|
|
$account['application_key'],
|
|
$account['application_secret'],
|
|
$account['endpoint'],
|
|
$account['consumer_key']);
|
|
}
|
|
public function createInstances($parameters = [])
|
|
{
|
|
// var_dump($parameters);
|
|
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(),'OvhInstances','create');
|
|
|
|
if($access == false)
|
|
{
|
|
Page::printApiResults(403,'Access Denied !');
|
|
}
|
|
$accountID = intval($parameters['account-id']);
|
|
$account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?',$accountID],['id']);
|
|
|
|
if(!is_array($account) || count($account) == 0)
|
|
{
|
|
Page::printApiResults(500,'Account not found !');
|
|
}
|
|
|
|
$nbInstances = intval($parameters['nb-of-instances']);
|
|
|
|
if($nbInstances == 0)
|
|
{
|
|
Page::printApiResults(500,'Please provide a number of instances to create !');
|
|
}
|
|
|
|
$domains = $parameters['domains'];
|
|
|
|
if(!is_array($domains) || count($domains) == 0)
|
|
{
|
|
Page::printApiResults(500,'Please provide at least one region !');
|
|
}
|
|
|
|
$region = $parameters['region'];
|
|
|
|
if($region == null || $region == '')
|
|
{
|
|
Page::printApiResults(500,'Please provide a region !');
|
|
}
|
|
|
|
$os = $parameters['os'];
|
|
|
|
if($os == null || $os == '')
|
|
{
|
|
Page::printApiResults(500,'Please provide an operating system to install with !');
|
|
}
|
|
|
|
$size = $parameters['size'];
|
|
|
|
if($size == null || $size == '')
|
|
{
|
|
Page::printApiResults(500,'Please provide an instance size to install with !');
|
|
}
|
|
|
|
# create a process object
|
|
$process = new OvhProcess();
|
|
$process->setStatus('In Progress');
|
|
$process->setAccountId($account['id']);
|
|
$process->setRegion($region);
|
|
$process->setNbInstances($nbInstances);
|
|
$process->setDomains(implode(',',$domains));
|
|
$process->setOs($os);
|
|
$process->setSize($size);
|
|
$process->setProgress('0%');
|
|
$process->setInstancesCreated('0');
|
|
$process->setInstancesInstalled('0');
|
|
$process->setStartTime(date('Y-m-d H:i:s'));
|
|
$process->setFinishTime(null);
|
|
//var_dump($process);
|
|
# call iresponse api
|
|
Api::call('Ovh','createInstances',['process-id' => $process->insert()],true,LOGS_PATH . DS . 'cloud_apis' . DS . 'inst_lind_' . $account['id'] . '.log');
|
|
Page::printApiResults(200,'Instances Creation process(es) started');
|
|
}
|
|
|
|
/**
|
|
* @name stopInstancesProcesses
|
|
* @description stop aws instances creation processes action
|
|
* @before init
|
|
*/
|
|
public function stopInstancesProcesses($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(),'OvhInstances','create');
|
|
|
|
if($access == false)
|
|
{
|
|
Page::printApiResults(403,'Access Denied !');
|
|
}
|
|
|
|
$processesIds = $this->app->utils->arrays->get($parameters,'processes-ids',[]);
|
|
|
|
if(!is_array($processesIds) || count($processesIds) == 0)
|
|
{
|
|
Page::printApiResults(500,'No processes found !');
|
|
}
|
|
|
|
# call iresponse api
|
|
$result = Api::call('Ovh','stopProcesses',['processes-ids' => $processesIds]);
|
|
|
|
if(count($result) == 0)
|
|
{
|
|
Page::printApiResults(500,'No response found !');
|
|
}
|
|
|
|
if($result['httpStatus'] == 500)
|
|
{
|
|
Page::printApiResults(500,$result['message']);
|
|
}
|
|
|
|
Page::printApiResults(200,$result['message']);
|
|
}
|
|
|
|
/**
|
|
* @name executeInstancesActions
|
|
* @description execute aws instances actions
|
|
* @before init
|
|
*/
|
|
public function executeInstancesActions($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(),'OvhInstances','main');
|
|
|
|
if($access == false)
|
|
{
|
|
Page::printApiResults(403,'Access Denied !');
|
|
}
|
|
|
|
$instancesIds = $this->app->utils->arrays->get($parameters,'instances-ids',[]);
|
|
|
|
if(!is_array($instancesIds) || count($instancesIds) == 0)
|
|
{
|
|
Page::printApiResults(500,'No processes found !');
|
|
}
|
|
|
|
$action = $this->app->utils->arrays->get($parameters,'action','');
|
|
|
|
if($action == null || $action == '')
|
|
{
|
|
Page::printApiResults(500,'Please provide an action !');
|
|
}
|
|
|
|
# call iresponse api
|
|
$result = Api::call('Ovh','executeInstancesActions',['instances-ids' => $instancesIds,'action' => $action]);
|
|
|
|
if(count($result) == 0)
|
|
{
|
|
Page::printApiResults(500,'No response found !');
|
|
}
|
|
|
|
if($result['httpStatus'] == 500)
|
|
{
|
|
Page::printApiResults(500,$result['message']);
|
|
}
|
|
|
|
Page::printApiResults(200,$result['message']);
|
|
}
|
|
|
|
/**
|
|
* @name getAccountDomains
|
|
* @description get account domains action
|
|
* @before init
|
|
*/
|
|
public function getAccountDomains($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(),'OvhInstances','create');
|
|
|
|
if($access == false)
|
|
{
|
|
Page::printApiResults(403,'Access Denied !');
|
|
}
|
|
|
|
$parts = explode('|',$this->app->utils->arrays->get($parameters,'account'));
|
|
|
|
if(count($parts) != 2)
|
|
{
|
|
Page::printApiResults(500,'Incorrect account !');
|
|
}
|
|
|
|
$accountId = intval($parts[1]);
|
|
$accountType = $parts[0];
|
|
|
|
if($accountId > 0 || $accountType == 'none')
|
|
{
|
|
$where = $accountType == 'none' ? ['status = ? and account_type = ? and availability = ?',['Activated',$accountType,'Available']] :
|
|
['status = ? and account_id = ? and account_type = ? and availability = ?',['Activated',$accountId,$accountType,'Available']];
|
|
$domains = Domain::all(Domain::FETCH_ARRAY,$where,['id','value']);
|
|
|
|
if(count($domains) == 0)
|
|
{
|
|
Page::printApiResults(500,'Domains not found !');
|
|
}
|
|
|
|
Page::printApiResults(200,'',['domains' => $domains]);
|
|
}
|
|
else
|
|
{
|
|
Page::printApiResults(500,'Incorrect account id !');
|
|
}
|
|
}
|
|
/**
|
|
* @name getAccountRegion
|
|
* @description get account regions action
|
|
* @before init
|
|
*/
|
|
public function getAccountRegion($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if (!Authentication::isUserAuthenticated()) {
|
|
Page::printApiResults(401, 'Only logged-in access allowed!');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(), 'OvhInstances', 'create');
|
|
|
|
if ($access == false) {
|
|
Page::printApiResults(403, 'Access Denied!');
|
|
}
|
|
|
|
$accountId = intval($parameters['ovh-accounts']);
|
|
|
|
if ($accountId > 0) {
|
|
$accounts = OvhAccount::first(OvhAccount::FETCH_ARRAY, ['id = ?', $accountId], ['project_key', 'region', 'secret_key', 'application_key', 'consumer_key']);
|
|
|
|
if (count($accounts) == 0) {
|
|
Page::printApiResults(500, 'Account not found!');
|
|
}
|
|
|
|
$account['application_key'] = $accounts['application_key'];
|
|
$account['application_secret'] = $accounts['secret_key'];
|
|
$account['endpoint'] = "ovh-" . $accounts['region'];
|
|
$account['consumer_key'] = $accounts['consumer_key'];
|
|
|
|
$url = '/cloud/project/' . $accounts['project_key'] . '/region';
|
|
$ovh = $this->getOvhApi($account);
|
|
|
|
$regions = $ovh->get($url);
|
|
|
|
// Fetch quota for each region
|
|
$regionsWithQuota = [];
|
|
foreach ($regions as $region) {
|
|
$quotaUrl = '/cloud/project/' . $accounts['project_key'] . '/region/' . $region . '/quota';
|
|
$quota = $ovh->get($quotaUrl);
|
|
|
|
// Format quota information
|
|
$formattedQuota = [
|
|
'region' => $region,
|
|
'instance' => [
|
|
'usedCores' => $quota['instance']['usedCores'],
|
|
'maxCores' => $quota['instance']['maxCores'],
|
|
'usedInstances' => $quota['instance']['usedInstances'],
|
|
'maxInstances' => $quota['instance']['maxInstances'],
|
|
'usedRAM' => $quota['instance']['usedRAM'],
|
|
'maxRAM' => $quota['instance']['maxRam'],
|
|
],
|
|
'volume' => [
|
|
'usedGigabytes' => $quota['volume']['usedGigabytes'],
|
|
'maxGigabytes' => $quota['volume']['maxGigabytes'],
|
|
'usedBackupGigabytes' => $quota['volume']['usedBackupGigabytes'],
|
|
'maxBackupGigabytes' => $quota['volume']['maxBackupGigabytes'],
|
|
],
|
|
'network' => [
|
|
'usedFloatingIPs' => $quota['network']['usedFloatingIPs'],
|
|
'maxFloatingIPs' => $quota['network']['maxFloatingIPs'],
|
|
],
|
|
'loadbalancer' => [
|
|
'usedLoadbalancers' => $quota['loadbalancer']['usedLoadbalancers'],
|
|
'maxLoadbalancers' => $quota['loadbalancer']['maxLoadbalancers'],
|
|
],
|
|
];
|
|
|
|
$regionsWithQuota[] = $formattedQuota;
|
|
}
|
|
|
|
Page::printApiResults(200, '', ['Regions' => $regionsWithQuota]);
|
|
} else {
|
|
Page::printApiResults(500, 'Incorrect account id!');
|
|
}
|
|
}
|
|
/**
|
|
* @name getFlavorsForRegion
|
|
* @description get account flavors action
|
|
* @before init
|
|
*/
|
|
public function getFlavorsForRegion($parameters = []) {
|
|
# check for authentication
|
|
if (!Authentication::isUserAuthenticated()) {
|
|
Page::printApiResults(401, 'Only logged-in access allowed!');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(), 'OvhInstances', 'create');
|
|
|
|
if ($access == false) {
|
|
Page::printApiResults(403, 'Access Denied!');
|
|
}
|
|
$accountId = intval($parameters['account-id']);
|
|
$regionId = $parameters['region-id'];
|
|
|
|
// $accountId = $this->app->utils->arrays->get($parameters, 'account-id');
|
|
// $regionId = $this->app->utils->arrays->get($parameters, 'region-id');
|
|
|
|
if ($accountId>0) {
|
|
$accounts = OvhAccount::first(OvhAccount::FETCH_ARRAY, ['id = ?', $accountId], ['project_key', 'region', 'secret_key', 'application_key', 'consumer_key']);
|
|
|
|
if (empty($accounts)) {
|
|
Page::printApiResults(404, 'Account not found!');
|
|
}
|
|
|
|
// Initialize OVH API client
|
|
$account['application_key'] = $accounts['application_key'];
|
|
$account['application_secret'] = $accounts['secret_key'];
|
|
$account['endpoint'] = "ovh-" . $accounts['region'];
|
|
$account['consumer_key'] = $accounts['consumer_key'];
|
|
|
|
|
|
|
|
// Fetch all flavors for the project
|
|
$url = '/cloud/project/' . $accounts['project_key'] . '/flavor';
|
|
$ovh = $this->getOvhApi($account);
|
|
|
|
$flavors = $ovh->get($url);
|
|
|
|
$processedFlavors = [];
|
|
|
|
// Process flavors
|
|
foreach ($flavors as $flavor) {
|
|
// Skip flavors that don't match the selected region
|
|
if ($flavor['region'] != $regionId) {
|
|
continue;
|
|
}
|
|
|
|
// Skip flavors that are not Linux-compatible
|
|
if ($flavor['osType'] != 'linux') {
|
|
continue;
|
|
}
|
|
|
|
// Skip flavors that are not available
|
|
if ($flavor['available'] != true) {
|
|
continue;
|
|
}
|
|
|
|
// Format flavor description
|
|
$ramDesc = 'RAM: ' . ($flavor['ram'] / 1024) . ' GB'; // Convert RAM to GB
|
|
$diskDesc = 'Disk: ' . $flavor['disk'] . ' GB';
|
|
$vcpus = 'VCPUs: ' . $flavor['vcpus'];
|
|
$desc = $flavor['name'] . ' - ' . $ramDesc . ', ' . $diskDesc . ', ' . $vcpus;
|
|
|
|
// Add to processed flavors
|
|
$processedFlavors[] = [
|
|
'id' => $flavor['id'],
|
|
'name' => $desc
|
|
];
|
|
}
|
|
// Return the processed flavors
|
|
Page::printApiResults(200, 'Success', ['Flavors' => $processedFlavors]);
|
|
}
|
|
else {
|
|
Page::printApiResults(500, 'Incorrect account id!');
|
|
|
|
}
|
|
|
|
}
|
|
/**
|
|
* @name createOvhCredentials
|
|
* @description get account cred action
|
|
* @before init
|
|
*/
|
|
public function createOvhCredentials($parameters = []) {
|
|
# check for authentication
|
|
if (!Authentication::isUserAuthenticated()) {
|
|
Page::printApiResults(401, 'Only logged-in access allowed!');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(), 'OvhInstances', 'create');
|
|
|
|
if ($access == false) {
|
|
Page::printApiResults(403, 'Access Denied!');
|
|
}
|
|
try {
|
|
|
|
// Initialize OVH API client
|
|
|
|
$applicationKey = $parameters['ovh-application-key'];
|
|
$applicationSecret = $parameters['ovh-secret-key'];
|
|
$endpoint = $parameters['ovh-region'];
|
|
$consumerKey = $parameters['ovh-consumer-key'];
|
|
|
|
|
|
|
|
$account['application_key'] = $applicationKey;
|
|
$account['application_secret'] = $applicationSecret;
|
|
$account['endpoint'] = "ovh-" . $endpoint;
|
|
$account['consumer_key'] = $consumerKey;
|
|
|
|
|
|
$accessRules = [
|
|
['method' => 'GET', 'path' => '/*'],
|
|
['method' => 'POST', 'path' => '/*'],
|
|
['method' => 'PUT', 'path' => '/*'],
|
|
['method' => 'DELETE', 'path' => '/*']
|
|
];
|
|
// Fetch all flavors for the project
|
|
$ovh = $this->getOvhApi($account);
|
|
|
|
$credentials = $ovh->requestCredentials($accessRules);
|
|
|
|
Page::printApiResults(200,'Credentials created successfully!',$credentials);
|
|
|
|
}catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
// Return error response
|
|
$response = $e->getResponse();
|
|
$responseBodyAsString = $response->getBody()->getContents();
|
|
Page::printApiResults(500,'Error',$responseBodyAsString);
|
|
|
|
}
|
|
|
|
}
|
|
/**
|
|
* @name getImageForRegion
|
|
* @description get account flavors action
|
|
* @before init
|
|
*/
|
|
public function getImageForRegion($parameters = []) {
|
|
# check for authentication
|
|
if (!Authentication::isUserAuthenticated()) {
|
|
Page::printApiResults(401, 'Only logged-in access allowed!');
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(), 'OvhInstances', 'create');
|
|
|
|
if ($access == false) {
|
|
Page::printApiResults(403, 'Access Denied!');
|
|
}
|
|
$accountId = intval($parameters['account-id']);
|
|
$regionId = $parameters['region-id'];
|
|
|
|
// $accountId = $this->app->utils->arrays->get($parameters, 'account-id');
|
|
// $regionId = $this->app->utils->arrays->get($parameters, 'region-id');
|
|
|
|
if ($accountId>0) {
|
|
$accounts = OvhAccount::first(OvhAccount::FETCH_ARRAY, ['id = ?', $accountId], ['project_key', 'region', 'secret_key', 'application_key', 'consumer_key']);
|
|
|
|
if (empty($accounts)) {
|
|
Page::printApiResults(404, 'Account not found!');
|
|
}
|
|
|
|
// Initialize OVH API client
|
|
$account['application_key'] = $accounts['application_key'];
|
|
$account['application_secret'] = $accounts['secret_key'];
|
|
$account['endpoint'] = "ovh-" . $accounts['region'];
|
|
$account['consumer_key'] = $accounts['consumer_key'];
|
|
|
|
|
|
|
|
// Fetch all flavors for the project
|
|
$url = '/cloud/project/' . $accounts['project_key'] . '/image';
|
|
$ovh = $this->getOvhApi($account);
|
|
|
|
$images = $ovh->get($url);
|
|
|
|
$processedImage = [];
|
|
|
|
// Process images
|
|
foreach ($images as $image) {
|
|
// Skip images that don't match the selected region
|
|
if ($image['region'] != $regionId) {
|
|
continue;
|
|
}
|
|
|
|
// Skip images that are not Linux-compatible
|
|
if ($image['type'] != 'linux') {
|
|
continue;
|
|
}
|
|
|
|
if ($image['name'] != 'Centos 7') {
|
|
continue;
|
|
}
|
|
|
|
// Format image description
|
|
$desc = $image['name'];
|
|
|
|
// Add to processed images
|
|
$processedImage[] = [
|
|
'id' => $image['id'],
|
|
'name' => $desc
|
|
];
|
|
}
|
|
// Return the processed images
|
|
Page::printApiResults(200, 'Success', ['Images' => $processedImage]);
|
|
}
|
|
else {
|
|
Page::printApiResults(500, 'Incorrect account id!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|