Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK SIO-4353 support configurable options in resize #83

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// Copyright 2021. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests;

use WHMCS\Module\Server\SolusIoVps\Helpers\Arr;

class ConfigOptionExtractor
{
public static function extractFromModuleParams(array $moduleParams, string $optionName)
{
return Arr::get($moduleParams, sprintf('configoptions.%s', $optionName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

// Copyright 2021. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests;

use WHMCS\Module\Server\SolusIoVps\Database\Models\ProductConfigOption;

class CustomPlanData
{
public static function fromModuleParams(array $moduleParams): ?array
{
$customPlanData = [];

if ($vcpu = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::VCPU)) {
$customPlanData['params']['vcpu'] = (int)$vcpu;
}
if ($ram = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::MEMORY)) {
$customPlanData['params']['ram'] = $ram * 1024 * 1024;
}
if ($disk = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::DISK_SPACE)) {
$customPlanData['params']['disk'] = (int)$disk;
}
if ($vcpuUnits = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::VCPU_UNITS)) {
$customPlanData['params']['vcpu_units'] = (int)$vcpuUnits;
}
if ($vcpuLimit = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::VCPU_LIMIT)) {
$customPlanData['params']['vcpu_limit'] = (int)$vcpuLimit;
}
if ($ioPriority = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::IO_PRIORITY)) {
$customPlanData['params']['io_priority'] = (int)$ioPriority;
}
if ($swap = ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::SWAP)) {
$customPlanData['params']['swap'] = $swap * 1024 * 1024;
}
if ($totalTrafficLimitMonthly =
ConfigOptionExtractor::extractFromModuleParams($moduleParams, ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY)
) {
$customPlanData['limits']['network_total_traffic'] = [
'limit' => (int)$totalTrafficLimitMonthly,
];
}

return count($customPlanData) > 0 ? $customPlanData : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ public static function fromWHMCSCreateAccountParams(array $params): self
$builder->withOperatingSystem($osId, $userData);
}

if ($additionalIpCount = self::getConfigOption($params, ProductConfigOption::EXTRA_IP_ADDRESS)) {
if ($additionalIpCount = ConfigOptionExtractor::extractFromModuleParams($params, ProductConfigOption::EXTRA_IP_ADDRESS)) {
$builder->withAdditionalIps($additionalIpCount);
}

$customPlanData = self::extractCustomPlanData($params);
$customPlanData = CustomPlanData::fromModuleParams($params);
if ($customPlanData) {
$builder->withCustomPlan($customPlanData);
}
Expand Down Expand Up @@ -257,45 +257,4 @@ public function get(): array

return $request;
}

private static function extractCustomPlanData(array $params): ?array
{
$customPlanData = [];

if ($vcpu = self::getConfigOption($params, ProductConfigOption::VCPU)) {
$customPlanData['params']['vcpu'] = (int)$vcpu;
}
if ($ram = self::getConfigOption($params, ProductConfigOption::MEMORY)) {
$customPlanData['params']['ram'] = $ram * 1024 * 1024;
}
if ($disk = self::getConfigOption($params, ProductConfigOption::DISK_SPACE)) {
$customPlanData['params']['disk'] = (int)$disk;
}
if ($vcpuUnits = self::getConfigOption($params, ProductConfigOption::VCPU_UNITS)) {
$customPlanData['params']['vcpu_units'] = (int)$vcpuUnits;
}
if ($vcpuLimit = self::getConfigOption($params, ProductConfigOption::VCPU_LIMIT)) {
$customPlanData['params']['vcpu_limit'] = (int)$vcpuLimit;
}
if ($ioPriority = self::getConfigOption($params, ProductConfigOption::IO_PRIORITY)) {
$customPlanData['params']['io_priority'] = (int)$ioPriority;
}
if ($swap = self::getConfigOption($params, ProductConfigOption::SWAP)) {
$customPlanData['params']['swap'] = $swap * 1024 * 1024;
}
if ($totalTrafficLimitMonthly =
self::getConfigOption($params, ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY)
) {
$customPlanData['limits']['network_total_traffic'] = [
'limit' => (int)$totalTrafficLimitMonthly,
];
}

return count($customPlanData) > 0 ? $customPlanData : null;
}

