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

157 lines
6.1 KiB
PHP
Raw Normal View History

2018-01-28 17:14:14 -06:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-01-28 17:14:14 -06:00
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Support\Arr;
2018-01-28 17:14:14 -06:00
use Illuminate\Validation\Rule;
2022-05-30 16:24:59 +02:00
use Illuminate\Validation\Validator;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Data\ServerCreationData;
2018-01-28 17:14:14 -06:00
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Concerns\FiltersDeployTags;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Validation\AllocationRules;
use Pterodactyl\Validation\ServerRules;
2018-01-28 17:14:14 -06:00
class StoreServerRequest extends ApplicationApiRequest
{
2026-10-05 18:10:18 -04:00
use FiltersDeployTags;
2022-10-14 10:59:20 -06:00
protected ?string $resource = AdminAcl::RESOURCE_SERVERS;
2018-01-28 17:14:14 -06:00
2022-10-14 10:59:20 -06:00
protected int $permission = AdminAcl::WRITE;
2018-01-28 17:14:14 -06:00
/**
* Rules to be applied to this request.
2026-10-05 18:10:18 -04:00
*
* @return ValidationRules
2018-01-28 17:14:14 -06:00
*/
public function rules(): array
{
2026-10-05 18:10:18 -04:00
$rules = ServerRules::rules();
2018-01-28 17:14:14 -06:00
return [
'external_id' => $rules['external_id'],
2018-01-28 17:14:14 -06:00
'name' => $rules['name'],
'description' => array_merge(['nullable'], $rules['description']),
'user' => $rules['owner_id'],
'egg' => $rules['egg_id'],
'docker_image' => $rules['image'],
'startup' => $rules['startup'],
2026-10-05 18:10:18 -04:00
'environment' => ['present', 'array'],
'skip_scripts' => ['sometimes', 'boolean'],
'oom_disabled' => ['sometimes', 'boolean'],
2018-01-28 17:14:14 -06:00
// Resource limitations
2026-10-05 18:10:18 -04:00
'limits' => ['required', 'array'],
2018-01-28 17:14:14 -06:00
'limits.memory' => $rules['memory'],
'limits.swap' => $rules['swap'],
'limits.disk' => $rules['disk'],
'limits.io' => $rules['io'],
2020-03-29 14:41:55 -04:00
'limits.threads' => $rules['threads'],
2018-01-28 17:14:14 -06:00
'limits.cpu' => $rules['cpu'],
2018-03-02 23:11:30 -06:00
// Application Resource Limits
2026-10-05 18:10:18 -04:00
'feature_limits' => ['required', 'array'],
2018-03-02 23:11:30 -06:00
'feature_limits.databases' => $rules['database_limit'],
'feature_limits.allocations' => $rules['allocation_limit'],
'feature_limits.backups' => $rules['backup_limit'],
2018-03-02 23:11:30 -06:00
2026-10-05 18:10:18 -04:00
// Allocations are chosen by hand unless a deployment object picks them.
'allocation.default' => ['exclude_with:deploy', 'required', 'integer', 'bail', AllocationRules::unassigned()],
'allocation.additional.*' => ['exclude_with:deploy', 'integer', AllocationRules::unassigned()],
2018-02-10 14:01:49 -06:00
2018-01-28 17:14:14 -06:00
// Automatic deployment rules
2026-10-05 18:10:18 -04:00
'deploy' => ['sometimes', 'required', 'array'],
'deploy.locations' => ['present_with:deploy', 'array', 'list'],
'deploy.locations.*' => ['required', Rule::anyOf([['integer:strict'], ['string']]), 'integer', 'min:1'],
'deploy.dedicated_ip' => ['required_with:deploy', 'boolean'],
'deploy.port_range' => ['present_with:deploy', 'array', 'list'],
'deploy.port_range.*' => ['string'],
// Deploy tags (D): the reservations this deployment opts into. Each may
// be given as a tag id or a slug; existence is checked in after()
// so the message can name the offending value.
'deploy.tags' => ['array'],
'deploy.tags.*' => [$this->validateDeployTagReference(...)],
'start_on_completion' => ['sometimes', 'boolean'],
2018-01-28 17:14:14 -06:00
];
}
/**
* Normalize the data into a format that can be consumed by the service.
2026-10-05 18:10:18 -04:00
*
* @return ServerCreationAttributes
2018-01-28 17:14:14 -06:00
*/
2026-10-05 18:10:18 -04:00
public function payload(): array
2018-01-28 17:14:14 -06:00
{
$data = parent::validated();
2026-10-05 18:10:18 -04:00
$payload = [
'external_id' => Arr::get($data, 'external_id'),
'name' => Arr::get($data, 'name'),
'description' => Arr::get($data, 'description'),
'owner_id' => Arr::get($data, 'user'),
'egg_id' => Arr::get($data, 'egg'),
'image' => Arr::get($data, 'docker_image'),
'startup' => Arr::get($data, 'startup'),
'environment' => Arr::get($data, 'environment'),
'memory' => Arr::get($data, 'limits.memory'),
'swap' => Arr::get($data, 'limits.swap'),
'disk' => Arr::get($data, 'limits.disk'),
'io' => Arr::get($data, 'limits.io'),
'cpu' => Arr::get($data, 'limits.cpu'),
'threads' => Arr::get($data, 'limits.threads'),
'skip_scripts' => Arr::get($data, 'skip_scripts', false),
'allocation_id' => Arr::get($data, 'allocation.default'),
'allocation_additional' => Arr::get($data, 'allocation.additional'),
'start_on_completion' => Arr::get($data, 'start_on_completion', false),
'database_limit' => Arr::get($data, 'feature_limits.databases'),
'allocation_limit' => Arr::get($data, 'feature_limits.allocations'),
'backup_limit' => Arr::get($data, 'feature_limits.backups'),
'oom_disabled' => Arr::get($data, 'oom_disabled'),
2018-01-28 17:14:14 -06:00
];
2026-10-05 18:10:18 -04:00
JsonValueGuard::assertPayload9($payload);
return ServerCreationData::parse($payload);
2018-01-28 17:14:14 -06:00
}
2026-10-05 18:10:18 -04:00
/**
* @return list<Closure(Validator): void>
2018-01-28 17:14:14 -06:00
*/
2026-10-05 18:10:18 -04:00
public function after(): array
2018-01-28 17:14:14 -06:00
{
2026-10-05 18:10:18 -04:00
return [$this->validateDeployTagsExist(...)];
2018-01-28 17:14:14 -06:00
}
/**
* Return a deployment object that can be passed to the server creation service.
*/
2022-10-14 10:59:20 -06:00
public function getDeploymentObject(): ?DeploymentObject
2018-01-28 17:14:14 -06:00
{
2026-10-05 18:10:18 -04:00
if (($this->input('deploy')) === null) {
2018-01-28 17:14:14 -06:00
return null;
}
2026-10-05 18:10:18 -04:00
$object = new DeploymentObject;
$object->setDedicated($this->boolean('deploy.dedicated_ip'));
$object->setLocations(JsonValueGuard::normalizedIntegerList(JsonValueGuard::integerStringList($this->input('deploy.locations', []))));
$object->setPorts(JsonValueGuard::stringList($this->input('deploy.port_range', [])));
$object->setTags($this->resolveDeployTagSlugs());
2018-01-28 17:14:14 -06:00
return $object;
}
2026-10-05 18:10:18 -04:00
/** @phpstan-assert int|string $value */
private function validateDeployTagReference(string $attribute, mixed $value, Closure $fail): void
{
if (! is_int($value) && ! is_string($value)) {
$fail("The {$attribute} field must be a tag ID or slug.");
}
}
2018-01-28 17:14:14 -06:00
}