diff --git a/web/app/Controllers/CloudAdminController.php b/web/app/Controllers/CloudAdminController.php index 4291054e..c61384ce 100644 --- a/web/app/Controllers/CloudAdminController.php +++ b/web/app/Controllers/CloudAdminController.php @@ -3,6 +3,7 @@ namespace App\Controllers; use App\Models\CloudHost; use App\Services\Cloud\CloudAccess; +use App\Services\Cloud\CloudAdminHostProjectionService; use App\Services\Cloud\CloudAdminHostSchedulingService; use App\Services\Cloud\CloudCountryCatalog; use App\Services\Cloud\CloudError; @@ -42,10 +43,14 @@ final class CloudAdminController $write = $request->getMethod() !== 'GET'; $this->actor($request, $write); if (!$write && !isset($args['id'])) { - return $response->withJson(['hosts' => CloudHost::orderBy('id')->get()->toArray()]); + return $response->withJson([ + 'hosts' => CloudAdminHostProjectionService::all(), + ])->withHeader('Cache-Control', 'no-store'); } if (!$write) { - return $response->withJson($this->host($args['id'])->toArray()) + return $response->withJson(CloudAdminHostProjectionService::one( + $this->host($args['id']) + )) ->withHeader('Cache-Control', 'no-store'); } $body = CloudInput::object($request); diff --git a/web/app/Services/Cloud/CloudAdminHostProjectionService.php b/web/app/Services/Cloud/CloudAdminHostProjectionService.php new file mode 100644 index 00000000..6c5b7053 --- /dev/null +++ b/web/app/Services/Cloud/CloudAdminHostProjectionService.php @@ -0,0 +1,52 @@ +get(); + if ($hosts->isEmpty()) { + return []; + } + + $memberships = CloudHostResourcePoolMembership::whereIn( + 'host_id', $hosts->pluck('id')->all() + )->orderBy('host_id')->orderBy('resource_pool_id')->orderBy('id')->get(); + $byHost = []; + foreach ($memberships as $membership) { + $byHost[(int) $membership->host_id][] = $membership; + } + + return $hosts->map(static fn (CloudHost $host): array => self::dto( + $host, $byHost[(int) $host->id] ?? [] + ))->values()->all(); + } + + public static function one(CloudHost $host): array + { + $memberships = CloudHostResourcePoolMembership::where( + 'host_id', (int) $host->id + )->orderBy('resource_pool_id')->orderBy('id')->get(); + return self::dto($host, $memberships); + } + + private static function dto(CloudHost $host, iterable $memberships): array + { + $selected = CloudHostPoolMembershipService::selected($memberships); + $state = (string) ($selected?->scheduler_state ?? 'disabled'); + $dto = $host->toArray(); + $dto['resource_pool_id'] = $selected === null + ? null : (int) $selected->resource_pool_id; + $dto['scheduler_state'] = in_array($state, self::SCHEDULER_STATES, true) + ? $state : 'disabled'; + return $dto; + } +} diff --git a/web/tests/AdminHostSchedulingContractTest.php b/web/tests/AdminHostSchedulingContractTest.php index a8abf72f..16d903d6 100644 --- a/web/tests/AdminHostSchedulingContractTest.php +++ b/web/tests/AdminHostSchedulingContractTest.php @@ -15,6 +15,9 @@ $assert = static function (bool $condition, string $message): void { }; $service = $read('app/Services/Cloud/CloudAdminHostSchedulingService.php'); +$projectionService = $read( + 'app/Services/Cloud/CloudAdminHostProjectionService.php' +); $membershipService = $read( 'app/Services/Cloud/CloudHostPoolMembershipService.php' ); @@ -39,6 +42,13 @@ $assert(str_contains($controller, "'resource_pool_id', 'scheduler_state'") strpos($controller, 'public function userIsoSettings') - strpos($controller, 'public function hostScheduling')), 'public_ip'), 'The scheduling allowlist must not accept manual public IP fields.'); +$assert(str_contains($controller, 'CloudAdminHostProjectionService::all()') + && str_contains($controller, 'CloudAdminHostProjectionService::one(') + && str_contains($projectionService, + 'CloudHostPoolMembershipService::selected($memberships)') + && !str_contains($projectionService, '$host->resource_pool_id') + && !str_contains($projectionService, '$host->scheduler_state'), + 'Host API projection must use the selected membership authority.'); $saveStart = strpos($service, 'public static function save'); $saveEnd = strpos($service, 'private static function validateActive', $saveStart); diff --git a/web/tests/CloudAdminHostListProjectionTest.php b/web/tests/CloudAdminHostListProjectionTest.php new file mode 100644 index 00000000..c374e171 --- /dev/null +++ b/web/tests/CloudAdminHostListProjectionTest.php @@ -0,0 +1,114 @@ +addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); +$schema = $capsule->schema('default'); +$schema->create('cloud_hosts', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('name'); + $table->string('region'); + $table->string('country_code', 2); + $table->string('default_network'); + $table->boolean('enabled'); + $table->integer('created_at'); + $table->integer('updated_at'); +}); +$schema->create('cloud_host_resource_pool_memberships', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('host_id'); + $table->integer('resource_pool_id'); + $table->string('scheduler_state'); + $table->integer('priority'); + $table->integer('weight'); + $table->integer('created_at'); + $table->integer('updated_at'); + $table->unique(['host_id', 'resource_pool_id']); + }); + +$assert(!$schema->hasColumn('cloud_hosts', 'resource_pool_id') + && !$schema->hasColumn('cloud_hosts', 'scheduler_state'), + 'Host projection regression schema accidentally restored retired columns.'); + +Capsule::table('cloud_hosts')->insert([ + ['id' => 2, 'name' => 'Host B', 'region' => 'Tokyo', 'country_code' => 'JP', + 'default_network' => 'public', 'enabled' => 0, 'created_at' => 20, + 'updated_at' => 20], + ['id' => 1, 'name' => 'Host A', 'region' => 'HongKong', 'country_code' => 'HK', + 'default_network' => 'public', 'enabled' => 1, 'created_at' => 10, + 'updated_at' => 10], +]); +Capsule::table('cloud_host_resource_pool_memberships')->insert([ + ['id' => 1, 'host_id' => 1, 'resource_pool_id' => 10, + 'scheduler_state' => 'disabled', 'priority' => 0, 'weight' => 100, + 'created_at' => 1, 'updated_at' => 300], + ['id' => 2, 'host_id' => 1, 'resource_pool_id' => 11, + 'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100, + 'created_at' => 1, 'updated_at' => 100], + ['id' => 3, 'host_id' => 1, 'resource_pool_id' => 12, + 'scheduler_state' => 'draining', 'priority' => 0, 'weight' => 100, + 'created_at' => 1, 'updated_at' => 400], +]); + +$request = Request::createFromEnvironment(Environment::mock([ + 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts', +]))->withAttribute('cloud_actor', [ + 'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false, + 'can_manage' => false, +]); +Capsule::connection('default')->enableQueryLog(); +$response = (new CloudAdminController())->hosts($request, new Response()); +$queries = count(Capsule::connection('default')->getQueryLog()); +$payload = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); + +$assert($response->getStatusCode() === 200 + && $response->getHeaderLine('Cache-Control') === 'no-store' + && isset($payload['hosts']) + && count($payload['hosts']) === 2, + 'Administrator host list did not return the expected JSON collection.'); +[$first, $second] = $payload['hosts']; +$assert($first['id'] === 1 && $first['name'] === 'Host A' + && $first['enabled'] === true && $first['resource_pool_id'] === 11 + && $first['scheduler_state'] === 'active', + 'Host list did not project the selected active membership authority.'); +$assert($second['id'] === 2 && $second['name'] === 'Host B' + && $second['enabled'] === false && $second['resource_pool_id'] === null + && $second['scheduler_state'] === 'disabled', + 'Host without a membership did not receive an explicit disabled projection.'); +$assert($queries === 2, + 'Host list introduced an N+1 query pattern: ' . $queries . ' queries.'); + +$oneRequest = Request::createFromEnvironment(Environment::mock([ + 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts/1', +]))->withAttribute('cloud_actor', [ + 'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false, + 'can_manage' => false, +]); +$oneResponse = (new CloudAdminController())->hosts( + $oneRequest, new Response(), ['id' => '1'] +); +$one = json_decode((string) $oneResponse->getBody(), true, 512, JSON_THROW_ON_ERROR); +$assert($oneResponse->getStatusCode() === 200 + && $oneResponse->getHeaderLine('Cache-Control') === 'no-store' + && $one['id'] === 1 && $one['resource_pool_id'] === 11 + && $one['scheduler_state'] === 'active', + 'Single-host read drifted from the authoritative membership projection.'); + +echo "Administrator host list membership projection passed.\n";