|
|
|
|
|
<?php
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
|
|
$System_Config['cloud_data_key'] = base64_encode(str_repeat('n', 32));
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudPublicIp;
|
|
|
|
|
|
use App\Models\CloudPublicIpPool;
|
|
|
|
|
|
use App\Services\Cloud\CloudAdminHostNetworkService;
|
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
$expectCloudError = static function (callable $callable, int $status,
|
|
|
|
|
|
string $messagePart) use ($assert): CloudError {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$callable();
|
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
|
$assert($error->httpStatus === $status,
|
|
|
|
|
|
'Unexpected HTTP status: ' . $error->httpStatus);
|
|
|
|
|
|
$assert(str_contains($error->getMessage(), $messagePart),
|
|
|
|
|
|
'Unexpected Cloud error: ' . $error->getMessage());
|
|
|
|
|
|
return $error;
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new RuntimeException('Expected CloudError was not thrown.');
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
|
|
|
|
], 'default');
|
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
|
|
|
|
|
|
|
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->string('name');
|
|
|
|
|
|
$table->boolean('enabled')->default(true);
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
|
|
|
|
$table->integer('host_id')->primary();
|
|
|
|
|
|
$table->text('sealed_api_secret');
|
|
|
|
|
|
$table->boolean('enabled');
|
|
|
|
|
|
$table->integer('revision');
|
|
|
|
|
|
$table->integer('tested_revision');
|
|
|
|
|
|
$table->string('last_test_status');
|
|
|
|
|
|
$table->string('last_error_code');
|
|
|
|
|
|
$table->string('last_agent_version');
|
|
|
|
|
|
$table->text('network_selectors_json');
|
|
|
|
|
|
$table->integer('last_tested_at');
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->string('name');
|
|
|
|
|
|
$table->string('status');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->integer('line_id');
|
|
|
|
|
|
$table->string('status');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_host_resource_pool_memberships',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('resource_pool_id');
|
|
|
|
|
|
$table->string('scheduler_state');
|
|
|
|
|
|
$table->integer('priority')->default(0);
|
|
|
|
|
|
$table->integer('weight')->default(100);
|
|
|
|
|
|
$table->integer('revision')->default(1);
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_host_network_attachments',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->string('attachment_key');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('line_id');
|
|
|
|
|
|
$table->string('name');
|
|
|
|
|
|
$table->string('network_mode');
|
|
|
|
|
|
$table->string('network_selector');
|
|
|
|
|
|
$table->integer('vlan_id')->nullable();
|
|
|
|
|
|
$table->integer('routing_table')->nullable();
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
$table->integer('priority');
|
|
|
|
|
|
$table->integer('config_revision');
|
|
|
|
|
|
$table->integer('verified_agent_revision');
|
|
|
|
|
|
$table->string('verification_state');
|
|
|
|
|
|
$table->integer('last_verified_at');
|
|
|
|
|
|
$table->string('verification_error_code');
|
|
|
|
|
|
$table->integer('desired_revision');
|
|
|
|
|
|
$table->integer('observed_revision');
|
|
|
|
|
|
$table->string('sync_state');
|
|
|
|
|
|
$table->string('last_error_code');
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_public_ip_pools',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->string('pool_key');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('network_attachment_id');
|
|
|
|
|
|
$table->string('name');
|
|
|
|
|
|
$table->string('cidr');
|
|
|
|
|
|
$table->string('gateway');
|
|
|
|
|
|
$table->integer('guest_prefix_length');
|
|
|
|
|
|
$table->text('dns_servers_json');
|
|
|
|
|
|
$table->text('reserved_ips_json');
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
$table->integer('priority');
|
|
|
|
|
|
$table->integer('desired_revision');
|
|
|
|
|
|
$table->integer('observed_revision');
|
|
|
|
|
|
$table->string('sync_state');
|
|
|
|
|
|
$table->string('last_error_code');
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('public_ip_pool_id');
|
|
|
|
|
|
$table->string('address')->unique();
|
|
|
|
|
|
$table->integer('prefix_length');
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
$table->integer('reservation_id')->nullable()->unique();
|
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
|
$table->string('operation_id')->nullable()->unique();
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_resource_reservations',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('public_ip_pool_id')->nullable();
|
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_public_ip_pool_replacement_batches',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->string('batch_key');
|
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
|
$table->integer('source_public_ip_pool_id');
|
|
|
|
|
|
$table->integer('target_public_ip_pool_id');
|
|
|
|
|
|
$table->string('source_initial_state');
|
|
|
|
|
|
$table->integer('parent_job_id');
|
|
|
|
|
|
$table->integer('requested_by_user_id');
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
$table->integer('active_claim')->nullable();
|
|
|
|
|
|
$table->string('last_error_code')->default('');
|
|
|
|
|
|
$table->integer('terminal_at')->nullable();
|
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
|
});
|
|
|
|
|
|
$schema->create('cloud_public_ip_pool_replacement_batch_items',
|
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
|
$table->integer('batch_id');
|
|
|
|
|
|
$table->integer('instance_id');
|
|
|
|
|
|
$table->integer('child_job_id');
|
|
|
|
|
|
$table->string('state');
|
|
|
|
|
|
$table->string('last_error_code')->default('');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
|
$hostId = Capsule::table('cloud_hosts')->insertGetId([
|
|
|
|
|
|
'name' => 'Host 1', 'enabled' => 1,
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$foreignHostId = Capsule::table('cloud_hosts')->insertGetId([
|
|
|
|
|
|
'name' => 'Host 2', 'enabled' => 1,
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$lineId = Capsule::table('cloud_network_lines')->insertGetId([
|
|
|
|
|
|
'name' => 'China optimized', 'status' => 'active',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$otherLineId = Capsule::table('cloud_network_lines')->insertGetId([
|
|
|
|
|
|
'name' => 'No membership', 'status' => 'active',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$resourcePoolId = Capsule::table('cloud_resource_pools')->insertGetId([
|
|
|
|
|
|
'line_id' => $lineId, 'status' => 'active',
|
|
|
|
|
|
]);
|
|
|
|
|
|
Capsule::table('cloud_host_resource_pool_memberships')->insert([
|
|
|
|
|
|
'host_id' => $hostId, 'resource_pool_id' => $resourcePoolId,
|
|
|
|
|
|
'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100,
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$selectors = ['bridge:br0', 'bridge:br1', 'bridge:br2'];
|
|
|
|
|
|
Capsule::table('cloud_host_agents')->insert([
|
|
|
|
|
|
'host_id' => $hostId, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
|
|
|
|
|
|
'revision' => 1, 'tested_revision' => 1,
|
|
|
|
|
|
'last_test_status' => 'healthy', 'last_error_code' => '',
|
|
|
|
|
|
'last_agent_version' => 'test',
|
|
|
|
|
|
'network_selectors_json' => json_encode($selectors, JSON_THROW_ON_ERROR),
|
|
|
|
|
|
'last_tested_at' => $now, 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$selector = 'bridge:br0';
|
|
|
|
|
|
$attachmentId = Capsule::table('cloud_host_network_attachments')->insertGetId([
|
|
|
|
|
|
'attachment_key' => substr(hash('sha256', $selector), 0, 32),
|
|
|
|
|
|
'host_id' => $hostId, 'line_id' => $lineId,
|
|
|
|
|
|
'name' => $selector, 'network_mode' => 'selector',
|
|
|
|
|
|
'network_selector' => $selector, 'vlan_id' => null,
|
|
|
|
|
|
'routing_table' => null, 'state' => 'active', 'priority' => 0,
|
|
|
|
|
|
'config_revision' => 1, 'verified_agent_revision' => 1,
|
|
|
|
|
|
'verification_state' => 'verified', 'last_verified_at' => $now,
|
|
|
|
|
|
'verification_error_code' => '',
|
|
|
|
|
|
'desired_revision' => 1, 'observed_revision' => 1,
|
|
|
|
|
|
'sync_state' => 'applied', 'last_error_code' => '',
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$attachmentBId = Capsule::table(
|
|
|
|
|
|
'cloud_host_network_attachments'
|
|
|
|
|
|
)->insertGetId([
|
|
|
|
|
|
'attachment_key' => str_repeat('c', 32),
|
|
|
|
|
|
'host_id' => $hostId, 'line_id' => $lineId,
|
|
|
|
|
|
'name' => 'Secondary', 'network_mode' => 'selector',
|
|
|
|
|
|
'network_selector' => 'bridge:br1', 'vlan_id' => null,
|
|
|
|
|
|
'routing_table' => null, 'state' => 'active', 'priority' => 10,
|
|
|
|
|
|
'config_revision' => 1, 'verified_agent_revision' => 1,
|
|
|
|
|
|
'verification_state' => 'verified', 'last_verified_at' => $now,
|
|
|
|
|
|
'verification_error_code' => '',
|
|
|
|
|
|
'desired_revision' => 1, 'observed_revision' => 1,
|
|
|
|
|
|
'sync_state' => 'applied', 'last_error_code' => '',
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$noMembershipAttachmentId = Capsule::table(
|
|
|
|
|
|
'cloud_host_network_attachments'
|
|
|
|
|
|
)->insertGetId([
|
|
|
|
|
|
'attachment_key' => str_repeat('d', 32),
|
|
|
|
|
|
'host_id' => $hostId, 'line_id' => $otherLineId,
|
|
|
|
|
|
'name' => 'No membership', 'network_mode' => 'selector',
|
|
|
|
|
|
'network_selector' => 'bridge:br2', 'vlan_id' => null,
|
|
|
|
|
|
'routing_table' => null, 'state' => 'active', 'priority' => 20,
|
|
|
|
|
|
'config_revision' => 1, 'verified_agent_revision' => 1,
|
|
|
|
|
|
'verification_state' => 'verified', 'last_verified_at' => $now,
|
|
|
|
|
|
'verification_error_code' => '',
|
|
|
|
|
|
'desired_revision' => 1, 'observed_revision' => 1,
|
|
|
|
|
|
'sync_state' => 'applied', 'last_error_code' => '',
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$foreignAttachmentId = Capsule::table(
|
|
|
|
|
|
'cloud_host_network_attachments'
|
|
|
|
|
|
)->insertGetId([
|
|
|
|
|
|
'attachment_key' => str_repeat('e', 32),
|
|
|
|
|
|
'host_id' => $foreignHostId, 'line_id' => $lineId,
|
|
|
|
|
|
'name' => 'Foreign', 'network_mode' => 'selector',
|
|
|
|
|
|
'network_selector' => 'bridge:foreign', 'vlan_id' => null,
|
|
|
|
|
|
'routing_table' => null, 'state' => 'active', 'priority' => 0,
|
|
|
|
|
|
'config_revision' => 1, 'verified_agent_revision' => 1,
|
|
|
|
|
|
'verification_state' => 'verified', 'last_verified_at' => $now,
|
|
|
|
|
|
'verification_error_code' => '',
|
|
|
|
|
|
'desired_revision' => 1, 'observed_revision' => 1,
|
|
|
|
|
|
'sync_state' => 'applied', 'last_error_code' => '',
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$poolKey = str_repeat('a', 32);
|
|
|
|
|
|
$poolId = Capsule::table('cloud_public_ip_pools')->insertGetId([
|
|
|
|
|
|
'pool_key' => $poolKey, 'host_id' => $hostId,
|
|
|
|
|
|
'network_attachment_id' => $attachmentId, 'name' => 'Public pool',
|
|
|
|
|
|
'cidr' => '8.8.8.0/29', 'gateway' => '8.8.8.1',
|
|
|
|
|
|
'guest_prefix_length' => 29, 'dns_servers_json' => '["1.1.1.1"]',
|
|
|
|
|
|
'reserved_ips_json' => '[]', 'state' => 'active', 'priority' => 0,
|
|
|
|
|
|
'desired_revision' => 1, 'observed_revision' => 1,
|
|
|
|
|
|
'sync_state' => 'applied', 'last_error_code' => '',
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
Capsule::table('cloud_public_ips')->insert([
|
|
|
|
|
|
[
|
|
|
|
|
|
'host_id' => $hostId, 'public_ip_pool_id' => $poolId,
|
|
|
|
|
|
'address' => '8.8.8.2', 'prefix_length' => 29,
|
|
|
|
|
|
'state' => 'assigned', 'reservation_id' => 41,
|
|
|
|
|
|
'instance_id' => 51, 'operation_id' => str_repeat('b', 32),
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'host_id' => $hostId, 'public_ip_pool_id' => $poolId,
|
|
|
|
|
|
'address' => '8.8.8.3', 'prefix_length' => 29,
|
|
|
|
|
|
'state' => 'available', 'reservation_id' => null,
|
|
|
|
|
|
'instance_id' => null, 'operation_id' => null,
|
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
|
],
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$actor = [
|
|
|
|
|
|
'user_id' => 1, 'is_admin' => true, 'is_super_admin' => true,
|
|
|
|
|
|
'can_manage' => true, 'session_id' => 'network-pool-test',
|
|
|
|
|
|
];
|
|
|
|
|
|
$body = static fn (
|
|
|
|
|
|
int $revision,
|
|
|
|
|
|
array $reserved,
|
|
|
|
|
|
string $dns = '1.1.1.1',
|
|
|
|
|
|
?int $targetAttachmentId = null,
|
|
|
|
|
|
string $state = 'active'
|
|
|
|
|
|
) => [
|
|
|
|
|
|
'network_attachment_id' => $targetAttachmentId ?? $attachmentId,
|
|
|
|
|
|
'name' => 'Public pool', 'cidr' => '8.8.8.0/29',
|
|
|
|
|
|
'gateway' => '8.8.8.1', 'dns_servers' => [$dns],
|
|
|
|
|
|
'reserved_ips' => $reserved, 'guest_prefix_length' => 29,
|
|
|
|
|
|
'state' => $state, 'expected_revision' => $revision,
|
|
|
|
|
|
];
|
|
|
|
|
|
$service = new CloudAdminHostNetworkService();
|
|
|
|
|
|
|
|
|
|
|
|
$saved = $service->update(
|
|
|
|
|
|
$hostId, $poolKey, $actor, $body(1, ['8.8.8.3'])
|
|
|
|
|
|
);
|
|
|
|
|
|
$pool = CloudPublicIpPool::find($poolId);
|
|
|
|
|
|
$assigned = CloudPublicIp::where('address', '8.8.8.2')->first();
|
|
|
|
|
|
$disabled = CloudPublicIp::where('address', '8.8.8.3')->first();
|
|
|
|
|
|
$assert(($saved['saved_pool_key'] ?? null) === $poolKey
|
|
|
|
|
|
&& !array_key_exists('default_network', $saved['host'])
|
|
|
|
|
|
&& count($saved['attachments']) === 3
|
|
|
|
|
|
&& (int) $saved['items'][0]['network_attachment_id'] === $attachmentId
|
|
|
|
|
|
&& (int) $saved['items'][0]['attachment']['id'] === $attachmentId
|
|
|
|
|
|
&& (int) $saved['items'][0]['line']['id'] === $lineId
|
|
|
|
|
|
&& (int) $pool->desired_revision === 2
|
|
|
|
|
|
&& json_decode((string) $pool->reserved_ips_json, true) === ['8.8.8.3']
|
|
|
|
|
|
&& (string) $assigned->state === 'assigned'
|
|
|
|
|
|
&& (int) $assigned->instance_id === 51
|
|
|
|
|
|
&& (string) $disabled->state === 'disabled',
|
|
|
|
|
|
'An unoccupied IP was not disabled without disturbing the assigned IP.');
|
|
|
|
|
|
Capsule::table('cloud_host_resource_pool_memberships')
|
|
|
|
|
|
->where('host_id', $hostId)->update(['scheduler_state' => 'disabled']);
|
|
|
|
|
|
$withoutMembership = $service->read($hostId, $actor);
|
|
|
|
|
|
$assert($withoutMembership['attachments'][0]['ready'] === false,
|
|
|
|
|
|
'Attachment remained ready without an active line membership.');
|
|
|
|
|
|
Capsule::table('cloud_host_resource_pool_memberships')
|
|
|
|
|
|
->where('host_id', $hostId)->update(['scheduler_state' => 'active']);
|
|
|
|
|
|
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId, $poolKey, $actor, $body(2, ['8.8.8.2', '8.8.8.3'])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'已分配或任务占用的公网 IP 不能加入保留 / 禁用列表:8.8.8.2'
|
|
|
|
|
|
);
|
|
|
|
|
|
$pool->refresh();
|
|
|
|
|
|
$assigned->refresh();
|
|
|
|
|
|
$assert((int) $pool->desired_revision === 2
|
|
|
|
|
|
&& json_decode((string) $pool->reserved_ips_json, true) === ['8.8.8.3']
|
|
|
|
|
|
&& (string) $assigned->state === 'assigned',
|
|
|
|
|
|
'Rejecting an occupied disabled IP changed the pool or assigned ledger row.');
|
|
|
|
|
|
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId, $poolKey, $actor, $body(2, ['8.8.8.3'], '9.9.9.9')
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'只能修改名称、运行状态和未占用的保留 / 禁用 IP'
|
|
|
|
|
|
);
|
|
|
|
|
|
$pool->refresh();
|
|
|
|
|
|
$assert((int) $pool->desired_revision === 2
|
|
|
|
|
|
&& (string) $pool->dns_servers_json === '["1.1.1.1"]',
|
|
|
|
|
|
'An occupied pool accepted a DNS identity change.');
|
|
|
|
|
|
|
|
|
|
|
|
$service->update($hostId, $poolKey, $actor, $body(2, []));
|
|
|
|
|
|
$pool->refresh();
|
|
|
|
|
|
$disabled->refresh();
|
|
|
|
|
|
$assert((int) $pool->desired_revision === 3
|
|
|
|
|
|
&& json_decode((string) $pool->reserved_ips_json, true) === []
|
|
|
|
|
|
&& (string) $disabled->state === 'available',
|
|
|
|
|
|
'Removing an unoccupied disabled IP did not restore its availability.');
|
|
|
|
|
|
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$poolKey,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
$body(3, [], '1.1.1.1', $attachmentBId)
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'只能修改名称、运行状态和未占用的保留 / 禁用 IP'
|
|
|
|
|
|
);
|
|
|
|
|
|
$pool->refresh();
|
|
|
|
|
|
$assert((int) $pool->network_attachment_id === $attachmentId
|
|
|
|
|
|
&& (int) $pool->desired_revision === 3,
|
|
|
|
|
|
'An occupied pool switched to a different attachment.');
|
|
|
|
|
|
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$poolKey,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
$body(2, [], '1.1.1.1', $foreignAttachmentId)
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'版本已变化'
|
|
|
|
|
|
);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$poolKey,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
$body(3, [], '1.1.1.1', $foreignAttachmentId)
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'不存在或不属于该宿主机'
|
|
|
|
|
|
);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->update(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$poolKey,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
$body(3, [], '1.1.1.1', $noMembershipAttachmentId)
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'没有启用中的资源池成员关系'
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$createBody = [
|
|
|
|
|
|
'network_attachment_id' => $attachmentBId,
|
|
|
|
|
|
'name' => 'Second public pool',
|
|
|
|
|
|
'cidr' => '8.8.4.0/29',
|
|
|
|
|
|
'gateway' => '8.8.4.1',
|
|
|
|
|
|
'dns_servers' => ['1.0.0.1'],
|
|
|
|
|
|
'reserved_ips' => [],
|
|
|
|
|
|
'guest_prefix_length' => 29,
|
|
|
|
|
|
'state' => 'active',
|
|
|
|
|
|
'request_key' => str_repeat('f', 32),
|
|
|
|
|
|
];
|
|
|
|
|
|
$created = $service->create($hostId, $actor, $createBody);
|
|
|
|
|
|
$createdPoolKey = (string) $created['saved_pool_key'];
|
|
|
|
|
|
$createdPool = CloudPublicIpPool::where(
|
|
|
|
|
|
'pool_key', $createdPoolKey
|
|
|
|
|
|
)->first();
|
|
|
|
|
|
$replayed = $service->create($hostId, $actor, $createBody);
|
|
|
|
|
|
$assert($createdPool !== null
|
|
|
|
|
|
&& (int) $createdPool->network_attachment_id === $attachmentBId
|
|
|
|
|
|
&& (int) $createdPool->desired_revision === 1
|
|
|
|
|
|
&& (string) $createdPool->sync_state === 'applied'
|
|
|
|
|
|
&& (int) $createdPool->observed_revision === 1
|
|
|
|
|
|
&& (string) $replayed['saved_pool_key'] === $createdPoolKey
|
|
|
|
|
|
&& CloudPublicIpPool::where('pool_key', $createdPoolKey)->count() === 1,
|
|
|
|
|
|
'Create/replay did not preserve one applied attachment-bound pool.');
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->create(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
array_replace($createBody, [
|
|
|
|
|
|
'network_attachment_id' => $attachmentId,
|
|
|
|
|
|
])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'同一创建请求标识不能用于不同的公网地址池配置'
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$switched = $service->update(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$createdPoolKey,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
[
|
|
|
|
|
|
...$createBody,
|
|
|
|
|
|
'network_attachment_id' => $attachmentId,
|
|
|
|
|
|
'expected_revision' => 1,
|
|
|
|
|
|
]
|
|
|
|
|
|
);
|
|
|
|
|
|
$createdPool->refresh();
|
|
|
|
|
|
$assert((int) $createdPool->network_attachment_id === $attachmentId
|
|
|
|
|
|
&& (int) $createdPool->desired_revision === 2
|
|
|
|
|
|
&& (int) $switched['items'][1]['attachment']['id'] === $attachmentId,
|
|
|
|
|
|
'An unoccupied pool could not switch its explicit attachment.');
|
|
|
|
|
|
Capsule::table('cloud_public_ip_pools')->where('id', $createdPool->id)
|
|
|
|
|
|
->update(['network_attachment_id' => 999999]);
|
|
|
|
|
|
$missingRead = $service->read($hostId, $actor);
|
|
|
|
|
|
$missingItem = array_values(array_filter(
|
|
|
|
|
|
$missingRead['items'],
|
|
|
|
|
|
static fn (array $item): bool =>
|
|
|
|
|
|
(string) $item['pool_key'] === $createdPoolKey
|
|
|
|
|
|
))[0] ?? null;
|
|
|
|
|
|
$assert(is_array($missingItem)
|
|
|
|
|
|
&& $missingItem['attachment']['state'] === 'missing'
|
|
|
|
|
|
&& $missingItem['line'] === null,
|
|
|
|
|
|
'A missing attachment made the read model unavailable.');
|
|
|
|
|
|
Capsule::table('cloud_public_ip_pools')->where('id', $createdPool->id)
|
|
|
|
|
|
->update(['network_attachment_id' => $attachmentId]);
|
|
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_host_network_attachments')
|
|
|
|
|
|
->where('id', $attachmentBId)->update(['state' => 'disabled']);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->create(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
array_replace($createBody, [
|
|
|
|
|
|
'request_key' => str_repeat('1', 32),
|
|
|
|
|
|
'cidr' => '8.8.5.0/29',
|
|
|
|
|
|
'gateway' => '8.8.5.1',
|
|
|
|
|
|
])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'网络接入未启用'
|
|
|
|
|
|
);
|
|
|
|
|
|
Capsule::table('cloud_host_network_attachments')
|
|
|
|
|
|
->where('id', $attachmentBId)->update([
|
|
|
|
|
|
'state' => 'active', 'verification_state' => 'stale',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->create(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
array_replace($createBody, [
|
|
|
|
|
|
'request_key' => str_repeat('2', 32),
|
|
|
|
|
|
'cidr' => '8.8.6.0/29',
|
|
|
|
|
|
'gateway' => '8.8.6.1',
|
|
|
|
|
|
])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'没有当前 Agent 版本的有效验证证据'
|
|
|
|
|
|
);
|
|
|
|
|
|
Capsule::table('cloud_host_network_attachments')
|
|
|
|
|
|
->where('id', $attachmentBId)->update([
|
|
|
|
|
|
'verification_state' => 'verified',
|
|
|
|
|
|
]);
|
|
|
|
|
|
Capsule::table('cloud_host_agents')->where('host_id', $hostId)->update([
|
|
|
|
|
|
'revision' => 2, 'tested_revision' => 2,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->create(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
array_replace($createBody, [
|
|
|
|
|
|
'request_key' => str_repeat('3', 32),
|
|
|
|
|
|
'cidr' => '8.8.7.0/29',
|
|
|
|
|
|
'gateway' => '8.8.7.1',
|
|
|
|
|
|
])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'没有当前 Agent 版本的有效验证证据'
|
|
|
|
|
|
);
|
|
|
|
|
|
Capsule::table('cloud_host_network_attachments')
|
|
|
|
|
|
->where('id', $attachmentBId)->update([
|
|
|
|
|
|
'verified_agent_revision' => 2,
|
|
|
|
|
|
]);
|
|
|
|
|
|
Capsule::table('cloud_host_agents')->where('host_id', $hostId)->update([
|
|
|
|
|
|
'network_selectors_json' => json_encode(
|
|
|
|
|
|
['bridge:br0'], JSON_THROW_ON_ERROR
|
|
|
|
|
|
),
|
|
|
|
|
|
]);
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
|
static fn () => $service->create(
|
|
|
|
|
|
$hostId,
|
|
|
|
|
|
$actor,
|
|
|
|
|
|
array_replace($createBody, [
|
|
|
|
|
|
'request_key' => str_repeat('4', 32),
|
|
|
|
|
|
'cidr' => '8.8.9.0/29',
|
|
|
|
|
|
'gateway' => '8.8.9.1',
|
|
|
|
|
|
])
|
|
|
|
|
|
),
|
|
|
|
|
|
409,
|
|
|
|
|
|
'没有当前 Agent 版本的有效验证证据'
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_public_ip_pool_replacement_batches')->insert([
|
|
|
|
|
|
'batch_key' => str_repeat('9', 32),
|
|
|
|
|
|
'host_id' => $hostId,
|
|
|
|
|
|
'source_public_ip_pool_id' => $poolId,
|
|
|
|
|
|
'target_public_ip_pool_id' => (int) $createdPool->id,
|
|
|
|
|
|
'source_initial_state' => 'active',
|
|
|
|
|
|
'parent_job_id' => 9001,
|
|
|
|
|
|
'requested_by_user_id' => 1,
|
|
|
|
|
|
'state' => 'succeeded',
|
|
|
|
|
|
'active_claim' => null,
|
|
|
|
|
|
'last_error_code' => '',
|
|
|
|
|
|
'terminal_at' => $now,
|
|
|
|
|
|
'created_at' => $now,
|
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$historyRead = $service->read($hostId, $actor);
|
|
|
|
|
|
$historyItem = array_values(array_filter(
|
|
|
|
|
|
$historyRead['items'],
|
|
|
|
|
|
static fn (array $item): bool =>
|
|
|
|
|
|
(string) $item['pool_key'] === $poolKey
|
|
|
|
|
|
))[0] ?? null;
|
|
|
|
|
|
$assert(is_array($historyItem)
|
|
|
|
|
|
&& $historyItem['replacement_history_count'] === 1
|
|
|
|
|
|
&& $historyItem['has_replacement_history'] === true,
|
|
|
|
|
|
'Terminal replacement history was not exposed on the pool DTO.');
|
|
|
|
|
|
|
|
|
|
|
|
echo "Public pools bind explicit, verified host attachments safely.\n";
|