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

188 lines
6.5 KiB
PHP

<?php
namespace App\Services\Cloud;
/** Immutable caller intent for one durable public-network transition. */
final class CloudPublicNetworkTransitionCommand
{
private function __construct(
private readonly string $kind,
private readonly int $instanceId,
private readonly int $actorUserId,
private readonly string $requestKey,
private readonly string $transitionKey,
private readonly string $fingerprint,
private readonly array $targets,
private readonly ?int $priceCents,
private readonly ?int $batchItemId,
private readonly ?int $parentJobId
) {
}
public static function userPaidAutomatic(int $instanceId, int $userId,
string $requestKey, string $expectedAmount): self
{
return self::make(
'user_paid',
$instanceId,
$userId,
$requestKey,
[4 => null],
CloudMoney::cents($expectedAmount),
null,
null
);
}
/**
* @param array<int,array{pool_key:string,address_cidr:string}> $targets
*/
public static function adminExact(int $instanceId, int $actorUserId,
string $requestKey, array $targets): self
{
return self::make(
'admin_manual',
$instanceId,
$actorUserId,
$requestKey,
self::normalizeTargets($targets),
null,
null,
null
);
}
/**
* The batch coordinator owns aggregation; this command freezes only one
* exact per-instance target and its immutable batch-item association.
*
* @param array<int,array{pool_key:string,address_cidr:string}> $targets
*/
public static function poolReplacement(int $instanceId, int $actorUserId,
string $requestKey, int $parentJobId, int $batchItemId,
array $targets): self
{
if ($parentJobId < 1 || $batchItemId < 1) {
throw new \InvalidArgumentException(
'Public-network batch identity is invalid.'
);
}
return self::make(
'pool_replacement',
$instanceId,
$actorUserId,
$requestKey,
self::normalizeTargets($targets),
null,
$batchItemId,
$parentJobId
);
}
private static function make(string $kind, int $instanceId,
int $actorUserId, string $requestKey, array $targets,
?int $priceCents, ?int $batchItemId, ?int $parentJobId): self
{
if (!in_array($kind,
['user_paid', 'admin_manual', 'pool_replacement'], true)
|| $instanceId < 1 || $actorUserId < 1
|| preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $requestKey) !== 1
|| $targets === []
|| ($kind === 'user_paid' && ($priceCents === null
|| $priceCents < 1 || $batchItemId !== null
|| $parentJobId !== null))
|| ($kind === 'admin_manual'
&& ($priceCents !== null || $batchItemId !== null
|| $parentJobId !== null))
|| ($kind === 'pool_replacement'
&& ($priceCents !== null || $batchItemId === null
|| $parentJobId === null || $parentJobId < 1))) {
throw new \InvalidArgumentException(
'Public-network transition command is invalid.'
);
}
$identity = [
'kind' => $kind,
'instance_id' => $instanceId,
'actor_user_id' => $actorUserId,
'targets' => $targets,
'price_cents' => $priceCents,
'batch_item_id' => $batchItemId,
'parent_job_id' => $parentJobId,
];
$fingerprint = hash('sha256', json_encode(
$identity,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
));
$transitionKey = substr(hash(
'sha256',
implode("\0", [
'public-network-transition-v1',
(string) $actorUserId,
$requestKey,
])
), 0, 32);
return new self(
$kind,
$instanceId,
$actorUserId,
$requestKey,
$transitionKey,
$fingerprint,
$targets,
$priceCents,
$batchItemId,
$parentJobId
);
}
/** @return array<int,array{pool_key:string,address_cidr:string}> */
private static function normalizeTargets(array $targets): array
{
$result = [];
foreach ($targets as $familyKey => $target) {
$family = filter_var($familyKey, FILTER_VALIDATE_INT);
if (!in_array($family, [4, 6], true)
|| isset($result[$family]) || !is_array($target)
|| count($target) !== 2
|| !array_key_exists('pool_key', $target)
|| !array_key_exists('address_cidr', $target)
|| !is_string($target['pool_key'])
|| preg_match('/^[a-f0-9]{32}$/D',
$target['pool_key']) !== 1) {
throw new \InvalidArgumentException(
'Public-network target is invalid.'
);
}
$prefix = CloudIpPrefix::address($target['address_cidr'] ?? null);
if ($prefix->family() !== $family) {
throw new \InvalidArgumentException(
'Public-network target family is invalid.'
);
}
$result[$family] = [
'pool_key' => $target['pool_key'],
'address_cidr' => $prefix->canonical(),
];
}
ksort($result, SORT_NUMERIC);
if ($result === []) {
throw new \InvalidArgumentException(
'Public-network target is required.'
);
}
return $result;
}
public function kind(): string { return $this->kind; }
public function instanceId(): int { return $this->instanceId; }
public function actorUserId(): int { return $this->actorUserId; }
public function requestKey(): string { return $this->requestKey; }
public function transitionKey(): string { return $this->transitionKey; }
public function fingerprint(): string { return $this->fingerprint; }
public function targets(): array { return $this->targets; }
public function priceCents(): ?int { return $this->priceCents; }
public function batchItemId(): ?int { return $this->batchItemId; }
public function parentJobId(): ?int { return $this->parentJobId; }
}