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/CloudPublicNetworkBundleCon...

198 lines
7.1 KiB
PHP

<?php
namespace App\Services\Cloud;
/**
* Immutable identity of one planned IPv4 + IPv6 allocation bundle.
*
* Availability is intentionally absent: it is mutable admission state. The
* allocator rechecks capacity while holding the exact catalog and address
* ledgers, but it must never substitute another attachment or pool.
*/
final class CloudPublicNetworkBundleConstraint
{
private function __construct(private readonly array $evidence)
{
}
public static function fromArray(mixed $value): self
{
if (!is_array($value) || array_is_list($value)) {
throw new \InvalidArgumentException(
'Public network bundle constraint must be an object.'
);
}
self::exactKeys($value, [
'host_id', 'line_id', 'network_attachment_id', 'attachment_key',
'network_selector', 'attachment_config_revision',
'attachment_verified_agent_revision', 'ipv4_pool', 'ipv6_pool',
]);
foreach (['host_id', 'line_id', 'network_attachment_id',
'attachment_config_revision',
'attachment_verified_agent_revision'] as $field) {
self::positiveInt($value[$field] ?? null);
}
self::hexKey($value['attachment_key'] ?? null);
if (!CloudNetworkSelector::isValid($value['network_selector'] ?? null)) {
throw new \InvalidArgumentException(
'Public network bundle selector is invalid.'
);
}
$ipv4 = self::pool(
$value['ipv4_pool'] ?? null,
4,
$value['network_attachment_id']
);
$ipv6 = self::pool(
$value['ipv6_pool'] ?? null,
6,
$value['network_attachment_id']
);
if ($ipv4['public_ip_pool_id'] === $ipv6['public_ip_pool_id']
|| hash_equals($ipv4['pool_key'], $ipv6['pool_key'])) {
throw new \InvalidArgumentException(
'Public network bundle pools must be distinct.'
);
}
$value['ipv4_pool'] = $ipv4;
$value['ipv6_pool'] = $ipv6;
return new self($value);
}
public function assertScope(int $hostId, int $lineId): void
{
if ($hostId < 1 || $lineId < 1
|| $hostId !== $this->evidence['host_id']
|| $lineId !== $this->evidence['line_id']) {
throw new CloudError('计划公网网络范围已经变化', 409);
}
}
public function attachmentId(): int
{
return $this->evidence['network_attachment_id'];
}
public function poolId(int $family): int
{
return $this->poolEvidence($family)['public_ip_pool_id'];
}
/** Validate locked rows against every immutable scheduler field. */
public function assertMatches(object $attachment, array $pools): void
{
if ((int) ($attachment->id ?? 0)
!== $this->evidence['network_attachment_id']
|| (int) ($attachment->host_id ?? 0)
!== $this->evidence['host_id']
|| (int) ($attachment->line_id ?? 0)
!== $this->evidence['line_id']
|| !hash_equals(
$this->evidence['attachment_key'],
(string) ($attachment->attachment_key ?? '')
)
|| !hash_equals(
$this->evidence['network_selector'],
(string) ($attachment->network_selector ?? '')
)
|| (int) ($attachment->config_revision ?? 0)
!== $this->evidence['attachment_config_revision']
|| (int) ($attachment->verified_agent_revision ?? 0)
!== $this->evidence['attachment_verified_agent_revision']) {
throw new CloudError('计划公网网络接入证据已经变化', 409);
}
foreach ([4, 6] as $family) {
$pool = $pools[$family] ?? null;
$expected = $this->poolEvidence($family);
if (!is_object($pool)
|| (int) ($pool->id ?? 0)
!== $expected['public_ip_pool_id']
|| (int) ($pool->host_id ?? 0)
!== $this->evidence['host_id']
|| (int) ($pool->network_attachment_id ?? 0)
!== $this->evidence['network_attachment_id']
|| (int) ($pool->address_family ?? 0) !== $family
|| !hash_equals(
$expected['pool_key'],
(string) ($pool->pool_key ?? '')
)
|| (int) ($pool->desired_revision ?? 0)
!== $expected['desired_revision']
|| (int) ($pool->observed_revision ?? 0)
!== $expected['observed_revision']) {
throw new CloudError('计划公网地址池证据已经变化', 409);
}
}
}
private function poolEvidence(int $family): array
{
return match ($family) {
4 => $this->evidence['ipv4_pool'],
6 => $this->evidence['ipv6_pool'],
default => throw new \InvalidArgumentException(
'Public network address family is invalid.'
),
};
}
private static function pool(mixed $value, int $family,
int $attachmentId): array
{
if (!is_array($value) || array_is_list($value)) {
throw new \InvalidArgumentException(
'Public network bundle pool must be an object.'
);
}
self::exactKeys($value, [
'public_ip_pool_id', 'pool_key', 'network_attachment_id',
'address_family', 'desired_revision', 'observed_revision',
]);
foreach (['public_ip_pool_id', 'network_attachment_id',
'address_family', 'desired_revision', 'observed_revision']
as $field) {
self::positiveInt($value[$field] ?? null);
}
self::hexKey($value['pool_key'] ?? null);
if ($value['network_attachment_id'] !== $attachmentId
|| $value['address_family'] !== $family
|| $value['observed_revision'] !== $value['desired_revision']) {
throw new \InvalidArgumentException(
'Public network bundle pool evidence is inconsistent.'
);
}
return $value;
}
private static function exactKeys(array $value, array $expected): void
{
$actual = array_keys($value);
sort($actual, SORT_STRING);
sort($expected, SORT_STRING);
if ($actual !== $expected) {
throw new \InvalidArgumentException(
'Public network bundle fields are invalid.'
);
}
}
private static function positiveInt(mixed $value): void
{
if (!is_int($value) || $value < 1) {
throw new \InvalidArgumentException(
'Public network bundle integer is invalid.'
);
}
}
private static function hexKey(mixed $value): void
{
if (!is_string($value)
|| preg_match('/^[a-f0-9]{32}$/D', $value) !== 1) {
throw new \InvalidArgumentException(
'Public network bundle key is invalid.'
);
}
}
}