Files
pterodactyl-panel/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php
T

158 lines
5.8 KiB
PHP
Raw Normal View History

2018-01-21 16:02:03 -06:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-01-21 16:02:03 -06:00
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Validation\ServerRules;
use UnexpectedValueException;
2018-01-21 16:02:03 -06:00
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
{
/**
2018-05-13 10:50:56 -04:00
* Return the rules to validate this request against.
2026-10-05 18:10:18 -04:00
*
* @return ValidationRules
2018-01-21 16:02:03 -06:00
*/
public function rules(): array
{
2026-10-05 18:10:18 -04:00
$rules = ServerRules::rules($this->parameter('server', Server::class));
2018-01-21 16:02:03 -06:00
return [
'allocation' => $rules['allocation_id'],
'oom_disabled' => $rules['oom_disabled'],
2026-10-05 18:10:18 -04:00
'limits' => ['sometimes', 'array'],
'limits.memory' => $this->requiredToOptional('memory', $rules['memory'], true),
'limits.swap' => $this->requiredToOptional('swap', $rules['swap'], true),
'limits.io' => $this->requiredToOptional('io', $rules['io'], true),
'limits.cpu' => $this->requiredToOptional('cpu', $rules['cpu'], true),
2020-03-29 14:41:55 -04:00
'limits.threads' => $this->requiredToOptional('threads', $rules['threads'], true),
'limits.disk' => $this->requiredToOptional('disk', $rules['disk'], true),
// Legacy rules to maintain backwards compatable API support without requiring
// a major version bump.
//
// @see https://github.com/pterodactyl/panel/issues/1500
'memory' => $this->requiredToOptional('memory', $rules['memory']),
'swap' => $this->requiredToOptional('swap', $rules['swap']),
'io' => $this->requiredToOptional('io', $rules['io']),
'cpu' => $this->requiredToOptional('cpu', $rules['cpu']),
2020-03-29 14:41:55 -04:00
'threads' => $this->requiredToOptional('threads', $rules['threads']),
'disk' => $this->requiredToOptional('disk', $rules['disk']),
2026-10-05 18:10:18 -04:00
'add_allocations' => ['bail', 'array'],
'add_allocations.*' => ['integer'],
'remove_allocations' => ['bail', 'array'],
'remove_allocations.*' => ['integer'],
2026-10-05 18:10:18 -04:00
'feature_limits' => ['required', 'array'],
'feature_limits.databases' => $rules['database_limit'],
'feature_limits.allocations' => $rules['allocation_limit'],
'feature_limits.backups' => $rules['backup_limit'],
2018-01-21 16:02:03 -06:00
];
}
/**
* Convert the allocation field into the expected format for the service handler.
2026-10-05 18:10:18 -04:00
*
* @return ServerBuildModificationData
2018-01-21 16:02:03 -06:00
*/
2026-10-05 18:10:18 -04:00
public function payload(): array
2018-01-21 16:02:03 -06:00
{
2026-10-05 18:10:18 -04:00
$data = [
'database_limit' => $this->filled('feature_limits.databases') ? $this->integer('feature_limits.databases') : null,
'allocation_limit' => $this->filled('feature_limits.allocations') ? $this->integer('feature_limits.allocations') : null,
'backup_limit' => $this->filled('feature_limits.backups') ? $this->integer('feature_limits.backups') : null,
];
if ($this->filled('allocation')) {
$data['allocation_id'] = $this->integer('allocation');
}
if ($this->has('oom_disabled')) {
$data['oom_disabled'] = $this->boolean('oom_disabled');
}
foreach (['memory', 'swap', 'io', 'cpu', 'disk'] as $field) {
if ($this->filled("limits.{$field}")) {
$data[$field] = $this->integer("limits.{$field}");
} elseif ($this->filled($field)) {
$data[$field] = $this->integer($field);
}
2026-10-05 18:10:18 -04:00
}
if ($this->has('limits.threads') || $this->has('threads')) {
$field = $this->has('limits.threads') ? 'limits.threads' : 'threads';
$data['threads'] = $this->filled($field) ? $this->string($field)->toString() : null;
}
2026-10-05 18:10:18 -04:00
foreach (['add_allocations', 'remove_allocations'] as $field) {
if ($this->has($field)) {
$data[$field] = $this->integerList($field);
}
}
2018-01-21 16:02:03 -06:00
return $data;
}
/**
* Custom attributes to use in error message responses.
*/
2022-10-14 10:59:20 -06:00
public function attributes(): array
2018-01-21 16:02:03 -06:00
{
return [
'add_allocations' => 'allocations to add',
'remove_allocations' => 'allocations to remove',
'add_allocations.*' => 'allocation to add',
'remove_allocations.*' => 'allocation to remove',
'feature_limits.databases' => 'Database Limit',
'feature_limits.allocations' => 'Allocation Limit',
'feature_limits.backups' => 'Backup Limit',
2018-01-21 16:02:03 -06:00
];
}
/**
* Converts existing rules for certain limits into a format that maintains backwards
* compatability with the old API endpoint while also supporting a more correct API
* call.
*
* @see https://github.com/pterodactyl/panel/issues/1500
2026-10-05 18:10:18 -04:00
*
* @param ValidationRuleSet $rules
* @return ValidationRuleSet
*/
2022-10-14 10:59:20 -06:00
protected function requiredToOptional(string $field, array $rules, bool $limits = false): array
{
2026-10-05 18:10:18 -04:00
if (! in_array('required', $rules, true)) {
return $rules;
}
2026-10-05 18:10:18 -04:00
$optional = [$limits ? 'required_with:limits' : 'required_without:limits'];
foreach ($rules as $rule) {
if ($rule !== 'required') {
$optional[] = $rule;
}
}
return $optional;
}
/** @return list<int> */
private function integerList(string $field): array
{
$values = $this->input($field, []);
throw_if(! is_array($values) || ! array_is_list($values), UnexpectedValueException::class, "The validated [{$field}] field must be a list.");
$integers = [];
foreach ($values as $value) {
throw_unless(is_int($value), UnexpectedValueException::class, "The validated [{$field}] field must contain integers.");
$integers[] = $value;
}
return $integers;
}
2018-01-21 16:02:03 -06:00
}