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/Services/Cloud/CloudVpcPageService.php

94 lines
3.8 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudRegionFabric;
/**
* Database-only bootstrap data for the tenant VPC page.
*
* The page must never enumerate a host or ask the engine for placement data.
* Regions are limited to platform mappings that can actually accept a VPC;
* instance choices contain only owner-scoped display fields needed by the
* attach form. The API remains authoritative for every write and validation.
*/
final class CloudVpcPageService
{
public static function bootstrap(array $actor): array
{
return [
'regions' => self::regions(),
'instances' => self::instances($actor),
];
}
private static function regions(): array
{
try {
$connection = (new CloudRegionFabric())->getConnection();
$schema = $connection->getSchemaBuilder();
if (!$schema->hasTable('cloud_region_fabrics')) {
return [];
}
return CloudRegionFabric::query()
->join('cloud_regions as region', 'region.id', '=', 'cloud_region_fabrics.region_id')
->where('cloud_region_fabrics.state', 'active')
->where('cloud_region_fabrics.fabric_revision', '>', 0)
->where('region.status', 'active')
->orderByDesc('region.sort')->orderBy('region.id')
->get([
'region.id as id', 'region.name', 'region.country_code',
])
->map(static fn ($row): array => [
'id' => (int) $row->id,
'name' => trim((string) $row->name),
'country_code' => strtoupper(trim((string) ($row->country_code ?? ''))),
])->all();
} catch (\Throwable) {
// A rolling schema install should leave the page usable while
// making the unavailable Fabric choice explicit to the tenant.
return [];
}
}
private static function instances(array $actor): array
{
$userId = (int) ($actor['user_id'] ?? 0);
if ($userId < 1) {
return [];
}
try {
return CloudInstance::query()
->where('user_id', $userId)
->where('status', 'active')
->orderBy('name')->orderBy('id')->limit(200)->get([
'id', 'name', 'spec_json', 'service_status', 'expires_at',
])->map(static function ($row): array {
$spec = json_decode((string) $row->spec_json, true);
$spec = is_array($spec) && !array_is_list($spec) ? $spec : [];
$regionId = filter_var($spec['region_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
$region = trim((string) ($spec['region'] ?? $spec['location'] ?? ''));
// A future expiry does not make a blocked/suspended
// service eligible for a private-network attachment.
// Keep this projection identical to CloudInstance's
// serviceAvailable() lifecycle gate.
$available = (string) $row->service_status === 'exempt'
|| ((string) $row->service_status === 'active'
&& $row->expires_at !== null
&& (int) $row->expires_at > time());
return [
'id' => (int) $row->id,
'name' => (string) $row->name,
'region_id' => $regionId === false ? null : (int) $regionId,
'region' => $region,
'service_available' => $available,
];
})->all();
} catch (\Throwable) {
return [];
}
}
}