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.
141 lines
4.9 KiB
PHP
141 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAdminResourceService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
/** Database-owned administrator resource catalog and reservation snapshots. */
|
|
final class CloudAdminResourceController
|
|
{
|
|
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);
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
private function json($response, array $payload, int $status = 200)
|
|
{
|
|
return $response->withJson($payload, $status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function resources($request, $response)
|
|
{
|
|
$this->actor($request);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::page($request->getQueryParams()));
|
|
}
|
|
|
|
public function settings($request, $response)
|
|
{
|
|
$write = $request->getMethod() !== 'GET';
|
|
$this->actor($request, $write);
|
|
if (!$write) {
|
|
return $this->json($response, CloudAdminResourceService::settings());
|
|
}
|
|
|
|
$body = CloudInput::object($request);
|
|
CloudInput::only($body, ['network_lines_enabled']);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::saveSettings($body));
|
|
}
|
|
|
|
public function regions($request, $response, $args = [])
|
|
{
|
|
$method = $request->getMethod();
|
|
$write = $method !== 'GET';
|
|
$this->actor($request, $write);
|
|
$id = isset($args['id']) ? (string) $args['id'] : null;
|
|
if (!$write) {
|
|
return $this->json($response, $id === null
|
|
? CloudAdminResourceService::regions($request->getQueryParams())
|
|
: CloudAdminResourceService::region($id));
|
|
}
|
|
|
|
$body = CloudInput::object($request);
|
|
CloudInput::only($body, ['code', 'name', 'country_code', 'status', 'sort']);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::saveRegion($id, $body),
|
|
$method === 'POST' ? 201 : 200);
|
|
}
|
|
|
|
public function regionFabric($request, $response, $args = [])
|
|
{
|
|
$method = $request->getMethod();
|
|
$actor = $this->actor($request, $method !== 'GET');
|
|
$id = (string) ($args['id'] ?? '');
|
|
if ($method === 'GET') {
|
|
return $this->json($response,
|
|
CloudAdminResourceService::regionFabric($id));
|
|
}
|
|
$body = CloudInput::object($request);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::saveRegionFabric(
|
|
$id, $body, $actor, $method === 'PATCH'
|
|
));
|
|
}
|
|
|
|
public function networkLines($request, $response, $args = [])
|
|
{
|
|
$method = $request->getMethod();
|
|
$write = $method !== 'GET';
|
|
$this->actor($request, $write);
|
|
$id = isset($args['id']) ? (string) $args['id'] : null;
|
|
if (!$write) {
|
|
return $this->json($response, $id === null
|
|
? CloudAdminResourceService::networkLines($request->getQueryParams())
|
|
: CloudAdminResourceService::networkLine($id));
|
|
}
|
|
|
|
$body = CloudInput::object($request);
|
|
CloudInput::only($body,
|
|
['region_id', 'code', 'name', 'is_default', 'status', 'sort']);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::saveNetworkLine($id, $body),
|
|
$method === 'POST' ? 201 : 200);
|
|
}
|
|
|
|
public function resourcePools($request, $response, $args = [])
|
|
{
|
|
$method = $request->getMethod();
|
|
$write = $method !== 'GET';
|
|
$this->actor($request, $write);
|
|
$id = isset($args['id']) ? (string) $args['id'] : null;
|
|
if (!$write) {
|
|
return $this->json($response, $id === null
|
|
? CloudAdminResourceService::resourcePools($request->getQueryParams())
|
|
: CloudAdminResourceService::resourcePool($id));
|
|
}
|
|
|
|
$body = CloudInput::object($request);
|
|
CloudInput::only($body,
|
|
['line_id', 'code', 'name', 'status', 'scheduling_policy', 'sort']);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::saveResourcePool($id, $body),
|
|
$method === 'POST' ? 201 : 200);
|
|
}
|
|
|
|
public function reservations($request, $response)
|
|
{
|
|
$this->actor($request);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::reservations($request->getQueryParams()));
|
|
}
|
|
|
|
public function reservation($request, $response, $args)
|
|
{
|
|
$this->actor($request);
|
|
return $this->json($response,
|
|
CloudAdminResourceService::reservation((string) ($args['id'] ?? '')));
|
|
}
|
|
}
|