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.
119 lines
4.5 KiB
PHP
119 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInput;
|
|
use App\Services\Cloud\CloudVpcFeature;
|
|
use App\Services\Cloud\CloudVpcMembershipBatchService;
|
|
use App\Services\Cloud\CloudVpcPortService;
|
|
use App\Services\Cloud\CloudVpcService;
|
|
|
|
/** Authenticated user boundary for region-scoped VPC lifecycle. */
|
|
final class CloudVpcController
|
|
{
|
|
private function actor($request, bool $idempotent = false): array
|
|
{
|
|
if (!CloudVpcFeature::enabled()) {
|
|
throw new CloudError('VPC 私网功能尚未开放', 404);
|
|
}
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
if ($idempotent) {
|
|
$key = trim((string) $request->getHeaderLine('Idempotency-Key'));
|
|
if (preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $key) !== 1) {
|
|
throw new CloudError('请求幂等标识无效', 400);
|
|
}
|
|
$actor['request_idempotency_key'] = $key;
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
public function index($request, $response)
|
|
{
|
|
$method = strtoupper((string) $request->getMethod());
|
|
$actor = $this->actor($request, $method === 'POST');
|
|
if ($method === 'GET') {
|
|
$result = CloudVpcService::library($actor);
|
|
$status = 200;
|
|
} else {
|
|
$body = CloudInput::object($request);
|
|
$result = CloudVpcService::create($actor, $body);
|
|
$status = 201;
|
|
}
|
|
return $response->withJson($result, $status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function delete($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request);
|
|
return $response->withJson(CloudVpcService::delete(
|
|
$actor, (string) ($args['vpc_key'] ?? '')
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
/** Submit one durable server-side attach/detach batch. */
|
|
public function membershipBatch($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request, true);
|
|
$result = CloudVpcMembershipBatchService::start(
|
|
$actor,
|
|
(string) ($args['vpc_key'] ?? ''),
|
|
CloudInput::object($request)
|
|
);
|
|
return $response->withJson([
|
|
'task_id' => (string) $result['job']->id,
|
|
'status_url' => '/api/v1/tasks/' . (int) $result['job']->id,
|
|
], 202)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
/** Read, attach, or detach the one optional VPC port on an instance. */
|
|
public function port($request, $response, $args)
|
|
{
|
|
$method = strtoupper((string) $request->getMethod());
|
|
$write = in_array($method, ['PUT', 'DELETE'], true);
|
|
$actor = $this->actor($request, $write);
|
|
$instance = CloudAccess::instance($args['id'] ?? null, $actor, $write);
|
|
if ($method === 'GET') {
|
|
$result = CloudVpcPortService::listing($instance, $actor);
|
|
$status = 200;
|
|
} elseif ($method === 'PUT') {
|
|
$result = CloudVpcPortService::attach(
|
|
$instance, $actor, CloudInput::object($request)
|
|
);
|
|
$status = $result['job'] === null ? 200 : 202;
|
|
} else {
|
|
$result = CloudVpcPortService::detach(
|
|
$instance, $actor, CloudInput::object($request)
|
|
);
|
|
$status = $result['job'] === null ? 200 : 202;
|
|
}
|
|
if (($result['job'] ?? null) !== null) {
|
|
$job = $result['job'];
|
|
return $response->withJson([
|
|
'task_id' => (string) $job->id,
|
|
'status_url' => '/api/v1/tasks/' . (int) $job->id,
|
|
], 202)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
unset($result['job']);
|
|
return $response->withJson($result, $status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
/** Continue a fail-closed attach rollback without replaying uncertainty. */
|
|
public function cancelMembershipBatch($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request);
|
|
$result = CloudVpcMembershipBatchService::cancelAttach(
|
|
$actor,
|
|
(string) ($args['vpc_key'] ?? ''),
|
|
(string) ($args['batch_key'] ?? '')
|
|
);
|
|
return $response->withJson([
|
|
'task_id' => (string) $result['job']->id,
|
|
'status_url' => '/api/v1/tasks/' . (int) $result['job']->id,
|
|
], 202)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|