Skip to content

Commit

Permalink
TASK SIO-4507 support changing number of additional IP`s during resize
Browse files Browse the repository at this point in the history
  • Loading branch information
vss414 committed Oct 20, 2022
1 parent 5ba6121 commit b8e72c9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,22 @@ public function changeBootMode(int $id, string $bootMode): array
],
]));
}

public function createAdditionalIps(int $id, int $count): array
{
return $this->processResponse($this->connector->post("servers/{$id}/ips", [
'json' => [
'count' => $count,
],
]));
}

public function deleteAdditionalIps(int $id, array $ipIds): array
{
return $this->processResponse($this->connector->delete("servers/{$id}/ips", [
'json' => [
'ids' => $ipIds,
],
]));
}
}
32 changes: 32 additions & 0 deletions modules/servers/solusiovps/solusiovps.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WHMCS\Module\Server\SolusIoVps\Helpers\Unit;
use WHMCS\Module\Server\SolusIoVps\Logger\Logger;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Helpers\Strings;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ConfigOptionExtractor;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ServerCreateRequestBuilder;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\ServerResizeRequestBuilder;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Requests\UserRequestBuilder;
Expand Down Expand Up @@ -638,6 +639,37 @@ function solusiovps_ChangePackage(array $params)
$requestBuilder = ServerResizeRequestBuilder::fromWHMCSUpgradeDowngradeParams($params);
$serverResource->resize($solusServerId, $requestBuilder->get());

// Handle additional IPs
$additionalIpCount = ConfigOptionExtractor::extractFromModuleParams($params, ProductConfigOption::EXTRA_IP_ADDRESS);

if ($additionalIpCount !== null) {
$additionalIpCount = (int)$additionalIpCount;
$solusServer = $serverResource->get($solusServerId);
$serverAdditionalIps = array_filter(Arr::get($solusServer, 'data.ip_addresses.ipv4'), function (array $ip) {
return $ip['is_primary'] === false;
});
$serverAdditionalIpCount = count($serverAdditionalIps);

if ($additionalIpCount > $serverAdditionalIpCount) {
$needIpCount = $additionalIpCount - $serverAdditionalIpCount;

$serverResource->createAdditionalIps($solusServerId, $needIpCount);
} elseif ($additionalIpCount < $serverAdditionalIpCount) {
// Remove IPs from the end
$reversedServerAdditionalIps = array_reverse($serverAdditionalIps);
$reversedServerAdditionalIpsForDelete = array_slice(
$reversedServerAdditionalIps,
0,
$serverAdditionalIpCount - $additionalIpCount
);
$ipIdsForDelete = array_map(static function (array $additionalIp) {
return $additionalIp['id'];
}, $reversedServerAdditionalIpsForDelete);

$serverResource->deleteAdditionalIps($solusServerId, $ipIdsForDelete);
}
}

return 'success';
} catch (Exception $e) {
return $e->getMessage();
Expand Down

0 comments on commit b8e72c9

Please sign in to comment.