Files

160 lines
6.7 KiB
PHP
Raw Permalink 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\Database\Eloquent\Model;
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-11 08:45:00 -04:00
use Pterodactyl\Actions\Servers\CreateServer;
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\Http\Requests\Concerns\ValidatesExtensionFields;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
2018-01-28 17:14:14 -06:00
class StoreServerRequest extends ApplicationApiRequest
{
2026-10-05 18:10:18 -04:00
use FiltersDeployTags;
2026-10-11 08:45:00 -04:00
use ValidatesExtensionFields {
actionInput as private inputWithExtensions;
}
2026-10-05 18:10:18 -04:00
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
/**
2026-10-11 08:45:00 -04:00
* Rules to be applied to this request. The Application API keeps its own field names and
* rules; actionInput() hands the values to the action under its names.
2026-10-05 18:10:18 -04:00
*
* @return ValidationRules
2018-01-28 17:14:14 -06:00
*/
public function rules(): array
{
2026-10-11 08:45:00 -04:00
$rules = CreateServer::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'],
2026-10-11 08:45:00 -04:00
'description' => $rules['description'],
2018-01-28 17:14:14 -06:00
'user' => $rules['owner_id'],
2026-10-11 08:45:00 -04:00
'egg' => ['required', 'exists:eggs,id'],
2018-01-28 17:14:14 -06:00
'docker_image' => $rules['image'],
'startup' => $rules['startup'],
2026-10-05 18:10:18 -04:00
'environment' => ['present', 'array'],
2026-10-11 08:45:00 -04:00
'skip_scripts' => $rules['skip_scripts'],
2026-10-05 18:10:18 -04:00
'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'],
2026-10-11 08:45:00 -04:00
'feature_limits.databases' => ['present', 'nullable', 'integer', 'min:0'],
2018-03-02 23:11:30 -06:00
'feature_limits.allocations' => $rules['allocation_limit'],
2026-10-11 08:45:00 -04:00
'feature_limits.backups' => ['present', 'nullable', 'integer', 'min:0'],
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.
2026-10-11 08:45:00 -04:00
'allocation.default' => ['exclude_with:deploy', ...$rules['allocation_id']],
'allocation.additional.*' => ['exclude_with:deploy', ...$rules['allocation_additional.*']],
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(...)],
2026-10-11 08:45:00 -04:00
'start_on_completion' => $rules['start_on_completion'],
2018-01-28 17:14:14 -06:00
];
}
/**
2026-10-11 08:45:00 -04:00
* The validated input under the action's field names: user and egg are its owner_id and
* egg_id, and docker_image its image. The limits, the feature limits and the default and
* additional allocations move to its top level, read with Laravel's typed accessors. The
* deployment goes to the action as getDeploymentObject().
2026-10-05 18:10:18 -04:00
*
2026-10-11 19:54:55 -04:00
* @return array<string, mixed>
2018-01-28 17:14:14 -06:00
*/
2026-10-11 08:45:00 -04:00
public function actionInput(): array
2018-01-28 17:14:14 -06:00
{
2026-10-11 08:45:00 -04:00
$input = $this->inputWithExtensions();
$validated = $this->safe();
$input['owner_id'] = $input['user'] ?? null;
$input['egg_id'] = $input['egg'] ?? null;
$input['image'] = $input['docker_image'] ?? null;
foreach (['memory', 'swap', 'disk', 'io', 'cpu'] as $limit) {
$input[$limit] = $validated->integer('limits.'.$limit);
}
$input['threads'] = $validated->filled('limits.threads') ? $validated->string('limits.threads')->toString() : null;
$input['database_limit'] = $validated->filled('feature_limits.databases') ? $validated->integer('feature_limits.databases') : null;
$input['allocation_limit'] = $validated->filled('feature_limits.allocations') ? $validated->integer('feature_limits.allocations') : null;
$input['backup_limit'] = $validated->filled('feature_limits.backups') ? $validated->integer('feature_limits.backups') : null;
$input['allocation_id'] = $validated->filled('allocation.default') ? $validated->integer('allocation.default') : null;
$input['allocation_additional'] = array_map(fn (int|string $key): int => $validated->integer('allocation.additional.'.$key), array_keys($validated->array('allocation.additional')));
unset($input['user'], $input['egg'], $input['docker_image'], $input['limits'], $input['feature_limits'], $input['allocation'], $input['deploy']);
return $input;
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'));
2026-10-11 16:17:55 -04:00
$object->setLocations(array_map(fn (int|string $index): int => $this->integer("deploy.locations.{$index}"), array_keys($this->array('deploy.locations'))));
$object->setPorts(array_map(fn (int|string $index): string => $this->string("deploy.port_range.{$index}")->toString(), array_keys($this->array('deploy.port_range'))));
2026-10-05 18:10:18 -04:00
$object->setTags($this->resolveDeployTagSlugs());
2018-01-28 17:14:14 -06:00
return $object;
}
2026-10-05 18:10:18 -04:00
/**
* {@inheritdoc}
*/
protected function extensionFieldsModel(): Model|string
{
return Server::class;
}
2026-10-05 18:10:18 -04:00
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
}