Skip to content

Commit

Permalink
Adding server sync feature to the module to support import from SolusIO
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Redikultsev committed Jul 26, 2021
1 parent e97c096 commit 823e869
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lang/english.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$_LANG['solusiovps_config_option_default_limit_group'] = 'Default Limit Group';

$_LANG['solusiovps_button_restart'] = 'Restart';
$_LANG['solusiovps_button_sync'] = 'Sync account';
$_LANG['solusiovps_button_vnc'] = 'VNC Access';
$_LANG['solusiovps_button_reinstall'] = 'Reinstall';
$_LANG['solusiovps_button_cancel'] = 'Cancel';
Expand All @@ -39,6 +40,7 @@
$_LANG['solusiovps_error_server_already_created'] = 'A server can be created only once.';
$_LANG['solusiovps_error_server_not_found'] = 'The server was not found.';
$_LANG['solusiovps_error_change_hostname'] = 'Failed to change the hostname';
$_LANG['solusiovps_error_user_not_found'] = 'User with such email not found in SolusIO';

$_LANG['solusiovps_rescue_mode_summary'] = 'Booting from the rescue ISO helps you fix kernel mismatches and corrupted file systems.';
$_LANG['solusiovps_rescue_mode_description'] = 'By default, a server is booted from its disk. If your server was booted from the rescue ISO and you want to boot the server from the disk again, do the following:<br /><br />1. Shut down or reboot your server. To shut down the server, click the Stop button above or use the command line.<br /><br />2. Power on your server. To do so, click the Start button above or boot the server from its disk.';
Expand Down
5 changes: 5 additions & 0 deletions lib/SolusAPI/Resources/ServerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function get(int $id): array
return $this->processResponse($this->connector->get("servers/{$id}"));
}

public function listServerPage(int $page): array
{
return $this->processResponse($this->connector->get("servers?page=$page"));
}

public function getAllByUser(int $userId): array
{
$response = $this->processResponse($this->connector->get("servers", [
Expand Down
91 changes: 90 additions & 1 deletion lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Copyright 2020. Plesk International GmbH.

use Carbon\Carbon;
use \GuzzleHttp\Exception\RequestException;
use WHMCS\Module\Server\SolusIoVps\Exceptions\SolusException;
use WHMCS\Module\Server\SolusIoVps\Helpers\Arr;
Expand Down Expand Up @@ -29,6 +30,7 @@
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Config;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Crypt;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Language;
use WHMCS\Service\Status;

if (!defined('WHMCS')) {
die('This file cannot be accessed directly');
Expand All @@ -54,7 +56,10 @@ function solusiovps_MetaData(): array
'APIVersion' => '1.1',
'RequiresServer' => true,
'ServiceSingleSignOnLabel' => false,
'AdminSingleSignOnLabel' => false
'AdminSingleSignOnLabel' => false,
'ListAccountsUniqueIdentifierDisplayName' => 'Domain',
'ListAccountsUniqueIdentifierField' => 'domain',
'ListAccountsProductField' => 'configoption1',
];
}

Expand Down Expand Up @@ -533,6 +538,7 @@ function solusiovps_UnsuspendAccount(array $params): string
function solusiovps_ClientArea(array $params): array
{
try {
solusiovps_syncAccount($params);
$serverResource = new ServerResource(Connector::create($params));
$server = SolusServer::getByServiceId((int) Arr::get($params, 'serviceid'));

Expand Down Expand Up @@ -609,6 +615,7 @@ function solusiovps_AdminCustomButtonArray(array $params): array
Language::trans('solusiovps_button_vnc') => [
'href' => "javascript:window.open('{$vncUrl}', '', 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=800,height=450');",
],
Language::trans('solusiovps_button_sync') => 'syncAccount',
];
}

Expand All @@ -629,3 +636,85 @@ function solusiovps_restart(array $params)
return $e->getMessage();
}
}

function solusiovps_ListAccounts(array $params)
{
try {
$accounts = [];

$serverParams = Server::getParams((int)$params['serverid']);
$serverResource = new ServerResource(Connector::create($serverParams));

$current_page = 1;
do {
$serversOnPage = $serverResource->listServerPage($current_page);
$lastPage = Arr::get($serversOnPage, 'meta', [])['last_page'];

foreach (Arr::get($serversOnPage, 'data', []) as $server) {
$accounts[] = [
'email' => $server['user']['email'],
'username' => $server['user']['email'],
'domain' => $server['name'],
'uniqueIdentifier' => $server['name'],
'product' => $server['plan']['name'],
'primaryip' => $server['ip_addresses']['ipv4'][0]['ip'],
'created' => Carbon::parse($server['created_at'])->format('Y-m-d H:i:s'),
'status' => !$server['is_suspended'] ? Status::ACTIVE : Status::SUSPENDED,
];
}
$current_page++;
} while ($current_page <= $lastPage);

return [
'success' => true,
'accounts' => $accounts,
];
} catch (Exception $e) {
return [
'success' => false,
'error' => $e->getMessage(),
];
}
}

function solusiovps_syncAccount(array $params)
{
try {
if (!empty(SolusServer::getByServiceId($params['serviceid']))) {
return [
'success' => "Account is already synced",
];
}

$connector = Connector::create(Server::getParams((int)$params['serverid']));
$userResource = new UserResource($connector);
$solusUser = $userResource->getUserByEmail($params['clientsdetails']['email']);
if (!$solusUser) {
throw new Exception(Language::trans('solusiovps_error_user_not_found'));
}

$serverResource = new ServerResource($connector);
$allServersOfUser = $serverResource->getAllByUser($solusUser['id']);

foreach ($allServersOfUser as $server) {
if ($server['name'] == $params['domain']) {
SolusServer::create([
'service_id' => $params['serviceid'],
'server_id' => (int)Arr::get($server, 'id', []),
'payload' => json_encode($server),
]);
return [
'success' => "Account has been synced correctly"
];
}
}
return [
'Success' => "Unable to find the service in SolusIO"
];
} catch (Exception $e) {
return [
'success' => false,
'error' => $e->getMessage(),
];
}
}

0 comments on commit 823e869

Please sign in to comment.