Skip to content

Commit 50c5ab9

Browse files
committed
[pterodactyl#1500] Add support for limits array or base level values
1 parent a4d7985 commit 50c5ab9

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
77
### Fixed
88
* Fixes a bug with the location update API endpoint throwing an error due to an unexected response value.
99

10+
### Changed
11+
* `allocation_limit` for servers now defaults to a null value, and is not required in PATCH/POST requests when adding
12+
a server through the API.
13+
* The `PATCH` endpoint for `/api/applications/servers/{server}/build` now accepts an array called `limits` to match
14+
the response from the server `GET` endpoint.
15+
1016
## v0.7.12 (Derelict Dermodactylus)
1117
### Fixed
1218
* Fixes an issue with the locations API endpoint referencing an invalid namespace.

app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
44

55
use Pterodactyl\Models\Server;
6+
use Illuminate\Support\Collection;
67

78
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
89
{
@@ -17,15 +18,29 @@ public function rules(): array
1718

1819
return [
1920
'allocation' => $rules['allocation_id'],
20-
'memory' => $rules['memory'],
21-
'swap' => $rules['swap'],
22-
'io' => $rules['io'],
23-
'cpu' => $rules['cpu'],
24-
'disk' => $rules['disk'],
21+
22+
'limits' => 'sometimes|array',
23+
'limits.memory' => $this->requiredToOptional('memory', $rules['memory'], true),
24+
'limits.swap' => $this->requiredToOptional('swap', $rules['swap'], true),
25+
'limits.io' => $this->requiredToOptional('io', $rules['io'], true),
26+
'limits.cpu' => $this->requiredToOptional('cpu', $rules['cpu'], true),
27+
'limits.disk' => $this->requiredToOptional('disk', $rules['disk'], true),
28+
29+
// Legacy rules to maintain backwards compatable API support without requiring
30+
// a major version bump.
31+
//
32+
// @see https://github.com/pterodactyl/panel/issues/1500
33+
'memory' => $this->requiredToOptional('memory', $rules['memory']),
34+
'swap' => $this->requiredToOptional('swap', $rules['swap']),
35+
'io' => $this->requiredToOptional('io', $rules['io']),
36+
'cpu' => $this->requiredToOptional('cpu', $rules['cpu']),
37+
'disk' => $this->requiredToOptional('disk', $rules['disk']),
38+
2539
'add_allocations' => 'bail|array',
2640
'add_allocations.*' => 'integer',
2741
'remove_allocations' => 'bail|array',
2842
'remove_allocations.*' => 'integer',
43+
2944
'feature_limits' => 'required|array',
3045
'feature_limits.databases' => $rules['database_limit'],
3146
'feature_limits.allocations' => $rules['allocation_limit'],
@@ -46,6 +61,15 @@ public function validated()
4661
$data['allocation_limit'] = $data['feature_limits']['allocations'];
4762
unset($data['allocation'], $data['feature_limits']);
4863

64+
// Adjust the limits field to match what is expected by the model.
65+
if (! empty($data['limits'])) {
66+
foreach ($data['limits'] as $key => $value) {
67+
$data[$key] = $value;
68+
}
69+
70+
unset($data['limits']);
71+
}
72+
4973
return $data;
5074
}
5175

@@ -65,4 +89,30 @@ public function attributes()
6589
'feature_limits.allocations' => 'Allocation Limit',
6690
];
6791
}
92+
93+
/**
94+
* Converts existing rules for certain limits into a format that maintains backwards
95+
* compatability with the old API endpoint while also supporting a more correct API
96+
* call.
97+
*
98+
* @param string $field
99+
* @param array $rules
100+
* @param bool $limits
101+
* @return array
102+
*
103+
* @see https://github.com/pterodactyl/panel/issues/1500
104+
*/
105+
protected function requiredToOptional(string $field, array $rules, bool $limits = false)
106+
{
107+
if (! in_array('required', $rules)) {
108+
return $rules;
109+
}
110+
111+
return (new Collection($rules))
112+
->filter(function ($value) {
113+
return $value !== 'required';
114+
})
115+
->prepend($limits ? 'required_with:limits' : 'required_without:limits')
116+
->toArray();
117+
}
68118
}

0 commit comments

Comments
 (0)