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.
161 lines
6.3 KiB
PHP
161 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestore;
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
/** Immutable coordinator result; it is not an Agent dispatch instruction. */
|
|
final class CloudPortableRestoreAttemptReservation
|
|
{
|
|
private function __construct(private readonly array $value)
|
|
{
|
|
}
|
|
|
|
public static function active(CloudBackupRestore $restore,
|
|
CloudBackupRestoreAttempt $attempt,
|
|
CloudResourceReservation $reservation,
|
|
CloudActivePublicNetworkClaimSet $claimSet,
|
|
CloudFrozenPublicNetworkIdentity $network,
|
|
CloudPortableRestoreTargetPlan $plan): self
|
|
{
|
|
self::assertRows($restore, $attempt, $reservation, $plan);
|
|
self::assertActiveLifecycle($attempt, $reservation);
|
|
$resources = $plan->toArray()['resources'];
|
|
$dispatchable = (string) $attempt->state === 'reserved'
|
|
&& (string) $reservation->state === 'held'
|
|
&& $attempt->job_id === null
|
|
&& (string) $attempt->effect_resolution === 'not_applicable';
|
|
return new self([
|
|
'restore_id' => (int) $restore->id,
|
|
'restore_key' => (string) $restore->restore_key,
|
|
'attempt_id' => (int) $attempt->id,
|
|
'attempt_key' => (string) $attempt->attempt_key,
|
|
'generation' => (int) $attempt->generation,
|
|
'state' => (string) $attempt->state,
|
|
'effect_resolution' => (string) $attempt->effect_resolution,
|
|
'operation_id' => (string) $attempt->operation_id,
|
|
'reservation_id' => (int) $reservation->id,
|
|
'claim_ids' => [
|
|
'ipv4' => $claimSet->claimId(4),
|
|
'ipv6' => $claimSet->claimId(6),
|
|
],
|
|
'claim_set_sha256' => $claimSet->digest(),
|
|
'target_plan_sha256' => $plan->sha256(),
|
|
'vpc_inherited' => false,
|
|
'dispatchable' => $dispatchable,
|
|
'dispatch_body' => $dispatchable ? [
|
|
'uuid' => strtolower((string) $attempt->target_uuid),
|
|
'name' => (string) $attempt->target_name,
|
|
'vcpu' => $resources['cpu_cores'],
|
|
'memory_mib' => $resources['memory_mib'],
|
|
'disk_gib' => $resources['disk_gib'],
|
|
'bandwidth_mbps' => $resources['bandwidth_mbps'],
|
|
'public_network' => $network->publicNetwork()->toArray(),
|
|
] : null,
|
|
]);
|
|
}
|
|
|
|
public static function terminal(CloudBackupRestore $restore,
|
|
CloudBackupRestoreAttempt $attempt,
|
|
CloudResourceReservation $reservation,
|
|
CloudPortableRestoreTargetPlan $plan,
|
|
array $claimIds): self
|
|
{
|
|
self::assertRows($restore, $attempt, $reservation, $plan);
|
|
foreach (['ipv4', 'ipv6'] as $family) {
|
|
if (!isset($claimIds[$family]) || !is_int($claimIds[$family])
|
|
|| $claimIds[$family] < 1) {
|
|
throw new CloudError('恢复尝试历史双栈声明不完整', 409);
|
|
}
|
|
}
|
|
return new self([
|
|
'restore_id' => (int) $restore->id,
|
|
'restore_key' => (string) $restore->restore_key,
|
|
'attempt_id' => (int) $attempt->id,
|
|
'attempt_key' => (string) $attempt->attempt_key,
|
|
'generation' => (int) $attempt->generation,
|
|
'state' => (string) $attempt->state,
|
|
'effect_resolution' => (string) $attempt->effect_resolution,
|
|
'operation_id' => (string) $attempt->operation_id,
|
|
'reservation_id' => (int) $reservation->id,
|
|
'claim_ids' => $claimIds,
|
|
'claim_set_sha256' =>
|
|
(string) $attempt->target_claim_set_sha256,
|
|
'target_plan_sha256' => $plan->sha256(),
|
|
'vpc_inherited' => false,
|
|
'dispatchable' => false,
|
|
'dispatch_body' => null,
|
|
]);
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return $this->value;
|
|
}
|
|
|
|
public function dispatchBody(): ?array
|
|
{
|
|
return $this->value['dispatch_body'];
|
|
}
|
|
|
|
private static function assertRows(CloudBackupRestore $restore,
|
|
CloudBackupRestoreAttempt $attempt,
|
|
CloudResourceReservation $reservation,
|
|
CloudPortableRestoreTargetPlan $plan): void
|
|
{
|
|
if ((int) $restore->id < 1 || (int) $attempt->id < 1
|
|
|| (int) $reservation->id < 1
|
|
|| (int) $attempt->backup_restore_id !== (int) $restore->id
|
|
|| (int) $attempt->reservation_id !== (int) $reservation->id
|
|
|| !$attempt->matchesReservation($reservation)
|
|
|| (int) $attempt->target_host_id
|
|
!== (int) $restore->target_host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $attempt->target_uuid),
|
|
strtolower((string) $restore->new_uuid)
|
|
)
|
|
|| !hash_equals(
|
|
(string) $attempt->target_name,
|
|
(string) $restore->new_name
|
|
)
|
|
|| !hash_equals(
|
|
(string) $attempt->target_plan_sha256,
|
|
$plan->sha256()
|
|
)
|
|
|| !hash_equals(
|
|
(string) $restore->selected_plan_sha256,
|
|
$plan->sha256()
|
|
)) {
|
|
throw new CloudError('恢复尝试资源身份不一致', 409);
|
|
}
|
|
}
|
|
|
|
private static function assertActiveLifecycle(
|
|
CloudBackupRestoreAttempt $attempt,
|
|
CloudResourceReservation $reservation
|
|
): void {
|
|
$state = (string) $attempt->state;
|
|
$resolution = (string) $attempt->effect_resolution;
|
|
$expectedReservations = match ($state) {
|
|
'reserved', 'queued', 'awaiting_receipt' =>
|
|
$resolution === 'not_applicable' ? ['held'] : [],
|
|
'effect_unknown' =>
|
|
$resolution === 'unknown' ? ['uncertain'] : [],
|
|
'finalization_pending' => match ($resolution) {
|
|
'applied' => ['held', 'uncertain'],
|
|
'not_applied' => ['held', 'uncertain'],
|
|
default => [],
|
|
},
|
|
'succeeded' => $resolution === 'applied' ? ['committed'] : [],
|
|
default => [],
|
|
};
|
|
if ($expectedReservations === []
|
|
|| !in_array((string) $reservation->state,
|
|
$expectedReservations, true)) {
|
|
throw new CloudError('恢复尝试与资源预留生命周期不一致', 409);
|
|
}
|
|
}
|
|
}
|