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.
77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAdminTrafficPoolService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
/** Administrator API for shared traffic-pool rollout and line identity. */
|
|
final class CloudAdminTrafficPoolController
|
|
{
|
|
private function actor($request, bool $write): array
|
|
{
|
|
$actor = $request->getAttribute('cloud_actor')
|
|
?? CloudAccess::actor(false);
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
throw new CloudError('需要管理员权限', 403);
|
|
}
|
|
if ($write) {
|
|
CloudAccess::manage($actor);
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
private function json($response, array $payload, int $status = 200)
|
|
{
|
|
return $response->withJson($payload, $status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function groups($request, $response, $args = [])
|
|
{
|
|
$method = strtoupper((string) $request->getMethod());
|
|
$write = $method !== 'GET';
|
|
$this->actor($request, $write);
|
|
$groupId = isset($args['id']) ? (int) $args['id'] : null;
|
|
if (!$write) {
|
|
return $this->json($response, $groupId === null
|
|
? CloudAdminTrafficPoolService::page(
|
|
$request->getQueryParams()
|
|
)
|
|
: CloudAdminTrafficPoolService::read($groupId));
|
|
}
|
|
$body = CloudInput::object($request);
|
|
if ($groupId === null) {
|
|
CloudInput::only($body, ['code', 'name']);
|
|
return $this->json($response,
|
|
CloudAdminTrafficPoolService::create($body), 201);
|
|
}
|
|
CloudInput::only($body, ['name', 'mode', 'expected_revision']);
|
|
return $this->json($response,
|
|
CloudAdminTrafficPoolService::update($groupId, $body));
|
|
}
|
|
|
|
public function lines($request, $response, $args)
|
|
{
|
|
$this->actor($request, true);
|
|
$body = CloudInput::object($request);
|
|
CloudInput::only($body, ['line_ids', 'expected_revision']);
|
|
return $this->json($response,
|
|
CloudAdminTrafficPoolService::replaceLines(
|
|
(int) ($args['id'] ?? 0),
|
|
$body
|
|
));
|
|
}
|
|
|
|
public function readiness($request, $response, $args)
|
|
{
|
|
$this->actor($request, false);
|
|
return $this->json($response,
|
|
CloudAdminTrafficPoolService::readiness(
|
|
(int) ($args['id'] ?? 0)
|
|
));
|
|
}
|
|
}
|