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'); } }