2018-01-21 22:02:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2018-01-21 22:02:03 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-03-02 22:44:59 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-01-21 22:02:03 +00:00
|
|
|
|
|
|
|
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
|
|
|
|
{
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Return the rules to validate this request against.
|
2018-01-21 22:02:03 +00:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2022-12-15 00:05:46 +00:00
|
|
|
$rules = Server::getRulesForUpdate($this->route()->parameter('server')->id);
|
2018-01-21 22:02:03 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
'allocation' => $rules['allocation_id'],
|
2019-08-03 20:41:24 +00:00
|
|
|
'oom_disabled' => $rules['oom_disabled'],
|
2019-03-02 22:44:59 +00: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 18:41:55 +00:00
|
|
|
'limits.threads' => $this->requiredToOptional('threads', $rules['threads'], true),
|
2019-03-02 22:44:59 +00:00
|
|
|
'limits.disk' => $this->requiredToOptional('disk', $rules['disk'], true),
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
// Legacy rules to maintain backwards compatible API support without requiring
|
2019-03-02 22:44:59 +00:00
|
|
|
// 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 18:41:55 +00:00
|
|
|
'threads' => $this->requiredToOptional('threads', $rules['threads']),
|
2019-03-02 22:44:59 +00:00
|
|
|
'disk' => $this->requiredToOptional('disk', $rules['disk']),
|
|
|
|
|
2018-01-21 22:02:03 +00:00
|
|
|
'add_allocations' => 'bail|array',
|
|
|
|
'add_allocations.*' => 'integer',
|
|
|
|
'remove_allocations' => 'bail|array',
|
|
|
|
'remove_allocations.*' => 'integer',
|
2019-03-02 22:44:59 +00:00
|
|
|
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits' => 'required|array',
|
|
|
|
'feature_limits.databases' => $rules['database_limit'],
|
|
|
|
'feature_limits.allocations' => $rules['allocation_limit'],
|
2020-10-17 21:23:00 +00:00
|
|
|
'feature_limits.backups' => $rules['backup_limit'],
|
2018-01-21 22:02:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the allocation field into the expected format for the service handler.
|
2022-12-15 00:05:46 +00:00
|
|
|
*
|
|
|
|
* @param string|null $key
|
|
|
|
* @param string|array|null $default
|
|
|
|
*
|
|
|
|
* @return mixed
|
2018-01-21 22:02:03 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function validated($key = null, $default = null)
|
2018-01-21 22:02:03 +00:00
|
|
|
{
|
|
|
|
$data = parent::validated();
|
|
|
|
|
|
|
|
$data['allocation_id'] = $data['allocation'];
|
2020-10-17 21:23:00 +00:00
|
|
|
$data['database_limit'] = $data['feature_limits']['databases'] ?? null;
|
|
|
|
$data['allocation_limit'] = $data['feature_limits']['allocations'] ?? null;
|
|
|
|
$data['backup_limit'] = $data['feature_limits']['backups'] ?? null;
|
2018-03-02 02:08:27 +00:00
|
|
|
unset($data['allocation'], $data['feature_limits']);
|
2018-01-21 22:02:03 +00:00
|
|
|
|
2019-03-02 22:44:59 +00:00
|
|
|
// Adjust the limits field to match what is expected by the model.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($data['limits'])) {
|
2019-03-02 22:44:59 +00:00
|
|
|
foreach ($data['limits'] as $key => $value) {
|
|
|
|
$data[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($data['limits']);
|
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
if (!is_null($key)) {
|
|
|
|
return Arr::get($data, $key, $default);
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:02:03 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom attributes to use in error message responses.
|
2022-12-15 00:05:46 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2018-01-21 22:02:03 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function attributes()
|
2018-01-21 22:02:03 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'add_allocations' => 'allocations to add',
|
|
|
|
'remove_allocations' => 'allocations to remove',
|
|
|
|
'add_allocations.*' => 'allocation to add',
|
|
|
|
'remove_allocations.*' => 'allocation to remove',
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits.databases' => 'Database Limit',
|
|
|
|
'feature_limits.allocations' => 'Allocation Limit',
|
2020-10-17 21:23:00 +00:00
|
|
|
'feature_limits.backups' => 'Backup Limit',
|
2018-01-21 22:02:03 +00:00
|
|
|
];
|
|
|
|
}
|
2019-03-02 22:44:59 +00: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.
|
|
|
|
*
|
2022-12-15 00:05:46 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2019-03-02 22:44:59 +00:00
|
|
|
* @see https://github.com/pterodactyl/panel/issues/1500
|
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
protected function requiredToOptional(string $field, array $rules, bool $limits = false)
|
2019-03-02 22:44:59 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!in_array('required', $rules)) {
|
2019-03-02 22:44:59 +00:00
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new Collection($rules))
|
|
|
|
->filter(function ($value) {
|
|
|
|
return $value !== 'required';
|
|
|
|
})
|
|
|
|
->prepend($limits ? 'required_with:limits' : 'required_without:limits')
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-01-21 22:02:03 +00:00
|
|
|
}
|