mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 11:55:51 -05:00
## Problem
Extensions can render content before and after most admin pages, but not
inside the forms on them. The only exception was
`panel.users.detail.form`, and it could only re-render or validate the
user's existing fields. An extension had no way to add a field of its
own, because the Panel's save requests had nowhere to carry the value
and nothing loaded it back.
## Changes
### Slots inside every admin form
The create and edit forms for users, nodes, servers, eggs, locations,
mounts and database hosts each render a slot inside the form, such as
`panel.servers.create.form` and `panel.servers.detail.details.form`. The
slot receives the native `form`, the form's `kind` (`admin.server`), and
`mode` (`'create'` or `'edit'`). Edit slots also receive the saved
`resource`. Contributed `form.AppField` controls share the form's
validation, pending state and submit action.
### Extension-owned fields
An extension registers fields in its provider:
```php
$this->registerFormFields('admin.user', ['plan' => ['required', 'in:free,pro']]);
$this->registerFormFields(
'admin.node',
['region' => ['required', 'string', 'max:32']],
fn (Node $node): array => ['region' => NodeRegion::for($node)],
fn (Node $node, array $values) => NodeRegion::store($node, $values['region']),
);
```
The 14 create and update endpoints accept an optional `extensions: {
[extensionId]: { [field]: value } }` body field. Each extension's rules
are applied as `extensions.{id}.{field}`, and only when the request
includes that extension's values. The extension's `save` callback runs
in the same database transaction as the core change, so a failure rolls
both back.
Server creation is the exception. Wings reads the new server while
provisioning it, so extension values are saved after provisioning
succeeds, outside the transaction.
For Node updates when Wings is unreachable. `UpdateNode` saves the node
and then throws `ConfigurationNotPersistedException` as a warning. The
node controller defers that exception until the extension values are
saved, so the node update is no longer rolled back.
`GET /api/admin/extensions/forms/{form}/{id}` returns every extension's
current values. It requires the resource's read permission. Edit pages
fetch it before rendering the form, but only when an extension has
registered fields for that form; the bootstrap payload now lists them
per extension in `forms`.
For users and servers the callbacks are optional, and values are stored
in the extension's scoped settings (`settings()->forUser($user)`,
`forServer($server)`). Values are limited to strings, numbers, booleans,
`null`, or lists of strings and numbers, which keeps the OpenAPI schema
and generated types concrete.
A `load` callback that throws, or returns an invalid value, is recorded
against the extension in the `form` phase. The frontend then hides that
extension's slot contributions on the edit page, so a save cannot
overwrite its stored values with defaults. Fields of a disabled or
failed extension are neither validated, saved nor loaded.
---------
Co-authored-by: robert dennis <31261583+robertdrakedennis@users.noreply.github.com>
161 lines
7.9 KiB
PHP
161 lines
7.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Knuckles\Scribe\Attributes\Endpoint;
|
|
use Knuckles\Scribe\Attributes\Group;
|
|
use Knuckles\Scribe\Attributes\QueryParam;
|
|
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
|
|
use Knuckles\Scribe\Attributes\Subgroup;
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
use Pterodactyl\Contracts\Nodes\CreatesNodes;
|
|
use Pterodactyl\Contracts\Nodes\DeletesNodes;
|
|
use Pterodactyl\Contracts\Nodes\UpdatesNodes;
|
|
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ExtensionFieldsParam;
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
|
|
use Pterodactyl\Facades\Fractal;
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest;
|
|
use Pterodactyl\Models\Node;
|
|
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
use Throwable;
|
|
|
|
#[Group('Application API', 'Root administrator endpoints for managing panel resources using application API tokens.')]
|
|
#[Subgroup('Nodes', 'Create, update, retrieve, and delete Wings nodes and their allocations.')]
|
|
class NodeController extends ApplicationApiController
|
|
{
|
|
private const array ACTIVE_SERVERS_ERROR = [
|
|
'errors' => [
|
|
[
|
|
'code' => 'HasActiveServersException',
|
|
'status' => '400',
|
|
'detail' => 'A node must have no servers linked to it in order to be deleted.',
|
|
],
|
|
],
|
|
];
|
|
|
|
private const array CONFIGURATION_NOT_PERSISTED_ERROR = [
|
|
'errors' => [
|
|
[
|
|
'code' => 'ConfigurationNotPersistedException',
|
|
'status' => '400',
|
|
'detail' => 'The daemon configuration has been updated, however there was an error encountered while attempting to automatically update the configuration file on the Daemon. You will need to manually update the configuration file (config.yml) for the daemon to apply these changes.',
|
|
],
|
|
],
|
|
];
|
|
|
|
/**
|
|
* Return all the nodes currently available on the Panel.
|
|
*
|
|
* @return ApiPayload
|
|
*/
|
|
#[Endpoint('List nodes', 'Returns a paginated list of Wings nodes registered on the panel.')]
|
|
#[QueryParam('per_page', 'integer', 'Number of nodes to return per page, up to 100.', required: false, example: 50)]
|
|
#[QueryParam('filter[uuid]', 'string', 'Filter nodes by UUID.', required: false, example: '3d8dd7f9-07a1-4d65-8db0-8c8921f2f4f7')]
|
|
#[QueryParam('filter[name]', 'string', 'Filter nodes by name.', required: false, example: 'Node 1')]
|
|
#[QueryParam('filter[fqdn]', 'string', 'Filter nodes by fully qualified domain name.', required: false, example: 'node.example.com')]
|
|
#[QueryParam('filter[daemon_token_id]', 'string', 'Filter nodes by daemon token identifier.', required: false, example: 'abcdefghijklmnop')]
|
|
#[QueryParam('sort', 'string', 'Sort nodes by id, uuid, memory, or disk. Prefix with "-" for descending order.', required: false, example: '-id', enum: ['id', '-id', 'uuid', '-uuid', 'memory', '-memory', 'disk', '-disk'])]
|
|
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "allocations", "location", and "servers" when the key can read those resources.', required: false, example: 'location,allocations', enum: ['allocations', 'location', 'servers', 'location,allocations'])]
|
|
#[ResponseFromTransformer(NodeTransformer::class, Node::class, collection: true, factoryStates: ['withLocation'], resourceKey: 'node', paginate: [IlluminatePaginatorAdapter::class, 50])]
|
|
public function index(GetNodesRequest $request): array
|
|
{
|
|
$nodes = QueryBuilder::for(Node::query()
|
|
->withSum('servers as sum_memory', 'memory')
|
|
->withSum('servers as sum_disk', 'disk'))
|
|
->allowedFilters(['uuid', 'name', 'fqdn', 'daemon_token_id'])
|
|
->allowedSorts(['id', 'uuid', 'memory', 'disk'])
|
|
->paginate($request->perPage());
|
|
|
|
return Fractal::collection($nodes)
|
|
->transformWith($this->getTransformer(NodeTransformer::class))
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Return data for a single instance of a node.
|
|
*
|
|
* @return ApiPayload
|
|
*/
|
|
#[Endpoint('Get node', 'Returns a single Wings node by internal numeric ID.')]
|
|
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "allocations", "location", and "servers" when the key can read those resources.', required: false, example: 'location,allocations', enum: ['allocations', 'location', 'servers', 'location,allocations'])]
|
|
#[ResponseFromTransformer(NodeTransformer::class, Node::class, factoryStates: ['withLocation'], resourceKey: 'node')]
|
|
public function view(GetNodeRequest $request, Node $node): array
|
|
{
|
|
return Fractal::item($node)
|
|
->transformWith($this->getTransformer(NodeTransformer::class)->withExtensionFields())
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Create a new node on the Panel. Returns the created node and an HTTP/201
|
|
* status response on success.
|
|
*/
|
|
#[Endpoint('Create node', 'Creates a new Wings node and returns the created resource.')]
|
|
#[ResponseFromTransformer(NodeTransformer::class, Node::class, status: 201, description: 'Node created.', factoryStates: ['withLocation'], resourceKey: 'node', meta: ['resource' => 'https://panel.example.test/api/application/nodes/1'])]
|
|
#[ExtensionFieldsParam]
|
|
public function store(StoreNodeRequest $request, CreatesNodes $nodes): JsonResponse
|
|
{
|
|
$node = $nodes->create($request->payload());
|
|
|
|
return Fractal::item($node)
|
|
->transformWith($this->getTransformer(NodeTransformer::class)->withExtensionFields())
|
|
->addMeta([
|
|
'resource' => route('api.application.nodes.view', [
|
|
'node' => $node->id,
|
|
]),
|
|
])
|
|
->respond(201);
|
|
}
|
|
|
|
/**
|
|
* Update an existing node on the Panel.
|
|
*
|
|
*
|
|
* @return ApiPayload
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
#[Endpoint('Update node', 'Updates an existing Wings node by internal numeric ID.')]
|
|
#[ResponseFromTransformer(NodeTransformer::class, Node::class, factoryStates: ['withLocation'], resourceKey: 'node')]
|
|
#[ScribeResponse(self::CONFIGURATION_NOT_PERSISTED_ERROR, status: 400, description: 'The panel saved the node changes but could not persist the configuration to Wings.')]
|
|
#[ExtensionFieldsParam]
|
|
public function update(UpdateNodeRequest $request, UpdatesNodes $nodes, Node $node): array
|
|
{
|
|
$node = $nodes->update(
|
|
$node,
|
|
$request->payload(),
|
|
$request->boolean('reset_secret')
|
|
);
|
|
|
|
return Fractal::item($node)
|
|
->transformWith($this->getTransformer(NodeTransformer::class)->withExtensionFields())
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Deletes a given node from the Panel as long as there are no servers
|
|
* currently attached to it.
|
|
*
|
|
* @throws HasActiveServersException
|
|
*/
|
|
#[Endpoint('Delete node', 'Deletes a Wings node by internal numeric ID when no servers are attached.')]
|
|
#[ScribeResponse(status: 204, description: 'Node deleted.')]
|
|
#[ScribeResponse(self::ACTIVE_SERVERS_ERROR, status: 400, description: 'The node has active servers attached.')]
|
|
public function delete(DeleteNodeRequest $request, DeletesNodes $nodes, Node $node): JsonResponse
|
|
{
|
|
$nodes->delete($node);
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
}
|
|
}
|