|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
|
|
|
use App\Services\Cloud\CloudAdminVpcControlService;
|
|
|
|
|
use App\Services\Cloud\CloudAdminVpcCidrPoolService;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
|
|
|
|
|
|
/** Administrator API for the region-owned VPC address catalog. */
|
|
|
|
|
final class CloudAdminVpcController
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pools($request, $response, $args = [])
|
|
|
|
|
{
|
|
|
|
|
$method = strtoupper((string) $request->getMethod());
|
|
|
|
|
$write = $method !== 'GET';
|
|
|
|
|
$this->actor($request, $write);
|
|
|
|
|
$id = isset($args['id']) ? (int) $args['id'] : null;
|
|
|
|
|
if (!$write) {
|
|
|
|
|
$result = $id === null
|
|
|
|
|
? CloudAdminVpcCidrPoolService::listing()
|
|
|
|
|
: CloudAdminVpcCidrPoolService::read($id);
|
|
|
|
|
$status = 200;
|
|
|
|
|
} else {
|
|
|
|
|
$body = CloudInput::object($request);
|
|
|
|
|
CloudInput::only($body, [
|
|
|
|
|
'region_id', 'name', 'cidr', 'allocation_prefix', 'state',
|
|
|
|
|
'request_key', 'expected_revision',
|
|
|
|
|
]);
|
|
|
|
|
$result = $id === null
|
|
|
|
|
? CloudAdminVpcCidrPoolService::create($body)
|
|
|
|
|
: CloudAdminVpcCidrPoolService::update($id, $body);
|
|
|
|
|
$status = $id === null ? 201 : 200;
|
|
|
|
|
}
|
|
|
|
|
return $response->withJson($result, $status)
|
|
|
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function control($request, $response)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request, false);
|
|
|
|
|
return $response->withJson(CloudAdminVpcControlService::page(
|
|
|
|
|
$actor, $request->getQueryParams()
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function networks($request, $response)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request, false);
|
|
|
|
|
return $response->withJson(CloudAdminVpcControlService::listing(
|
|
|
|
|
$actor, $request->getQueryParams()
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function detail($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request, false);
|
|
|
|
|
return $response->withJson(CloudAdminVpcControlService::detail(
|
|
|
|
|
$actor, (int) ($args['id'] ?? 0)
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
}
|