[ 'user_id' => 7, 'is_admin' => false, 'can_manage' => false, 'request_idempotency_key' => $key, ]; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_regions', static function (Blueprint $table): void { $table->increments('id'); $table->string('code'); $table->string('name'); $table->string('country_code', 2); $table->string('status'); $table->integer('sort'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_network_lines', static function (Blueprint $table): void { $table->increments('id'); $table->integer('region_id'); $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_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->boolean('enabled'); }); $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'); }); $schema->create('cloud_host_vpc_transports', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->string('sync_state'); $table->integer('desired_revision'); $table->integer('observed_revision'); }); $schema->create('cloud_vpc_cidr_pools', static function (Blueprint $table): void { $table->increments('id'); $table->string('pool_key', 32); $table->integer('region_id'); $table->string('name'); $table->string('cidr', 18); $table->integer('prefix_length'); $table->integer('allocation_prefix'); $table->string('state'); $table->integer('revision'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_vpcs', static function (Blueprint $table): void { $table->increments('id'); $table->string('vpc_key', 32)->unique(); $table->integer('user_id'); $table->integer('region_id'); $table->integer('cidr_pool_id'); $table->string('name'); $table->string('cidr', 18); $table->integer('prefix_length'); $table->string('gateway', 15); $table->integer('active_claim')->nullable(); $table->string('state'); $table->integer('desired_revision'); $table->integer('deleted_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['region_id', 'cidr', 'active_claim']); }); $schema->create('cloud_vpc_ports', static function (Blueprint $table): void { $table->increments('id'); $table->integer('vpc_id'); $table->integer('active_claim')->nullable(); }); $schema->create('cloud_vpc_host_states', static function (Blueprint $table): void { $table->increments('id'); $table->integer('vpc_id'); }); $schema->create('cloud_vpc_membership_batches', static function (Blueprint $table): void { $table->increments('id'); $table->string('batch_key'); $table->integer('vpc_id'); $table->integer('user_id'); $table->integer('parent_job_id'); $table->string('kind'); $table->string('state'); $table->integer('desired_revision'); $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_vpc_membership_batch_items', static function (Blueprint $table): void { $table->increments('id'); $table->integer('batch_id'); $table->integer('user_id'); $table->integer('instance_id'); $table->integer('vpc_port_id'); $table->integer('host_id'); $table->integer('child_job_id')->nullable(); $table->string('state'); $table->string('last_error_code')->default(''); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('uuid'); $table->integer('host_id'); $table->integer('user_id'); $table->integer('resource_pool_id')->nullable(); $table->string('name'); $table->string('status'); $table->string('service_status'); $table->string('network_status'); $table->integer('expires_at')->nullable(); $table->text('spec_json'); }); Capsule::table('cloud_regions')->insert([ 'id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong', 'country_code' => 'HK', 'status' => 'active', 'sort' => 0, 'created_at' => 1, 'updated_at' => 1, ]); Capsule::table('cloud_network_lines')->insert([ 'id' => 1, 'region_id' => 1, 'status' => 'active', ]); Capsule::table('cloud_resource_pools')->insert([ 'id' => 1, 'line_id' => 1, 'status' => 'active', ]); Capsule::table('cloud_hosts')->insert(['id' => 1, 'enabled' => 1]); Capsule::table('cloud_host_resource_pool_memberships')->insert([ 'id' => 1, 'host_id' => 1, 'resource_pool_id' => 1, 'scheduler_state' => 'active', ]); Capsule::table('cloud_host_vpc_transports')->insert([ 'host_id' => 1, 'sync_state' => 'ready', 'desired_revision' => 1, 'observed_revision' => 1, ]); Capsule::table('cloud_vpc_cidr_pools')->insert([ 'id' => 1, 'pool_key' => str_repeat('a', 32), 'region_id' => 1, 'name' => 'HKG', 'cidr' => '10.64.0.0/16', 'prefix_length' => 16, 'allocation_prefix' => 24, 'state' => 'active', 'revision' => 1, 'created_at' => 1, 'updated_at' => 1, ]); $first = CloudVpcService::create($actor('vpc-create-request-0001'), [ 'name' => 'production', 'region_id' => 1, ]); $assert($first['vpc']['cidr'] === '10.64.0.0/24' && $first['vpc']['gateway'] === '10.64.0.1' && $first['vpc']['state'] === 'active', 'First VPC did not receive the first canonical subnet and gateway.'); $replay = CloudVpcService::create($actor('vpc-create-request-0001'), [ 'name' => 'production', 'region_id' => 1, ]); $assert($replay['vpc']['id'] === $first['vpc']['id'] && Capsule::table('cloud_vpcs')->count() === 1, 'VPC create idempotency replay allocated a second subnet.'); try { CloudVpcService::create($actor('vpc-create-request-0001'), [ 'name' => 'different', 'region_id' => 1, ]); throw new RuntimeException('Reused VPC idempotency key changed intent.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'Changed VPC idempotency intent did not conflict.'); } $second = CloudVpcService::create($actor('vpc-create-request-0002'), [ 'name' => 'staging', 'region_id' => 1, ]); $assert($second['vpc']['cidr'] === '10.64.1.0/24', 'VPC allocator did not select the first free gap.'); Capsule::table('cloud_vpc_ports')->insert([ 'id' => 1, 'vpc_id' => $first['vpc']['id'], 'active_claim' => 1, ]); try { CloudVpcService::delete(['user_id' => 7], $first['vpc']['vpc_key']); throw new RuntimeException('VPC with an active port was deleted.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'Active VPC port did not protect deletion.'); } Capsule::table('cloud_vpc_ports')->where('id', 1)->delete(); $deleted = CloudVpcService::delete( ['user_id' => 7], $first['vpc']['vpc_key'] ); $assert($deleted['vpc']['state'] === 'deleted' && Capsule::table('cloud_vpcs')->where('id', $first['vpc']['id']) ->value('active_claim') === null, 'VPC deletion did not release its active CIDR claim.'); $third = CloudVpcService::create($actor('vpc-create-request-0003'), [ 'name' => 'replacement', 'region_id' => 1, ]); $assert($third['vpc']['cidr'] === '10.64.0.0/24', 'Released VPC CIDR was not safely reusable.'); $library = CloudVpcService::library(['user_id' => 7]); $assert($library['used'] === 2 && count($library['items']) === 2 && $library['instances'] === [] && count($library['regions']) === 1 && $library['regions'][0]['available'] === 254, 'VPC library or regional availability projection is incorrect.'); echo "User VPC lifecycle and IPAM service passed.\n";