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.
virt/web/app/Controllers/CloudVpcController.php

54 lines
1.8 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\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');
}
}