255 lines
7.4 KiB
PHP
Executable File
255 lines
7.4 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 Eshipillah Samson <contact@mediagroup.tech>
|
|
* @date 2022
|
|
* @name HetznerServers.php
|
|
*/
|
|
|
|
# core
|
|
use IR\Core\Base as Base;
|
|
use IR\Core\Application as Application;
|
|
|
|
# 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\Jobs as Jobs;
|
|
use IR\App\Helpers\AuditLog as AuditLog;
|
|
|
|
# orm
|
|
use IR\Orm\Sequence as Sequence;
|
|
use IR\Orm\Query as Query;
|
|
|
|
# http
|
|
use IR\Http\Request as Request;
|
|
|
|
# exceptions
|
|
use IR\Exceptions\Types\SystemException as SystemException;
|
|
|
|
# models
|
|
use IR\App\Models\Admin\OvhAccount as OvhAccount;
|
|
use IR\App\Models\Admin\OvhServer as OvhServer;
|
|
use IR\App\Models\Admin\OvhSshKey as OvhSshKey;
|
|
use IR\App\Models\Admin\Domain as Domain;
|
|
|
|
# logging
|
|
use IR\Logs\Logger as Logger;
|
|
|
|
# ovh api
|
|
use \Ovh\Api as OvhApi;
|
|
|
|
# helpers
|
|
use IR\App\Helpers\Caching as Caching;
|
|
|
|
# utilities
|
|
use IR\Utils\System\Terminal as Terminal;
|
|
|
|
|
|
/**
|
|
* @name OvhServers
|
|
* @description OvhServers WebService
|
|
*/
|
|
class OvhServers extends Base
|
|
{
|
|
/**
|
|
* @app
|
|
* @readwrite
|
|
*/
|
|
protected $app;
|
|
|
|
/**
|
|
* @name init
|
|
* @description initializing proccess before the action method executed
|
|
* @once
|
|
* @protected
|
|
*/
|
|
public function init()
|
|
{
|
|
# set the current application to a local variable
|
|
$this->app = Application::getCurrent();
|
|
}
|
|
|
|
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'],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @name getSshKeys
|
|
* @description getSshKeys action
|
|
* @before init
|
|
*/
|
|
public function getSshKeys($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# todo:- check permissions
|
|
|
|
$accountId = $this->app->utils->arrays->get($parameters,'account-id');
|
|
$account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?', $accountId]);
|
|
$ovh = $this->getOvhApi($account);
|
|
$url = '/cloud/project/' . $account['project_id'] . '/sshkey';
|
|
$sshKeys = $ovh->get($url);
|
|
Page::printApiResults(200, 'success', $sshKeys);
|
|
}
|
|
|
|
/**
|
|
* @name getRegions
|
|
* @description getRegions action
|
|
* @before init
|
|
*/
|
|
public function getRegions($parameters = []) {
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# todo:- check permissions
|
|
|
|
$accountId = $this->app->utils->arrays->get($parameters,'account-id');
|
|
$account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?', $accountId]);
|
|
$ovh = $this->getOvhApi($account);
|
|
$url = '/cloud/project/' . $account['project_id'] . '/region';
|
|
$result = $ovh->get($url);
|
|
Page::printApiResults(200, 'success', $result);
|
|
}
|
|
|
|
/**
|
|
* @name getImages
|
|
* @description getImages action
|
|
* @before init
|
|
*/
|
|
public function getImages($parameters = []) {
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# todo:- check permissions
|
|
|
|
$accountId = $this->app->utils->arrays->get($parameters,'account-id');
|
|
$regionId = $this->app->utils->arrays->get($parameters,'region-id');
|
|
|
|
$account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?', $accountId]);
|
|
$ovh = $this->getOvhApi($account);
|
|
|
|
$url = '/cloud/project/' . $account['project_id'] . '/image';
|
|
$images = $ovh->get($url);
|
|
$imagesForRegion = [];
|
|
foreach($images as $image) {
|
|
if ($image['type'] != 'linux') {
|
|
continue;
|
|
}
|
|
|
|
if ($image['region'] == $regionId) {
|
|
$imagesForRegion[] = $image;
|
|
}
|
|
}
|
|
Page::printApiResults(200, 'success', $imagesForRegion);
|
|
}
|
|
|
|
/**
|
|
* @name getFlavors
|
|
* @description getRegions action
|
|
* @before init
|
|
*/
|
|
public function getFlavors($parameters = []) {
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
}
|
|
|
|
# todo:- check permissions
|
|
|
|
$accountId = $this->app->utils->arrays->get($parameters,'account-id');
|
|
$regionId = $this->app->utils->arrays->get($parameters,'region-id');
|
|
|
|
$account = OvhAccount::first(OvhAccount::FETCH_ARRAY,['id = ?', $accountId]);
|
|
$ovh = $this->getOvhApi($account);
|
|
$url = '/cloud/project/' . $account['project_id'] . '/flavor';
|
|
$result = $ovh->get($url);
|
|
$processedFlavors = [];
|
|
foreach($result as $flavor) {
|
|
if ($flavor['region'] != $regionId) {
|
|
continue;
|
|
}
|
|
|
|
if ($flavor['osType'] != 'linux') {
|
|
continue;
|
|
}
|
|
|
|
if ($flavor['available'] != true) {
|
|
continue;
|
|
}
|
|
|
|
if ($flavor['name'] != 'd2-4') {
|
|
continue;
|
|
}
|
|
|
|
$ramDes = 'Ram: ' . $flavor['ram'] / 1000 . 'GB';
|
|
$diskDesc = 'Disk: ' . $flavor['disk'] . 'GB';
|
|
$vcpus = 'VCPUS: ' . $flavor['vcpus'];
|
|
$desc = $flavor['name'] . ' - ' . $ramDes . ', ' . $diskDesc . ', ' . $vcpus;
|
|
|
|
$processedFlavors[] = [
|
|
'id' => $flavor['id'],
|
|
'name' => $desc
|
|
];
|
|
}
|
|
Page::printApiResults(200, 'success', $processedFlavors);
|
|
}
|
|
|
|
/**
|
|
* @name addServers
|
|
* @description addServers action
|
|
* @before init
|
|
*/
|
|
public function addServers($parameters = [])
|
|
{
|
|
# check for authentication
|
|
if(!Authentication::isUserAuthenticated())
|
|
{
|
|
Page::printApiResults(401,'Only logged-in access allowed !');
|
|
return;
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# check for permissions
|
|
//$access = Permissions::checkForAuthorization(Authentication::getAuthenticatedUser(),'HetznerServers','addServers');
|
|
|
|
// if($access == false)
|
|
// {
|
|
// Page::printApiResults(403,'Access Denied !');
|
|
// }
|
|
|
|
# serialize parameters to JSON
|
|
$parameters['authenticated-user-email'] = Authentication::getAuthenticatedUser()->getEmail();
|
|
$parametersJson = json_encode($parameters);
|
|
Caching::getInstance()->set('ovh-add-servers-parameters', $parametersJson);
|
|
|
|
# run background process
|
|
Jobs::callJobsProccess('OvhServers', 'addServers', [], true);
|
|
Page::printApiResults(200, "The server(s) are being added!");
|
|
}
|
|
} |