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/CloudAdminPublicIpPoolRepla...

73 lines
2.4 KiB
PHTML

<?php
namespace App\Controllers;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudPublicIpPoolReplacementBatchService;
/** HTTP boundary for durable same-host public-pool replacement batches. */
final class CloudAdminPublicIpPoolReplacementController
{
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()) === 'POST';
$actor = $this->actor($request, $write);
$hostId = (int) ($args['id'] ?? 0);
$sourcePoolKey = (string) ($args['pool_key'] ?? '');
if (!$write) {
$result = CloudPublicIpPoolReplacementBatchService::overview(
$hostId,
$sourcePoolKey,
$actor
);
return $response->withJson($result)
->withHeader('Cache-Control', 'private, no-store');
}
$body = CloudInput::object($request);
CloudInput::only($body, [
'target_pool_key', 'expected_source_revision',
'expected_target_revision', 'request_key',
'confirm_replacement',
]);
$result = CloudPublicIpPoolReplacementBatchService::start(
$hostId,
$sourcePoolKey,
$actor,
$body
);
$job = $result['job'];
return $response->withJson([
'task_id' => (string) $job->id,
'status_url' => '/api/v1/tasks/' . (int) $job->id,
], 202)->withHeader('Cache-Control', 'no-store');
}
public function detail($request, $response, $args)
{
$actor = $this->actor($request, false);
$result = CloudPublicIpPoolReplacementBatchService::detail(
(int) ($args['id'] ?? 0),
(string) ($args['pool_key'] ?? ''),
(string) ($args['batch_key'] ?? ''),
$actor
);
return $response->withJson($result)
->withHeader('Cache-Control', 'private, no-store');
}
}