From 341ccfb86ca69351213d85f2755f79cabd281017 Mon Sep 17 00:00:00 2001 From: ste87 Date: Sat, 14 Mar 2026 14:12:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Speed/StoreServerRequest.p?= =?UTF-8?q?hp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Speed/StoreServerRequest.php | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Speed/StoreServerRequest.php diff --git a/Speed/StoreServerRequest.php b/Speed/StoreServerRequest.php new file mode 100644 index 0000000..487a742 --- /dev/null +++ b/Speed/StoreServerRequest.php @@ -0,0 +1,87 @@ + $rules['name'], + 'user_id' => $rules['user_id'], + 'node_id' => $rules['node_id'], + // TODO: validation should be added for manually setting the vmid + 'vmid' => 'present|nullable|numeric|min:100|max:999999999', + 'hostname' => $rules['hostname'], + 'limits' => 'required|array', + 'limits.cpu' => $rules['cpu'], + 'limits.memory' => $rules['memory'], + 'limits.disk' => $rules['disk'], + 'limits.snapshots' => $rules['snapshot_limit'], + 'limits.backups' => $rules['backup_limit'], + 'limits.bandwidth' => $rules['bandwidth_limit'], + 'limits.address_ids' => 'sometimes|nullable|array', + 'limits.address_ids.*' => 'integer|exists:ip_addresses,id', + 'account_password' => ['required_if:should_create_server,1', 'string', 'min:8', 'max:191', new Password( + ), new USKeyboardCharacters()], + 'should_create_server' => 'present|boolean', + 'template_uuid' => 'required_if:create_server,1|string|exists:templates,uuid', + 'start_on_completion' => 'present|boolean', + ]; + } + + public function withValidator(Validator $validator): void + { + $validator->after(function ($validator) { + $addressIds = $this->input('limits.address_ids'); + + if (!is_null($addressIds)) { + $addresses = Address::whereIn('id', $addressIds)->get(); + + foreach ($addresses as $address) { + if ($address->server_id !== null) { + $validator->errors()->add( + 'limits.address_ids', + 'One or more of the selected addresses are already in use', + ); + break; + } + } + } + + // check if the memory and disk isn't exceeding the node limits + $node = Node::findOrFail($this->input('node_id'))->load('servers'); + + $nodeMemoryLimit = ($node->memory * (($node->memory_overallocate / 100) + 1)) - $node->memory_allocated; + $nodeDiskLimit = ($node->disk * (($node->disk_overallocate / 100) + 1)) - $node->disk_allocated; + + $memory = intval($this->input('limits.memory')); + $disk = intval($this->input('limits.disk')); + + if ($memory > $nodeMemoryLimit || $memory < 0) { + $validator->errors()->add( + 'limits.memory', 'The memory value exceeds the node\'s limit.', + ); + } + + if ($disk > $nodeDiskLimit || $disk < 0) { + $validator->errors()->add( + 'limits.disk', 'The disk value exceeds the node\'s limit.', + ); + } + }); + } +}