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/CloudVpcRegionReadiness.php

100 lines
3.9 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudHostVpcTransport;
use App\Models\CloudRegion;
use App\Models\CloudVpc;
use App\Models\CloudVpcCidrPool;
/** One projection for regions that can accept a newly allocated VPC. */
final class CloudVpcRegionReadiness
{
/** @return list<int> */
public static function eligibleRegionIds(): array
{
return CloudHostVpcTransport::query()
->join('cloud_hosts as vpc_host', 'vpc_host.id', '=',
'cloud_host_vpc_transports.host_id')
->join('cloud_host_resource_pool_memberships as membership',
'membership.host_id', '=', 'vpc_host.id')
->join('cloud_resource_pools as resource_pool',
'resource_pool.id', '=', 'membership.resource_pool_id')
->join('cloud_network_lines as network_line',
'network_line.id', '=', 'resource_pool.line_id')
->join('cloud_regions as vpc_region',
'vpc_region.id', '=', 'network_line.region_id')
->where('vpc_host.enabled', 1)
->where('membership.scheduler_state', 'active')
->where('resource_pool.status', 'active')
->where('network_line.status', 'active')
->where('vpc_region.status', 'active')
->where('cloud_host_vpc_transports.sync_state', 'ready')
->whereColumn('cloud_host_vpc_transports.observed_revision',
'cloud_host_vpc_transports.desired_revision')
->distinct()->orderBy('vpc_region.id')
->pluck('vpc_region.id')->map(
static fn ($id): int => (int) $id
)->all();
}
/**
* Return only host-ready regions with at least one allocatable subnet.
*
* @return list<array{id:int,code:string,name:string,country_code:string,
* available:int,prefix_lengths:list<int>}>
*/
public static function catalog(): array
{
$regionIds = self::eligibleRegionIds();
if ($regionIds === []) {
return [];
}
$regions = CloudRegion::query()->whereIn('id', $regionIds)
->orderBy('sort')->orderBy('id')->get([
'id', 'code', 'name', 'country_code', 'status',
])->keyBy('id');
if ($regions->isEmpty()) {
return [];
}
$usage = CloudVpc::query()->where('active_claim', 1)
->selectRaw('cidr_pool_id, COUNT(*) AS row_count')
->groupBy('cidr_pool_id')->pluck('row_count', 'cidr_pool_id');
$pools = CloudVpcCidrPool::query()
->whereIn('region_id', $regions->keys()->all())
->where('state', 'active')->orderBy('id')->get();
$summary = [];
foreach ($pools as $pool) {
$regionId = (int) $pool->region_id;
$capacity = 1 << ((int) $pool->allocation_prefix
- (int) $pool->prefix_length);
$used = (int) ($usage[(int) $pool->id] ?? 0);
$summary[$regionId]['available'] =
($summary[$regionId]['available'] ?? 0)
+ max(0, $capacity - $used);
$summary[$regionId]['prefixes'][
(int) $pool->allocation_prefix
] = true;
}
$result = [];
foreach ($regions as $region) {
$regionId = (int) $region->id;
$available = (int) ($summary[$regionId]['available'] ?? 0);
if ($available < 1) {
continue;
}
$prefixes = array_keys($summary[$regionId]['prefixes'] ?? []);
sort($prefixes, SORT_NUMERIC);
$result[] = [
'id' => $regionId,
'code' => (string) $region->code,
'name' => (string) $region->name,
'country_code' => (string) $region->country_code,
'available' => $available,
'prefix_lengths' => $prefixes,
];
}
return $result;
}
}