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

143 lines
5.2 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudNetworkLine;
use App\Models\CloudResourcePool;
use App\Models\CloudServicePeriod;
use App\Models\CloudTrafficPoolGroup;
/**
* Freezes the traffic-pool identity at the service-period boundary.
*
* Live line configuration is read only while an order snapshot is created.
* Provisioning consumes that snapshot, while renewals inherit the previous
* period. An unpooled period may follow the legacy upgrade rules, while a
* pooled period cannot change quota mode or move to another pool.
*/
final class CloudTrafficPoolPeriodBinding
{
/** @return array{traffic_pool_group_id:?int,traffic_quota_mode:string} */
public static function purchaseSnapshot(
CloudResourcePool $resourcePool,
CloudNetworkLine $line,
int $quotaBytes
): array {
if ((int) $resourcePool->line_id !== (int) $line->id) {
throw new CloudError('资源池与线路配置不一致', 409);
}
// Administrator mapping and order snapshots share one catalog lock
// order: network line first, then its current traffic-pool group.
// Re-reading the line under lock prevents a NULL -> group mapping from
// racing an order that was built from a stale model instance.
$lockedLine = CloudNetworkLine::where('id', (int) $line->id)
->lockForUpdate()->first();
if ($lockedLine === null
|| (int) $resourcePool->line_id !== (int) $lockedLine->id) {
throw new CloudError('资源池与线路配置不一致', 409);
}
$groupId = self::nullableGroupId(
$lockedLine->traffic_pool_group_id ?? null
);
if ($groupId !== null) {
$group = CloudTrafficPoolGroup::where('id', $groupId)
->lockForUpdate()->first();
if ($group === null) {
throw new CloudError('线路绑定的流量池分组不存在', 409);
}
}
return [
'traffic_pool_group_id' => $groupId,
'traffic_quota_mode' => self::quotaMode($quotaBytes),
];
}
/** @return array{traffic_pool_group_id:?int,traffic_quota_mode:string} */
public static function orderSnapshot(array $spec, int $quotaBytes): array
{
if (!array_key_exists('traffic_pool_group_id', $spec)
|| !array_key_exists('traffic_quota_mode', $spec)) {
throw new CloudError('订单缺少不可变的流量池计费快照', 409);
}
$snapshotGroup = $spec['traffic_pool_group_id'];
if ($snapshotGroup !== null
&& (!is_int($snapshotGroup) || $snapshotGroup < 1)) {
throw new CloudError('订单流量池分组快照无效', 409);
}
$groupId = $snapshotGroup;
$expectedMode = self::quotaMode($quotaBytes);
$snapshotMode = $spec['traffic_quota_mode'];
if (!is_string($snapshotMode)
|| !in_array($snapshotMode, ['finite', 'unlimited'], true)
|| $snapshotMode !== $expectedMode) {
throw new CloudError('订单流量池计费快照不一致', 409);
}
return [
'traffic_pool_group_id' => $groupId,
'traffic_quota_mode' => $snapshotMode,
];
}
public static function bindPeriod(
CloudServicePeriod $period,
?int $trafficPoolGroupId,
int $quotaBytes
): void {
$groupId = self::nullableGroupId(
$trafficPoolGroupId
);
$quotaMode = self::quotaMode($quotaBytes);
if ($period->exists) {
self::assertUpgradeQuotaTransition(
$groupId,
(string) $period->traffic_quota_mode,
$quotaBytes
);
}
$period->traffic_pool_group_id = $groupId;
$period->traffic_quota_mode = $quotaMode;
}
/** Reject a pooled quota-mode change before a paid upgrade is created. */
public static function assertUpgradeQuotaTransition(
?int $trafficPoolGroupId,
string $sourceMode,
int $targetQuotaBytes
): void {
$groupId = self::nullableGroupId($trafficPoolGroupId);
$targetMode = self::quotaMode($targetQuotaBytes);
if ($groupId !== null
&& in_array($sourceMode, ['finite', 'unlimited'], true)
&& $sourceMode !== $targetMode) {
throw new CloudError(
'活动服务周期不支持在有限与无限流量之间切换',
409
);
}
}
public static function quotaMode(int $quotaBytes): string
{
if ($quotaBytes < 0) {
throw new CloudError('服务周期流量额度不能为负数', 409);
}
return $quotaBytes === 0 ? 'unlimited' : 'finite';
}
private static function nullableGroupId(mixed $value): ?int
{
if ($value === null || $value === '') {
return null;
}
if (is_int($value) && $value > 0) {
return $value;
}
if (is_string($value)
&& preg_match('/^[1-9][0-9]*$/D', $value) === 1
&& (int) $value > 0) {
return (int) $value;
}
throw new CloudError('流量池分组快照无效', 409);
}
}