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.
68 lines
2.2 KiB
PHTML
68 lines
2.2 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Controllers;
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudAccess;
|
||
|
|
use App\Services\Cloud\CloudAdminHostNetworkService;
|
||
|
|
use App\Services\Cloud\CloudError;
|
||
|
|
use App\Services\Cloud\CloudInput;
|
||
|
|
|
||
|
|
/** Thin HTTP boundary for the independently revisioned host public pools. */
|
||
|
|
final class CloudAdminHostNetworkController
|
||
|
|
{
|
||
|
|
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)
|
||
|
|
{
|
||
|
|
$method = strtoupper((string) $request->getMethod());
|
||
|
|
$write = $method !== 'GET';
|
||
|
|
$actor = $this->actor($request, $write);
|
||
|
|
$hostId = (int) ($args['id'] ?? 0);
|
||
|
|
$service = new CloudAdminHostNetworkService();
|
||
|
|
|
||
|
|
if (!$write) {
|
||
|
|
$result = $service->read($hostId, $actor);
|
||
|
|
$status = 200;
|
||
|
|
} else {
|
||
|
|
$body = CloudInput::object($request);
|
||
|
|
CloudInput::only($body, [
|
||
|
|
'name', 'cidr', 'gateway', 'dns_servers', 'reserved_ips',
|
||
|
|
'guest_prefix_length', 'state', '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', 'cidr', 'gateway', 'dns_servers', 'reserved_ips',
|
||
|
|
'guest_prefix_length', 'state', 'expected_revision',
|
||
|
|
]);
|
||
|
|
$result = (new CloudAdminHostNetworkService())->update(
|
||
|
|
(int) ($args['id'] ?? 0),
|
||
|
|
(string) ($args['pool_key'] ?? ''),
|
||
|
|
$actor,
|
||
|
|
$body
|
||
|
|
);
|
||
|
|
return $response->withJson($result)
|
||
|
|
->withHeader('Cache-Control', 'private, no-store');
|
||
|
|
}
|
||
|
|
}
|