-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK SIO-4353 support configurable options in resize
- Loading branch information
Showing
11 changed files
with
345 additions
and
51 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
modules/servers/solusiovps/lib/SolusAPI/Requests/ConfigOptionExtractor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
modules/servers/solusiovps/lib/SolusAPI/Requests/CustomPlanData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
modules/servers/solusiovps/lib/SolusAPI/Requests/ServerResizeRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.