feat: add inline instance display-name editing
parent
b8105fab23
commit
3f9d3d2d63
@ -0,0 +1,45 @@
|
||||
<?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];
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026100403' scope='global'}
|
||||
{assign var=venix_asset_version value='2026100404' scope='global'}
|
||||
|
||||
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudError;
|
||||
use App\Services\Cloud\CloudInstanceNameService;
|
||||
use App\Controllers\CloudApiController;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Slim\Http\Environment;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$serviceSource = file_get_contents(
|
||||
dirname(__DIR__) . '/app/Services/Cloud/CloudInstanceNameService.php'
|
||||
);
|
||||
$assert(str_contains($serviceSource, '->lockForUpdate()')
|
||||
&& str_contains($serviceSource, 'hash_equals($currentName, $name)')
|
||||
&& str_contains($serviceSource, 'hash_equals($currentName, $initialName)'),
|
||||
'Display-name persistence lost its compare-and-swap concurrency guard.');
|
||||
$expectError = static function (callable $callback, int $status, string $message) use ($assert): void {
|
||||
try {
|
||||
$callback();
|
||||
} catch (CloudError $error) {
|
||||
$assert($error->httpStatus === $status,
|
||||
$message . ' Unexpected status: ' . $error->httpStatus);
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException($message);
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$capsule->schema('default')->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('uuid');
|
||||
$table->integer('user_id');
|
||||
$table->string('name');
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
|
||||
$rows = [
|
||||
['id' => 1, 'uuid' => '11111111-1111-4111-8111-111111111111',
|
||||
'user_id' => 1, 'name' => 'vm-owner1', 'status' => 'active',
|
||||
'spec_json' => '{"resource_name":"vc-immutable-owner"}', 'updated_at' => 10],
|
||||
['id' => 2, 'uuid' => '22222222-2222-4222-8222-222222222222',
|
||||
'user_id' => 2, 'name' => 'vm-owner2', 'status' => 'active',
|
||||
'spec_json' => '{"resource_name":"vc-immutable-foreign"}', 'updated_at' => 20],
|
||||
['id' => 3, 'uuid' => '33333333-3333-4333-8333-333333333333',
|
||||
'user_id' => 1, 'name' => 'vm-deleting', 'status' => 'deleting',
|
||||
'spec_json' => '{}', 'updated_at' => 30],
|
||||
['id' => 4, 'uuid' => '44444444-4444-4444-8444-444444444444',
|
||||
'user_id' => 1, 'name' => 'vm-deleted', 'status' => 'deleted',
|
||||
'spec_json' => '{}', 'updated_at' => 40],
|
||||
];
|
||||
Capsule::table('cloud_instances')->insert($rows);
|
||||
|
||||
$owner = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false,
|
||||
'can_manage' => false];
|
||||
$readonlyAdmin = ['user_id' => 1, 'is_admin' => true, 'is_super_admin' => false,
|
||||
'can_manage' => false];
|
||||
$global = ['user_id' => 99, 'is_admin' => true, 'is_super_admin' => true,
|
||||
'can_manage' => true];
|
||||
|
||||
$result = CloudInstanceNameService::rename(1, $owner, 'Prod.Web_01');
|
||||
$renamed = Capsule::table('cloud_instances')->where('id', 1)->first();
|
||||
$assert($result === ['id' => '1', 'name' => 'Prod.Web_01']
|
||||
&& $renamed->name === 'Prod.Web_01'
|
||||
&& $renamed->uuid === $rows[0]['uuid']
|
||||
&& $renamed->spec_json === $rows[0]['spec_json']
|
||||
&& $renamed->status === 'active'
|
||||
&& (int) $renamed->updated_at > 10,
|
||||
'Owner rename did not persist only the panel display identity.');
|
||||
|
||||
$sameTimestamp = (int) $renamed->updated_at;
|
||||
$assert(CloudInstanceNameService::rename(1, $owner, 'Prod.Web_01')['name'] === 'Prod.Web_01'
|
||||
&& (int) Capsule::table('cloud_instances')->where('id', 1)->value('updated_at')
|
||||
=== $sameTimestamp,
|
||||
'Idempotent rename rewrote the instance row.');
|
||||
|
||||
foreach ([null, '', '-bad', 'space name', str_repeat('a', 64), 'bad/segment'] as $invalid) {
|
||||
$expectError(static fn () => CloudInstanceNameService::rename(1, $owner, $invalid),
|
||||
422, 'Invalid display name was accepted.');
|
||||
}
|
||||
$apiRequest = Request::createFromEnvironment(Environment::mock([
|
||||
'REQUEST_METHOD' => 'PATCH', 'REQUEST_URI' => '/api/v1/vms/1/display-name',
|
||||
]))->withParsedBody(['name' => 'Api-Label_02'])
|
||||
->withAttribute('cloud_actor', $owner);
|
||||
$apiResponse = (new CloudApiController())->displayName(
|
||||
$apiRequest, new Response(), ['id' => '1']
|
||||
);
|
||||
$apiPayload = json_decode((string) $apiResponse->getBody(), true);
|
||||
$assert($apiResponse->getStatusCode() === 200
|
||||
&& $apiResponse->getHeaderLine('Cache-Control') === 'no-store'
|
||||
&& $apiPayload === ['id' => '1', 'name' => 'Api-Label_02']
|
||||
&& Capsule::table('cloud_instances')->where('id', 1)->value('name') === 'Api-Label_02',
|
||||
'Display-name controller did not return a non-cacheable bounded response.');
|
||||
$extraFieldRequest = $apiRequest->withParsedBody([
|
||||
'name' => 'Rejected', 'uuid' => $rows[0]['uuid'],
|
||||
]);
|
||||
$expectError(static fn () => (new CloudApiController())->displayName(
|
||||
$extraFieldRequest, new Response(), ['id' => '1']
|
||||
), 422, 'Display-name endpoint accepted an extra mutable field.');
|
||||
|
||||
$expectError(static fn () => CloudInstanceNameService::rename(2, $owner, 'cross-tenant'),
|
||||
404, 'Ordinary owner renamed another tenant instance.');
|
||||
$expectError(static fn () => CloudInstanceNameService::rename(2, $readonlyAdmin, 'cross-tenant'),
|
||||
404, 'Read-only administrator inherited global rename permission.');
|
||||
$expectError(static fn () => CloudInstanceNameService::rename(3, $owner, 'too-late'),
|
||||
409, 'Deleting instance accepted a display-name mutation.');
|
||||
$expectError(static fn () => CloudInstanceNameService::rename(4, $owner, 'resurrected'),
|
||||
404, 'Deleted instance accepted a display-name mutation.');
|
||||
|
||||
$globalResult = CloudInstanceNameService::rename(2, $global, 'Prod.Web_01');
|
||||
$assert($globalResult['name'] === 'Prod.Web_01'
|
||||
&& Capsule::table('cloud_instances')->where('id', 2)->value('name') === 'Prod.Web_01',
|
||||
'Super administrator rename failed or an unsupported uniqueness rule was introduced.');
|
||||
|
||||
echo "Cloud instance display-name ownership and persistence passed.\n";
|
||||
Loading…
Reference in New Issue