You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/CloudAdminHostNetworkAttach...

92 lines
3.1 KiB
PHP

<?php
namespace App\Controllers;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudHostNetworkAttachmentService;
use App\Services\Cloud\CloudInput;
/** Thin HTTP boundary for independently verified host network attachments. */
final class CloudAdminHostNetworkAttachmentController
{
private function actor($request, bool $write): array
{
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor();
if (!($actor['is_admin'] ?? false)) {
throw new CloudError('需要管理员权限', 403);
}
if ($write) {
CloudAccess::manage($actor);
}
return $actor;
}
public function index($request, $response, $args)
{
$write = strtoupper((string) $request->getMethod()) !== 'GET';
$actor = $this->actor($request, $write);
$service = new CloudHostNetworkAttachmentService();
$hostId = (int) ($args['id'] ?? 0);
if (!$write) {
$result = $service->read($hostId, $actor);
$status = 200;
} else {
$body = CloudInput::object($request);
CloudInput::only($body, [
'name', 'line_id', 'network_selector', 'state', 'priority',
'request_key',
]);
$result = $service->create($hostId, $actor, $body);
$status = 201;
}
return $response->withJson($result, $status)
->withHeader('Cache-Control', 'private, no-store');
}
public function update($request, $response, $args)
{
$actor = $this->actor($request, true);
$body = CloudInput::object($request);
CloudInput::only($body, [
'name', 'line_id', 'network_selector', 'state', 'priority',
'expected_revision',
]);
$result = (new CloudHostNetworkAttachmentService())->update(
(int) ($args['id'] ?? 0),
(string) ($args['attachment_key'] ?? ''),
$actor,
$body
);
return $response->withJson($result)
->withHeader('Cache-Control', 'private, no-store');
}
public function discover($request, $response, $args)
{
$actor = $this->actor($request, true);
CloudInput::only(CloudInput::object($request), []);
$result = (new CloudHostNetworkAttachmentService())->discover(
(int) ($args['id'] ?? 0),
$actor
);
return $response->withJson($result)
->withHeader('Cache-Control', 'private, no-store');
}
public function test($request, $response, $args)
{
$actor = $this->actor($request, true);
$body = CloudInput::object($request);
CloudInput::only($body, ['expected_revision']);
$result = (new CloudHostNetworkAttachmentService())->test(
(int) ($args['id'] ?? 0),
(string) ($args['attachment_key'] ?? ''),
$actor,
$body
);
return $response->withJson($result)
->withHeader('Cache-Control', 'private, no-store');
}
}