private static function getConfigOption(array $params, string $optionName)
{
return Arr::get($params, sprintf('configoptions.%s', $optionName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

// Copyright 2021. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests;

use WHMCS\Module\Server\SolusIoVps\Helpers\Arr;

class ServerResizeRequestBuilder
{
/**
* @var int
*/
private $planId;

/**
* @var ?array
*/
private $customPlanData;

private function __construct(int $planId)
{
$this->planId = $planId;
}

public static function fromWHMCSUpgradeDowngradeParams(array $params): self
{
$planId = (int)Arr::get($params, 'configoption1');
$builder = new self($planId);

$customPlanData = CustomPlanData::fromModuleParams($params);
if ($customPlanData) {
$builder->withCustomPlan($customPlanData);
}

return $builder;
}

public function get(): array
{
$request = [
'plan_id' => $this->planId,
'preserve_disk' => !isset($this->customPlanData['params']['disk']),
];

if ($this->customPlanData) {
$request['custom_plan'] = $this->customPlanData;
}

return $request;
}

public function withCustomPlan(array $customPlanData): self
{
$this->customPlanData = $customPlanData;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ public function changeHostname(int $id, string $hostname): array
]));
}

public function resize(int $id, int $planId): array
public function resize(int $id, array $data): array
{
return $this->processResponse($this->connector->post("servers/{$id}/resize", [
'json' => [
'preserve_disk' => true,
'plan_id' => $planId,
],
'json' => $data,
]));
}

Expand Down
5 changes: 4 additions & 1 deletion modules/servers/solusiovps/lib/WhmcsAPI/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public static function upgrade(int $upgradeId): void
$serverParams = Server::getParams($serverId);
$serverResource = new ServerResource(Connector::create($serverParams));

$serverResource->resize($server->server_id, $newPlanId);
$serverResource->resize($server->server_id, [
'plan_id' => $newPlanId,
'preserve_disk' => false,
]);
}
}
25 changes: 25 additions & 0 deletions modules/servers/solusiovps/solusiovps.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WHMCS\Module\Server\SolusIoVps\Logger\Logger;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Helpers\Strings;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ServerCreateRequestBuilder;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ServerResizeRequestBuilder;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\UserRequestBuilder;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ApplicationResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\LimitGroupResource;
Expand Down Expand Up @@ -620,3 +621,27 @@ function solusiovps_syncAccount(array $params)
];
}
}

function solusiovps_ChangePackage(array $params)
{
try {
if ($_REQUEST['type'] !== 'configoptions') {
return 'success';
}

$server = SolusServer::getByServiceId((int)$params['serviceid']);
if ($server === null) {
return Language::trans('solusiovps_error_server_not_found');
}
$solusServerId = $server->server_id;
$serverResource = new ServerResource(Connector::create($params));

// Handle plan params
$requestBuilder = ServerResizeRequestBuilder::fromWHMCSUpgradeDowngradeParams($params);
$serverResource->resize($solusServerId, $requestBuilder->get());

return 'success';
} catch (Exception $e) {
return $e->getMessage();
}
}
26 changes: 26 additions & 0 deletions tests/lib/SolusAPI/Requests/ConfigOptionExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Copyright 2021. Plesk International GmbH.

namespace Tests\lib\SolusAPI\Requests;

use PHPUnit\Framework\TestCase;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ConfigOptionExtractor;

class ConfigOptionExtractorTest extends TestCase
{
/**
* @testWith ["foo", "bar"]
* ["unknown", null]
*/
public function testExtractFromModuleParams(string $optionName, mixed $expectedValue): void
{
$moduleParams = [
'configoptions' => [
'foo' => 'bar',
],
];

self::assertEquals($expectedValue, ConfigOptionExtractor::extractFromModuleParams($moduleParams, $optionName));
}
}
81 changes: 81 additions & 0 deletions tests/lib/SolusAPI/Requests/CustomPlanDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

// Copyright 2021. Plesk International GmbH.

namespace Tests\lib\SolusAPI\Requests;

use PHPUnit\Framework\TestCase;
use WHMCS\Module\Server\SolusIoVps\Database\Models\ProductConfigOption;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\CustomPlanData;

/**
* @runTestsInSeparateProcesses
*/
class CustomPlanDataTest extends TestCase
{
public function testFromModuleParams(): void
{
$moduleParams = [
'configoptions' => [
ProductConfigOption::VCPU => 1,
ProductConfigOption::MEMORY => 2,
ProductConfigOption::DISK_SPACE => 2,
ProductConfigOption::VCPU_UNITS => 8,
ProductConfigOption::VCPU_LIMIT => 10,
ProductConfigOption::IO_PRIORITY => 6,
ProductConfigOption::SWAP => 4,
ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY => 5,
],
];

self::assertEquals([
'params' => [
'vcpu' => $moduleParams['configoptions'][ProductConfigOption::VCPU],
'ram' => $moduleParams['configoptions'][ProductConfigOption::MEMORY] * 1024 * 1024,
'disk' => $moduleParams['configoptions'][ProductConfigOption::DISK_SPACE],
'vcpu_units' => $moduleParams['configoptions'][ProductConfigOption::VCPU_UNITS],
'vcpu_limit' => $moduleParams['configoptions'][ProductConfigOption::VCPU_LIMIT],
'io_priority' => $moduleParams['configoptions'][ProductConfigOption::IO_PRIORITY],
'swap' => $moduleParams['configoptions'][ProductConfigOption::SWAP] * 1024 * 1024,
],
'limits' => [
'network_total_traffic' => [
'limit' => $moduleParams['configoptions'][ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY],
],
],
], CustomPlanData::fromModuleParams($moduleParams));
}

public function testFromModuleParamsEmptyConfigOptions(): void
{
self::assertEquals(null, CustomPlanData::fromModuleParams([
'configoptions' => [],
]));
}

public function testFromModuleParamsNoConfigOptions(): void
{
self::assertEquals(null, CustomPlanData::fromModuleParams([]));
}

public function testFromModuleParamsSomeValues(): void
{
$moduleParams = [
'configoptions' => [
ProductConfigOption::VCPU => 1,
ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY => 5,
],
];

self::assertEquals([
'params' => [
'vcpu' => $moduleParams['configoptions'][ProductConfigOption::VCPU],
],
'limits' => [
'network_total_traffic' => [
'limit' => $moduleParams['configoptions'][ProductConfigOption::TOTAL_TRAFFIC_LIMIT_MONTHLY],
],
],
], CustomPlanData::fromModuleParams($moduleParams));
}
}
Loading