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.
95 lines
3.0 KiB
PHP
95 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAdminFabricService;
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
/** Administrator-only Fabric topology and reconciliation API. */
|
|
final class CloudAdminFabricController
|
|
{
|
|
private function actor($request, bool $write = false): array
|
|
{
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
throw new CloudError('需要管理员权限', 403);
|
|
}
|
|
if ($write) {
|
|
CloudAccess::manage($actor);
|
|
if (!($actor['is_super_admin'] ?? false)) {
|
|
throw new CloudError('仅超级管理员可以修改 Fabric', 403);
|
|
}
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
private function json($response, array $payload, int $status = 200)
|
|
{
|
|
return $response->withJson($payload, $status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
private function assertEmptyBody($request, string $operation): void
|
|
{
|
|
$body = trim((string) $request->getBody());
|
|
if ($body !== '' && $body !== '{}') {
|
|
throw new CloudError($operation . ' 不接受请求体', 422);
|
|
}
|
|
}
|
|
|
|
private function operationStatus(array $payload): int
|
|
{
|
|
$status = $payload['operation']['status'] ?? null;
|
|
return in_array($status, ['queued', 'running'], true) ? 202 : 200;
|
|
}
|
|
|
|
public function index($request, $response)
|
|
{
|
|
return $this->json($response,
|
|
CloudAdminFabricService::listing($this->actor($request)));
|
|
}
|
|
|
|
public function detail($request, $response, $args)
|
|
{
|
|
return $this->json($response,
|
|
CloudAdminFabricService::detail(
|
|
(string) ($args['fabric_id'] ?? ''), $this->actor($request)
|
|
));
|
|
}
|
|
|
|
public function probeRegion($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request, true);
|
|
$this->assertEmptyBody($request, '宿主机检测');
|
|
return $this->json($response,
|
|
CloudAdminFabricService::probeRegion(
|
|
(string) ($args['region_id'] ?? ''), $actor
|
|
));
|
|
}
|
|
|
|
public function applyRegion($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request, true);
|
|
$this->assertEmptyBody($request, '地区私网自动配置');
|
|
$payload = CloudAdminFabricService::applyRegion(
|
|
(string) ($args['region_id'] ?? ''), $actor
|
|
);
|
|
return $this->json(
|
|
$response, $payload, $this->operationStatus($payload)
|
|
);
|
|
}
|
|
|
|
public function reconcileRegion($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request, true);
|
|
$this->assertEmptyBody($request, '地区私网重新核对');
|
|
$payload = CloudAdminFabricService::reconcileRegion(
|
|
(string) ($args['region_id'] ?? ''), $actor
|
|
);
|
|
return $this->json(
|
|
$response, $payload, $this->operationStatus($payload)
|
|
);
|
|
}
|
|
}
|