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.
46 lines
1.9 KiB
PHP
46 lines
1.9 KiB
PHP
<?php
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
/** Persist the user-facing label without mutating any engine resource. */
|
|
final class CloudInstanceNameService
|
|
{
|
|
public static function rename($id, array $actor, $value): array
|
|
{
|
|
$name = CloudInput::name($value);
|
|
$instance = CloudAccess::instance($id, $actor, true);
|
|
if ((string) $instance->status !== 'active') {
|
|
throw new CloudError('当前实例状态不允许修改名称', 409);
|
|
}
|
|
|
|
$initialName = (string) $instance->name;
|
|
return $instance->getConnection()->transaction(static function () use (
|
|
$instance, $initialName, $name
|
|
): array {
|
|
// Compare under a row lock in PHP. The production column uses a
|
|
// case-insensitive collation, so a SQL name predicate cannot be a
|
|
// safe compare-and-swap guard for case-preserving display names.
|
|
$current = CloudInstance::where('id', (int) $instance->id)
|
|
->where('uuid', (string) $instance->uuid)
|
|
->where('user_id', (int) $instance->user_id)
|
|
->lockForUpdate()
|
|
->first(['id', 'name', 'status']);
|
|
if ($current === null || (string) $current->status !== 'active') {
|
|
throw new CloudError('实例状态已变化,请刷新后重试', 409);
|
|
}
|
|
$currentName = (string) $current->name;
|
|
if (hash_equals($currentName, $name)) {
|
|
return ['id' => (string) $current->id, 'name' => $name];
|
|
}
|
|
if (!hash_equals($currentName, $initialName)) {
|
|
throw new CloudError('实例名称已变化,请刷新后重试', 409);
|
|
}
|
|
$current->name = $name;
|
|
$current->updated_at = time();
|
|
$current->save();
|
|
return ['id' => (string) $current->id, 'name' => $name];
|
|
});
|
|
}
|
|
}
|