, * attachments:array * }> */ public static function forHosts(array $hostIds): array { $hostIds = array_values(array_unique(array_filter(array_map( static fn ($id): int => (int) $id, $hostIds ), static fn (int $id): bool => $id > 0))); sort($hostIds, SORT_NUMERIC); $result = []; foreach ($hostIds as $hostId) { $result[$hostId] = [ 'aggregate' => self::empty(), 'lines' => [], 'attachments' => [], ]; } if ($hostIds === []) { return $result; } $agents = []; foreach (CloudHostAgent::whereIn('host_id', $hostIds)->get() as $agent) { $agents[(int) $agent->host_id] = $agent; } $pools = CloudPublicIpPool::query() ->from('cloud_public_ip_pools as public_pool') ->join( 'cloud_host_network_attachments as network_attachment', 'network_attachment.id', '=', 'public_pool.network_attachment_id' ) ->whereIn('public_pool.host_id', $hostIds) ->whereColumn('network_attachment.host_id', 'public_pool.host_id') ->get([ 'public_pool.id', 'public_pool.host_id', 'public_pool.pool_key', 'public_pool.address_family', 'public_pool.cidr', 'public_pool.guest_prefix_length', 'public_pool.max_allocations', 'public_pool.state as pool_state', 'public_pool.sync_state as pool_sync_state', 'public_pool.observed_revision as pool_observed_revision', 'public_pool.desired_revision as pool_desired_revision', 'network_attachment.id as attachment_id', 'network_attachment.line_id', 'network_attachment.network_selector', 'network_attachment.state as attachment_state', 'network_attachment.verification_state', 'network_attachment.verified_agent_revision', 'network_attachment.last_verified_at', ]); $poolById = []; foreach ($pools as $pool) { $poolById[(int) $pool->id] = $pool; } $addresses = $poolById === [] ? collect() : CloudPublicIp::whereIn( 'public_ip_pool_id', array_keys($poolById) )->get(); $occupiedByPool = []; $availableIpv4ByPool = []; $attachmentState = []; foreach ($pools as $pool) { $hostId = (int) $pool->host_id; $lineId = (int) $pool->line_id; $attachmentId = (int) $pool->attachment_id; if ($lineId < 1 || $attachmentId < 1) { continue; } $attachmentState[$hostId][$lineId][$attachmentId] ??= self::emptyAttachmentState(); } foreach ($addresses as $address) { $pool = $poolById[(int) $address->public_ip_pool_id] ?? null; if ($pool === null) { continue; } $hostId = (int) $pool->host_id; $lineId = (int) $pool->line_id; $attachmentId = (int) $pool->attachment_id; $family = (int) $pool->address_family; if ($lineId < 1 || $attachmentId < 1 || !in_array($family, [4, 6], true)) { continue; } $attachmentState[$hostId][$lineId][$attachmentId] ??= self::emptyAttachmentState(); $occupied = in_array( (string) $address->state, self::OCCUPIED_STATES, true ) || $address->reservation_id !== null || $address->instance_id !== null || $address->operation_id !== null; if ($occupied) { $occupiedByPool[(int) $pool->id] = ($occupiedByPool[(int) $pool->id] ?? 0) + 1; } elseif ((string) $address->state === 'available' && $family === 4 && self::validAvailableIpv4($address, $pool)) { $availableIpv4ByPool[(int) $pool->id] = ($availableIpv4ByPool[(int) $pool->id] ?? 0) + 1; } $state = (string) $address->state; if ($state === 'assigned') { $identity = (int) ($address->instance_id ?? 0) > 0 ? 'instance:' . (int) $address->instance_id : 'address:' . (int) $address->id; $attachmentState[$hostId][$lineId][$attachmentId] ['claims']['allocated'][$identity][$family] = true; } elseif (in_array( $state, ['reserved', 'changing', 'uncertain'], true )) { $identity = self::reservationIdentity($address); $attachmentState[$hostId][$lineId][$attachmentId] ['claims']['reserved'][$identity][$family] = true; } elseif ($state === 'disabled') { $attachmentState[$hostId][$lineId][$attachmentId] ['disabled'][$family]++; } } foreach ($pools as $pool) { $hostId = (int) $pool->host_id; $lineId = (int) $pool->line_id; $attachmentId = (int) $pool->attachment_id; $family = (int) $pool->address_family; if ($lineId < 1 || $attachmentId < 1 || !in_array($family, [4, 6], true) || !self::poolReady($pool, $agents[$hostId] ?? null)) { continue; } $available = $family === 6 ? max( 0, (int) $pool->max_allocations - (int) ($occupiedByPool[(int) $pool->id] ?? 0) ) : (int) ($availableIpv4ByPool[(int) $pool->id] ?? 0); $attachmentState[$hostId][$lineId][$attachmentId] ['available'][$family] += $available; } foreach ($attachmentState as $hostId => $lines) { foreach ($lines as $lineId => $attachments) { $lineCapacities = []; foreach ($attachments as $attachmentId => $state) { $capacity = self::fromAttachmentState($state); $result[$hostId]['attachments'][$attachmentId] = $capacity; $lineCapacities[] = $capacity; } $result[$hostId]['lines'][$lineId] = self::sum($lineCapacities); } $result[$hostId]['aggregate'] = self::sum( $result[$hostId]['lines'] ); } return $result; } public static function empty(): array { return [ 'bundles' => [ 'unit' => 'dual_stack_bundle', 'total' => 0, 'allocated' => 0, 'reserved' => 0, 'available' => 0, ], 'incomplete_claims' => [ 'unit' => 'allocation_identity', 'allocated' => 0, 'reserved' => 0, ], 'disabled_addresses' => [ 'unit' => 'address', 'ipv4' => 0, 'ipv6' => 0, 'total' => 0, ], ]; } public static function sum(iterable $capacities): array { $result = self::empty(); foreach ($capacities as $capacity) { if (!is_array($capacity)) { continue; } foreach (['total', 'allocated', 'reserved', 'available'] as $field) { $result['bundles'][$field] += max( 0, (int) ($capacity['bundles'][$field] ?? 0) ); } foreach (['allocated', 'reserved'] as $field) { $result['incomplete_claims'][$field] += max( 0, (int) ($capacity['incomplete_claims'][$field] ?? 0) ); } foreach (['ipv4', 'ipv6'] as $field) { $result['disabled_addresses'][$field] += max( 0, (int) ($capacity['disabled_addresses'][$field] ?? 0) ); } } $result['disabled_addresses']['total'] = $result['disabled_addresses']['ipv4'] + $result['disabled_addresses']['ipv6']; return $result; } private static function emptyAttachmentState(): array { return [ 'available' => [4 => 0, 6 => 0], 'claims' => ['allocated' => [], 'reserved' => []], 'disabled' => [4 => 0, 6 => 0], ]; } private static function fromAttachmentState(array $state): array { $result = self::empty(); $result['bundles']['available'] = min( max(0, (int) ($state['available'][4] ?? 0)), max(0, (int) ($state['available'][6] ?? 0)) ); foreach (['allocated', 'reserved'] as $claimState) { foreach ($state['claims'][$claimState] ?? [] as $families) { if (isset($families[4], $families[6])) { $result['bundles'][$claimState]++; } else { $result['incomplete_claims'][$claimState]++; } } } $result['bundles']['total'] = $result['bundles']['allocated'] + $result['bundles']['reserved'] + $result['bundles']['available']; $result['disabled_addresses']['ipv4'] = max( 0, (int) ($state['disabled'][4] ?? 0) ); $result['disabled_addresses']['ipv6'] = max( 0, (int) ($state['disabled'][6] ?? 0) ); $result['disabled_addresses']['total'] = $result['disabled_addresses']['ipv4'] + $result['disabled_addresses']['ipv6']; return $result; } private static function reservationIdentity($address): string { if ((int) ($address->reservation_id ?? 0) > 0) { return 'reservation:' . (int) $address->reservation_id; } $operationId = trim((string) ($address->operation_id ?? '')); return $operationId !== '' ? 'operation:' . $operationId : 'address:' . (int) $address->id; } private static function poolReady($pool, ?CloudHostAgent $agent): bool { return preg_match('/^[a-f0-9]{32}$/D', (string) $pool->pool_key) === 1 && (string) $pool->attachment_state === 'active' && (string) $pool->verification_state === 'verified' && (int) $pool->last_verified_at > 0 && (string) $pool->pool_state === 'active' && (string) $pool->pool_sync_state === 'applied' && (int) $pool->pool_observed_revision >= (int) $pool->pool_desired_revision && $agent !== null && CloudAgentSettingsService::publicNetworkReady( $agent, (string) $pool->network_selector, (int) $pool->verified_agent_revision ); } private static function validAvailableIpv4($address, $pool): bool { if ((int) $address->prefix_length !== (int) $pool->guest_prefix_length) { return false; } try { $network = CloudIpPrefix::network((string) $pool->cidr); $ip = CloudIpAddress::parse((string) $address->address); } catch (\InvalidArgumentException) { return false; } if ($network->family() !== 4 || $network->prefixLength() < 24 || $network->prefixLength() > 30 || !$ip->isPublicUnicast() || !$network->contains($ip) || $ip->equals($network->networkAddress()) || $ip->equals($network->lastAddress())) { return false; } return true; } }