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.
621 lines
25 KiB
PHP
621 lines
25 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostAgent;
|
|
use App\Models\CloudHostNetworkAttachment;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInvoice;
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudPublicIp;
|
|
use App\Models\CloudPublicIpPool;
|
|
use App\Models\CloudPublicNetworkTransition;
|
|
use App\Models\CloudResourceReservation;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/**
|
|
* Owns the locked read model and invariant checks for a network transition.
|
|
*
|
|
* Callers must already own the instance lock. This module preserves the
|
|
* canonical host/agent -> transition -> topology -> reservation -> legs ->
|
|
* claims/IPs acquisition order while hiding the frozen dual-stack ledger.
|
|
*/
|
|
final class CloudPublicNetworkTransitionContext
|
|
{
|
|
private const FAMILIES = [4, 6];
|
|
|
|
/** @return array{0:CloudHost,1:CloudHostAgent} */
|
|
public static function lockHostBoundary(int $hostId): array
|
|
{
|
|
if ($hostId < 1) {
|
|
throw new CloudError('公网网络变更宿主机身份缺失', 409);
|
|
}
|
|
$host = CloudHost::where('id', $hostId)
|
|
->lockForUpdate()->first();
|
|
$agent = CloudHostAgent::where('host_id', $hostId)
|
|
->lockForUpdate()->first();
|
|
if (!$host instanceof CloudHost
|
|
|| !$agent instanceof CloudHostAgent) {
|
|
throw new CloudError('公网网络变更宿主机账本缺失', 409);
|
|
}
|
|
return [$host, $agent];
|
|
}
|
|
|
|
/**
|
|
* @return array{0:CloudPublicNetworkSnapshot,1:CloudPublicNetworkSnapshot}
|
|
*/
|
|
public static function assertAssociation(
|
|
CloudJob $job,
|
|
CloudInstance $instance,
|
|
CloudPublicNetworkTransition $transition,
|
|
?array $payload,
|
|
bool $requirePayload
|
|
): array {
|
|
$jobInvoiceId = $job->invoice_id === null
|
|
? null : (int) $job->invoice_id;
|
|
$transitionInvoiceId = $transition->invoice_id === null
|
|
? null : (int) $transition->invoice_id;
|
|
if ((string) $job->kind !== 'network_change'
|
|
|| (string) $job->execution_mode !== 'agent'
|
|
|| (int) $job->public_network_transition_id
|
|
!== (int) $transition->id
|
|
|| (int) $job->instance_id !== (int) $instance->id
|
|
|| (int) $transition->instance_id !== (int) $instance->id
|
|
|| (int) $job->host_id !== (int) $instance->host_id
|
|
|| (int) $transition->host_id !== (int) $instance->host_id
|
|
|| (int) $transition->reservation_id
|
|
!== (int) $instance->reservation_id
|
|
|| (int) $job->user_id
|
|
!== (int) $transition->requested_by_user_id
|
|
|| !hash_equals(
|
|
(string) $job->operation_id,
|
|
(string) $transition->operation_id
|
|
)
|
|
|| $jobInvoiceId !== $transitionInvoiceId
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) $transition->operation_id) !== 1
|
|
|| preg_match('/^[a-f0-9]{64}$/D',
|
|
(string) $transition->source_claim_set_sha256) !== 1
|
|
|| preg_match('/^[a-f0-9]{64}$/D',
|
|
(string) $transition->target_claim_set_sha256) !== 1) {
|
|
throw new CloudError('公网网络变更任务与冻结账本不一致', 409);
|
|
}
|
|
try {
|
|
$source = CloudPublicNetworkSnapshot::fromArray(json_decode(
|
|
(string) $transition->source_public_network_json,
|
|
true,
|
|
16,
|
|
JSON_THROW_ON_ERROR
|
|
));
|
|
$target = CloudPublicNetworkSnapshot::fromArray(json_decode(
|
|
(string) $transition->target_public_network_json,
|
|
true,
|
|
16,
|
|
JSON_THROW_ON_ERROR
|
|
));
|
|
$spec = json_decode(
|
|
(string) $instance->spec_json,
|
|
true,
|
|
64,
|
|
JSON_THROW_ON_ERROR
|
|
);
|
|
$projected = CloudPublicNetworkSnapshot::fromArray(
|
|
is_array($spec) ? ($spec['public_network'] ?? null) : null
|
|
);
|
|
} catch (\Throwable) {
|
|
throw new CloudError('公网网络变更冻结快照损坏', 409);
|
|
}
|
|
if (!hash_equals(
|
|
(string) $transition->source_public_network_sha256,
|
|
$source->fingerprint()
|
|
)
|
|
|| !hash_equals(
|
|
(string) $transition->target_public_network_sha256,
|
|
$target->fingerprint()
|
|
)
|
|
|| hash_equals($source->fingerprint(), $target->fingerprint())
|
|
|| (!hash_equals(
|
|
$projected->fingerprint(),
|
|
$source->fingerprint()
|
|
) && !hash_equals(
|
|
$projected->fingerprint(),
|
|
$target->fingerprint()
|
|
))) {
|
|
throw new CloudError('公网网络变更冻结快照摘要不一致', 409);
|
|
}
|
|
if ($requirePayload) {
|
|
$body = is_array($payload['body'] ?? null)
|
|
&& !array_is_list($payload['body'])
|
|
? $payload['body'] : null;
|
|
$bodyKeys = is_array($body) ? array_keys($body) : [];
|
|
$unexpected = array_diff(
|
|
$bodyKeys,
|
|
['operation_id', 'public_network', 'confirm_restart']
|
|
);
|
|
if ($body === null || $unexpected !== []
|
|
|| ($body['confirm_restart'] ?? null) !== true
|
|
|| ($payload['public_network_transition_id'] ?? null)
|
|
!== (int) $transition->id
|
|
|| !hash_equals(
|
|
(string) ($payload['source_claim_set_sha256'] ?? ''),
|
|
(string) $transition->source_claim_set_sha256
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['target_claim_set_sha256'] ?? ''),
|
|
(string) $transition->target_claim_set_sha256
|
|
)) {
|
|
throw new CloudError('公网网络变更任务载荷关联无效', 409);
|
|
}
|
|
try {
|
|
$requested = CloudPublicNetworkSnapshot::fromArray(
|
|
$body['public_network'] ?? null
|
|
);
|
|
} catch (\Throwable) {
|
|
throw new CloudError('公网网络变更任务目标无效', 409);
|
|
}
|
|
if (!hash_equals(
|
|
$requested->fingerprint(),
|
|
$target->fingerprint()
|
|
)) {
|
|
throw new CloudError('公网网络变更任务目标已漂移', 409);
|
|
}
|
|
}
|
|
return [$source, $target];
|
|
}
|
|
|
|
public static function assertInvoiceAssociation(
|
|
CloudPublicNetworkTransition $transition,
|
|
CloudJob $job,
|
|
?User $actor,
|
|
?array $payload,
|
|
bool $expectRefunded
|
|
): void {
|
|
if ((string) $transition->kind !== 'user_paid') {
|
|
if ($transition->invoice_id !== null || $job->invoice_id !== null
|
|
|| ($payload !== null && (
|
|
($payload['price_cents'] ?? null) !== null
|
|
|| ($payload['charged_amount'] ?? null) !== null
|
|
|| ($payload['currency'] ?? null) !== null
|
|
))) {
|
|
throw new CloudError('非付费公网网络变更账单关联无效', 409);
|
|
}
|
|
return;
|
|
}
|
|
if (!$actor instanceof User
|
|
|| (int) $actor->id !== (int) $transition->requested_by_user_id
|
|
|| $transition->invoice_id === null
|
|
|| (int) $job->invoice_id !== (int) $transition->invoice_id) {
|
|
throw new CloudError('付费公网网络变更账户关联无效', 409);
|
|
}
|
|
$invoice = CloudInvoice::where(
|
|
'id',
|
|
(int) $transition->invoice_id
|
|
)->lockForUpdate()->first();
|
|
if (!$invoice instanceof CloudInvoice
|
|
|| (string) $invoice->purpose
|
|
!== CloudIpChangeBillingService::PURPOSE
|
|
|| (int) $invoice->instance_id !== (int) $transition->instance_id
|
|
|| (int) $invoice->user_id !== (int) $actor->id
|
|
|| (string) $invoice->currency !== 'USD'
|
|
|| (string) $invoice->paid_via !== 'balance'
|
|
|| $invoice->paid_at === null
|
|
|| !hash_equals(
|
|
'public-network-transition-'
|
|
. (string) $transition->transition_key,
|
|
(string) $invoice->payment_reference
|
|
)
|
|
|| (string) $invoice->status
|
|
!== ($expectRefunded ? 'refunded' : 'paid')) {
|
|
throw new CloudError('付费公网网络变更账单关联不一致', 409);
|
|
}
|
|
if ($payload === null) {
|
|
return;
|
|
}
|
|
$priceCents = $payload['price_cents'] ?? null;
|
|
$amount = $payload['charged_amount'] ?? null;
|
|
if (!is_int($priceCents) || $priceCents < 1
|
|
|| !is_string($amount)
|
|
|| ($payload['currency'] ?? null) !== 'USD'
|
|
|| CloudMoney::cents($amount) !== $priceCents
|
|
|| CloudMoney::cents((string) $invoice->amount) !== $priceCents) {
|
|
throw new CloudError('付费公网网络变更冻结金额不一致', 409);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lock and validate the exact dual-stack ledger frozen by a transition.
|
|
*
|
|
* @return array{legs:array<int,object>,source_claims:array<int,object>,
|
|
* target_claims:array<int,object>,source_ips:array<int,CloudPublicIp>,
|
|
* target_ips:array<int,CloudPublicIp>,projection:string}
|
|
*/
|
|
public static function lockLedger(
|
|
CloudInstance $instance,
|
|
CloudPublicNetworkTransition $transition,
|
|
CloudPublicNetworkSnapshot $source,
|
|
CloudPublicNetworkSnapshot $target
|
|
): array {
|
|
self::lockFrozenTopologyLocked($transition, $source, $target);
|
|
$reservation = CloudResourceReservation::where(
|
|
'id',
|
|
(int) $transition->reservation_id
|
|
)->lockForUpdate()->first();
|
|
if (!$reservation instanceof CloudResourceReservation
|
|
|| (string) $reservation->state !== 'committed'
|
|
|| (int) $reservation->instance_id !== (int) $instance->id
|
|
|| (int) $reservation->host_id !== (int) $instance->host_id
|
|
|| (int) $reservation->resource_pool_id
|
|
!== (int) $instance->resource_pool_id) {
|
|
throw new CloudError('公网网络变更资源预留关联不一致', 409);
|
|
}
|
|
$legRows = DB::table('cloud_public_network_transition_ips')
|
|
->where('transition_id', (int) $transition->id)
|
|
->orderBy('address_family')->orderBy('id')
|
|
->lockForUpdate()->get()->all();
|
|
$legs = [];
|
|
$claimIds = [];
|
|
foreach ($legRows as $leg) {
|
|
$family = (int) ($leg->address_family ?? 0);
|
|
if (!in_array($family, self::FAMILIES, true)
|
|
|| isset($legs[$family])
|
|
|| (int) ($leg->source_claim_id ?? 0) < 1
|
|
|| ((bool) ($leg->changed ?? false))
|
|
!== ($leg->target_claim_id !== null)) {
|
|
throw new CloudError('公网网络变更双栈明细损坏', 409);
|
|
}
|
|
$legs[$family] = $leg;
|
|
$claimIds[] = (int) $leg->source_claim_id;
|
|
if ($leg->target_claim_id !== null) {
|
|
$claimIds[] = (int) $leg->target_claim_id;
|
|
}
|
|
}
|
|
if (array_keys($legs) !== self::FAMILIES) {
|
|
throw new CloudError('公网网络变更双栈明细不完整', 409);
|
|
}
|
|
$claimIds = array_values(array_unique($claimIds));
|
|
sort($claimIds, SORT_NUMERIC);
|
|
$claims = DB::table('cloud_resource_reservation_ips')
|
|
->whereIn('id', $claimIds)->orderBy('id')
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
if ($claims->count() !== count($claimIds)) {
|
|
throw new CloudError('公网网络变更地址声明缺失', 409);
|
|
}
|
|
$ipIds = [];
|
|
foreach ($claims as $claim) {
|
|
$ipIds[] = (int) $claim->public_ip_id;
|
|
}
|
|
$ipIds = array_values(array_unique($ipIds));
|
|
sort($ipIds, SORT_NUMERIC);
|
|
$ips = CloudPublicIp::whereIn('id', $ipIds)->orderBy('id')
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
if ($ips->count() !== count($ipIds)) {
|
|
throw new CloudError('公网网络变更地址记录缺失', 409);
|
|
}
|
|
|
|
$sourceClaims = [];
|
|
$targetClaims = [];
|
|
$sourceIps = [];
|
|
$targetIps = [];
|
|
$projection = null;
|
|
foreach (self::FAMILIES as $family) {
|
|
$leg = $legs[$family];
|
|
$sourceClaim = $claims->get((int) $leg->source_claim_id);
|
|
$sourceIp = is_object($sourceClaim)
|
|
? $ips->get((int) $sourceClaim->public_ip_id) : null;
|
|
if (!is_object($sourceClaim)
|
|
|| !$sourceIp instanceof CloudPublicIp) {
|
|
throw new CloudError('公网网络变更源声明缺失', 409);
|
|
}
|
|
self::assertFrozenRowIdentity(
|
|
$sourceClaim,
|
|
$sourceIp,
|
|
$family,
|
|
(int) $reservation->id,
|
|
$source,
|
|
(int) $transition->source_network_attachment_id,
|
|
(int) $transition->host_id
|
|
);
|
|
$sourceClaims[$family] = $sourceClaim;
|
|
$sourceIps[$family] = $sourceIp;
|
|
if (!(bool) $leg->changed) {
|
|
if ($leg->target_claim_id !== null
|
|
|| !self::sourceActive(
|
|
$sourceClaim,
|
|
$sourceIp,
|
|
(int) $instance->id
|
|
)
|
|
|| !hash_equals(
|
|
$source->address($family),
|
|
$target->address($family)
|
|
)) {
|
|
throw new CloudError('公网网络变更未变更明细不一致', 409);
|
|
}
|
|
$targetClaims[$family] = $sourceClaim;
|
|
$targetIps[$family] = $sourceIp;
|
|
continue;
|
|
}
|
|
$targetClaim = $claims->get((int) $leg->target_claim_id);
|
|
$targetIp = is_object($targetClaim)
|
|
? $ips->get((int) $targetClaim->public_ip_id) : null;
|
|
if (!is_object($targetClaim)
|
|
|| !$targetIp instanceof CloudPublicIp
|
|
|| (int) $targetClaim->id === (int) $sourceClaim->id) {
|
|
throw new CloudError('公网网络变更目标声明缺失', 409);
|
|
}
|
|
self::assertFrozenRowIdentity(
|
|
$targetClaim,
|
|
$targetIp,
|
|
$family,
|
|
(int) $reservation->id,
|
|
$target,
|
|
(int) $transition->target_network_attachment_id,
|
|
(int) $transition->host_id
|
|
);
|
|
$targetClaims[$family] = $targetClaim;
|
|
$targetIps[$family] = $targetIp;
|
|
$legProjection = self::legProjection(
|
|
$sourceClaim,
|
|
$sourceIp,
|
|
$targetClaim,
|
|
$targetIp,
|
|
$instance,
|
|
$transition
|
|
);
|
|
if ($projection === null) {
|
|
$projection = $legProjection;
|
|
} elseif ($projection !== $legProjection) {
|
|
throw new CloudError('公网网络变更双栈投影部分提交', 409);
|
|
}
|
|
}
|
|
$projection ??= 'prepared';
|
|
if (!hash_equals(
|
|
self::frozenClaimSetDigest(
|
|
(int) $reservation->id,
|
|
$sourceClaims,
|
|
$sourceIps,
|
|
$source
|
|
),
|
|
(string) $transition->source_claim_set_sha256
|
|
)
|
|
|| !hash_equals(
|
|
self::frozenClaimSetDigest(
|
|
(int) $reservation->id,
|
|
$targetClaims,
|
|
$targetIps,
|
|
$target
|
|
),
|
|
(string) $transition->target_claim_set_sha256
|
|
)) {
|
|
throw new CloudError('公网网络变更声明集合摘要不一致', 409);
|
|
}
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
$projected = CloudPublicNetworkSnapshot::fromArray(
|
|
is_array($spec) ? ($spec['public_network'] ?? null) : null
|
|
);
|
|
$expectedSpec = $projection === 'applied' ? $target : $source;
|
|
if (!hash_equals(
|
|
$projected->fingerprint(),
|
|
$expectedSpec->fingerprint()
|
|
)) {
|
|
throw new CloudError('公网网络变更实例投影与账本不一致', 409);
|
|
}
|
|
return [
|
|
'legs' => $legs,
|
|
'source_claims' => $sourceClaims,
|
|
'target_claims' => $targetClaims,
|
|
'source_ips' => $sourceIps,
|
|
'target_ips' => $targetIps,
|
|
'projection' => $projection,
|
|
];
|
|
}
|
|
|
|
private static function lockFrozenTopologyLocked(
|
|
CloudPublicNetworkTransition $transition,
|
|
CloudPublicNetworkSnapshot $source,
|
|
CloudPublicNetworkSnapshot $target
|
|
): void {
|
|
$attachmentIds = array_values(array_unique([
|
|
(int) $transition->source_network_attachment_id,
|
|
(int) $transition->target_network_attachment_id,
|
|
]));
|
|
sort($attachmentIds, SORT_NUMERIC);
|
|
$attachments = CloudHostNetworkAttachment::whereIn(
|
|
'id', $attachmentIds
|
|
)->orderBy('id')->lockForUpdate()->get();
|
|
if ($attachments->count() !== count($attachmentIds)) {
|
|
throw new CloudError('公网网络变更冻结附件缺失', 409);
|
|
}
|
|
|
|
$poolKeys = [];
|
|
foreach ([$source->toArray(), $target->toArray()] as $snapshot) {
|
|
foreach (['ipv4', 'ipv6'] as $leg) {
|
|
$poolKeys[(string) $snapshot[$leg]['pool_key']] = true;
|
|
}
|
|
}
|
|
$poolKeys = array_keys($poolKeys);
|
|
sort($poolKeys, SORT_STRING);
|
|
$pools = CloudPublicIpPool::where(
|
|
'host_id', (int) $transition->host_id
|
|
)->whereIn('pool_key', $poolKeys)->orderBy('id')
|
|
->lockForUpdate()->get();
|
|
if ($pools->count() !== count($poolKeys)) {
|
|
throw new CloudError('公网网络变更冻结地址池缺失', 409);
|
|
}
|
|
}
|
|
|
|
private static function assertFrozenRowIdentity(
|
|
object $claim,
|
|
CloudPublicIp $ip,
|
|
int $family,
|
|
int $reservationId,
|
|
CloudPublicNetworkSnapshot $network,
|
|
int $attachmentId,
|
|
int $hostId
|
|
): void {
|
|
if ((int) ($claim->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($claim->address_family ?? 0) !== $family
|
|
|| (int) ($claim->claim_generation ?? 0) < 1
|
|
|| (int) ($claim->network_attachment_id ?? 0) !== $attachmentId
|
|
|| (int) ($claim->public_ip_pool_id ?? 0) < 1
|
|
|| (int) ($claim->public_ip_id ?? 0) !== (int) $ip->id
|
|
|| (int) $ip->host_id !== $hostId
|
|
|| (int) $ip->address_family !== $family
|
|
|| (int) $ip->public_ip_pool_id
|
|
!== (int) $claim->public_ip_pool_id
|
|
|| !hash_equals(
|
|
(string) $ip->address . '/' . (int) $ip->prefix_length,
|
|
$network->address($family)
|
|
)
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) ($claim->operation_id ?? '')) !== 1) {
|
|
throw new CloudError('公网网络变更声明具体身份不一致', 409);
|
|
}
|
|
}
|
|
|
|
private static function sourceActive(
|
|
object $claim,
|
|
CloudPublicIp $ip,
|
|
int $instanceId
|
|
): bool {
|
|
return (int) ($claim->active_claim ?? 0) === 1
|
|
&& (string) ($claim->state ?? '') === 'committed'
|
|
&& $claim->released_at === null
|
|
&& (string) $ip->state === 'assigned'
|
|
&& (int) ($ip->reservation_id ?? 0)
|
|
=== (int) $claim->reservation_id
|
|
&& (int) ($ip->reservation_ip_claim_id ?? 0)
|
|
=== (int) $claim->id
|
|
&& (int) ($ip->instance_id ?? 0) === $instanceId
|
|
&& hash_equals(
|
|
(string) ($claim->operation_id ?? ''),
|
|
(string) ($ip->operation_id ?? '')
|
|
);
|
|
}
|
|
|
|
private static function legProjection(
|
|
object $sourceClaim,
|
|
CloudPublicIp $sourceIp,
|
|
object $targetClaim,
|
|
CloudPublicIp $targetIp,
|
|
CloudInstance $instance,
|
|
CloudPublicNetworkTransition $transition
|
|
): string {
|
|
$sourceActive = self::sourceActive(
|
|
$sourceClaim,
|
|
$sourceIp,
|
|
(int) $instance->id
|
|
);
|
|
$sourceReleased = $sourceClaim->active_claim === null
|
|
&& (string) $sourceClaim->state === 'released'
|
|
&& $sourceClaim->released_at !== null
|
|
&& (string) $sourceClaim->release_reason
|
|
=== 'public_network_transition_succeeded'
|
|
&& (string) $sourceIp->state === 'available'
|
|
&& $sourceIp->reservation_id === null
|
|
&& $sourceIp->reservation_ip_claim_id === null
|
|
&& $sourceIp->instance_id === null
|
|
&& $sourceIp->operation_id === null;
|
|
$targetPrepared = $targetClaim->active_claim === null
|
|
&& in_array((string) $targetClaim->state,
|
|
['reserved', 'uncertain'], true)
|
|
&& $targetClaim->released_at === null
|
|
&& (string) $targetIp->state === (string) $targetClaim->state
|
|
&& (int) ($targetIp->reservation_id ?? 0)
|
|
=== (int) $transition->reservation_id
|
|
&& (int) ($targetIp->reservation_ip_claim_id ?? 0)
|
|
=== (int) $targetClaim->id
|
|
&& $targetIp->instance_id === null
|
|
&& hash_equals(
|
|
(string) $transition->operation_id,
|
|
(string) $targetClaim->operation_id
|
|
)
|
|
&& hash_equals(
|
|
(string) $transition->operation_id,
|
|
(string) $targetIp->operation_id
|
|
);
|
|
$targetActive = (int) ($targetClaim->active_claim ?? 0) === 1
|
|
&& (string) $targetClaim->state === 'committed'
|
|
&& $targetClaim->released_at === null
|
|
&& (string) $targetIp->state === 'assigned'
|
|
&& (int) ($targetIp->reservation_id ?? 0)
|
|
=== (int) $transition->reservation_id
|
|
&& (int) ($targetIp->reservation_ip_claim_id ?? 0)
|
|
=== (int) $targetClaim->id
|
|
&& (int) ($targetIp->instance_id ?? 0) === (int) $instance->id
|
|
&& hash_equals(
|
|
(string) $transition->operation_id,
|
|
(string) $targetClaim->operation_id
|
|
)
|
|
&& hash_equals(
|
|
(string) $transition->operation_id,
|
|
(string) $targetIp->operation_id
|
|
);
|
|
$targetReleased = $targetClaim->active_claim === null
|
|
&& (string) $targetClaim->state === 'released'
|
|
&& $targetClaim->released_at !== null
|
|
&& (string) $targetClaim->release_reason
|
|
=== 'public_network_transition_not_applied'
|
|
&& (string) $targetIp->state === 'available'
|
|
&& $targetIp->reservation_id === null
|
|
&& $targetIp->reservation_ip_claim_id === null
|
|
&& $targetIp->instance_id === null
|
|
&& $targetIp->operation_id === null;
|
|
if ($sourceActive && $targetPrepared) {
|
|
return 'prepared';
|
|
}
|
|
if ($sourceReleased && $targetActive) {
|
|
return 'applied';
|
|
}
|
|
if ($sourceActive && $targetReleased) {
|
|
return 'not_applied';
|
|
}
|
|
throw new CloudError('公网网络变更地址账本部分提交', 409);
|
|
}
|
|
|
|
private static function frozenClaimSetDigest(
|
|
int $reservationId,
|
|
array $claims,
|
|
array $ips,
|
|
CloudPublicNetworkSnapshot $network
|
|
): string {
|
|
$canonical = [];
|
|
$attachmentId = null;
|
|
foreach (self::FAMILIES as $family) {
|
|
$claim = $claims[$family] ?? null;
|
|
$ip = $ips[$family] ?? null;
|
|
if (!is_object($claim) || !$ip instanceof CloudPublicIp
|
|
|| (int) ($claim->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($claim->address_family ?? 0) !== $family
|
|
|| (int) ($claim->public_ip_id ?? 0) !== (int) $ip->id
|
|
|| !hash_equals(
|
|
(string) $ip->address . '/' . (int) $ip->prefix_length,
|
|
$network->address($family)
|
|
)) {
|
|
throw new CloudError('公网网络变更摘要输入不一致', 409);
|
|
}
|
|
$currentAttachment = (int) $claim->network_attachment_id;
|
|
if ($attachmentId === null) {
|
|
$attachmentId = $currentAttachment;
|
|
} elseif ($attachmentId !== $currentAttachment) {
|
|
throw new CloudError('公网网络变更摘要跨越网络接入', 409);
|
|
}
|
|
$canonical[] = [
|
|
'family' => $family,
|
|
'claim_id' => (int) $claim->id,
|
|
'claim_generation' => (int) $claim->claim_generation,
|
|
'claim_operation_id' => (string) $claim->operation_id,
|
|
'public_ip_id' => (int) $claim->public_ip_id,
|
|
'network_attachment_id' => $currentAttachment,
|
|
'public_ip_pool_id' => (int) $claim->public_ip_pool_id,
|
|
];
|
|
}
|
|
return hash('sha256', json_encode([
|
|
'reservation_id' => $reservationId,
|
|
'claims' => $canonical,
|
|
'public_network_sha256' => $network->fingerprint(),
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
|
|
}
|
|
}
|