From 54dcecd516e305974782176ebc71e272c93d00b7 Mon Sep 17 00:00:00 2001 From: codex Date: Wed, 28 Oct 2026 18:40:40 +0800 Subject: [PATCH] feat(vpc): add control-plane schema foundation --- web/app/Command/CloudSetup.php | 88 +++++++++- web/app/Models/CloudHostVpcTransport.php | 22 +++ web/app/Models/CloudJob.php | 2 + web/app/Models/CloudVpc.php | 21 +++ web/app/Models/CloudVpcCidrPool.php | 18 +++ web/app/Models/CloudVpcHostState.php | 21 +++ web/app/Models/CloudVpcPort.php | 23 +++ web/app/Services/Cloud/CloudVpcFeature.php | 47 ++++++ web/sql/cloud.sql | 177 +++++++++++++++++++++ web/tests/CloudFreshSchemaContractTest.php | 6 +- web/tests/CloudVpcSchemaContractTest.php | 114 +++++++++++++ 11 files changed, 535 insertions(+), 4 deletions(-) create mode 100644 web/app/Models/CloudHostVpcTransport.php create mode 100644 web/app/Models/CloudVpc.php create mode 100644 web/app/Models/CloudVpcCidrPool.php create mode 100644 web/app/Models/CloudVpcHostState.php create mode 100644 web/app/Models/CloudVpcPort.php create mode 100644 web/app/Services/Cloud/CloudVpcFeature.php create mode 100644 web/tests/CloudVpcSchemaContractTest.php diff --git a/web/app/Command/CloudSetup.php b/web/app/Command/CloudSetup.php index 83f13ede..730c1fa1 100644 --- a/web/app/Command/CloudSetup.php +++ b/web/app/Command/CloudSetup.php @@ -8,7 +8,7 @@ use App\Services\Config; /** Initializes only the clean-break PHP business and Agent log schemas. */ final class CloudSetup { - private const SCHEMA_VERSION = 'go-agent-wss-v12-object-cleanup-v1'; + private const SCHEMA_VERSION = 'go-agent-wss-v13-vpc-l3-v1'; private const MAIN_TABLES = [ 'cloud_regions', 'cloud_network_lines', 'cloud_resource_pools', 'cloud_hosts', 'cloud_host_agents', @@ -18,8 +18,10 @@ final class CloudSetup 'cloud_rescue_template_imports', 'cloud_upload_staging', 'cloud_host_resource_pool_memberships', 'cloud_host_network_attachments', 'cloud_public_ip_pools', + 'cloud_host_vpc_transports', 'cloud_vpc_cidr_pools', 'cloud_host_capacity_policies', 'cloud_public_ips', 'cloud_offers', - 'cloud_instances', 'cloud_instance_firewalls', + 'cloud_instances', 'cloud_vpcs', 'cloud_vpc_ports', + 'cloud_vpc_host_states', 'cloud_instance_firewalls', 'cloud_instance_firewall_versions', 'cloud_instance_firewall_rules', 'cloud_platform_test_policy_state', 'cloud_platform_test_policy_rules', 'cloud_instance_snapshots', @@ -52,6 +54,7 @@ final class CloudSetup 'id', 'operation_id', 'request_idempotency_key', 'request_fingerprint', 'parent_job_id', 'user_id', 'host_id', 'backup_id', 'backup_restore_id', 'backup_recovery_id', + 'vpc_id', 'vpc_port_id', 'kind', 'execution_mode', 'phase', 'outcome', 'status', 'attempts', 'dispatch_attempts', 'dispatch_deadline_at', 'dispatch_started_at', 'agent_accepted_at', @@ -62,6 +65,36 @@ final class CloudSetup 'lease_token', 'lease_until', 'available_at', 'error_code', 'result_json', 'created_at', 'updated_at', ], + 'cloud_host_vpc_transports' => [ + 'host_id', 'transport_address', 'transport_prefix_length', + 'transport_interface', 'transport_mode', 'mtu', + 'desired_revision', 'observed_revision', 'sync_state', + 'last_operation_id', 'last_error_code', 'last_verified_at', + 'created_at', 'updated_at', + ], + 'cloud_vpc_cidr_pools' => [ + 'id', 'pool_key', 'region_id', 'name', 'cidr', + 'prefix_length', 'allocation_prefix', 'state', + 'created_at', 'updated_at', + ], + 'cloud_vpcs' => [ + 'id', 'vpc_key', 'user_id', 'region_id', 'cidr_pool_id', + 'name', 'cidr', 'prefix_length', 'gateway', 'state', + 'desired_revision', 'deleted_at', 'created_at', 'updated_at', + ], + 'cloud_vpc_ports' => [ + 'id', 'port_key', 'vpc_id', 'instance_id', 'user_id', + 'private_ip', 'mac_address', 'active_claim', 'state', + 'desired_revision', 'applied_revision', 'last_operation_id', + 'last_error_code', 'attached_at', 'released_at', + 'created_at', 'updated_at', + ], + 'cloud_vpc_host_states' => [ + 'id', 'vpc_id', 'host_id', 'desired_revision', + 'applied_revision', 'desired_digest', 'applied_digest', + 'sync_state', 'last_operation_id', 'last_error_code', + 'last_success_at', 'last_failure_at', 'created_at', 'updated_at', + ], 'cloud_agent_completion_events' => [ 'event_id', 'host_id', 'operation_id', 'payload_sha256', 'outcome', 'effect_may_have_applied', 'safe_to_resubmit', @@ -171,6 +204,29 @@ final class CloudSetup ], ]; private const REQUIRED_CHECKS = [ + 'cloud_host_vpc_transports' => [ + 'chk_cloud_host_vpc_transport_prefix', + 'chk_cloud_host_vpc_transport_mode', + 'chk_cloud_host_vpc_transport_mtu', + 'chk_cloud_host_vpc_transport_state', + 'chk_cloud_host_vpc_transport_revision', + ], + 'cloud_vpc_cidr_pools' => [ + 'chk_cloud_vpc_cidr_pool_prefix', + 'chk_cloud_vpc_cidr_pool_state', + ], + 'cloud_vpcs' => [ + 'chk_cloud_vpc_prefix', 'chk_cloud_vpc_state', + 'chk_cloud_vpc_deleted_at', + ], + 'cloud_vpc_ports' => [ + 'chk_cloud_vpc_port_active_claim', + 'chk_cloud_vpc_port_state', 'chk_cloud_vpc_port_release', + 'chk_cloud_vpc_port_revision', + ], + 'cloud_vpc_host_states' => [ + 'chk_cloud_vpc_host_state', 'chk_cloud_vpc_host_revision', + ], 'cloud_backup_subscriptions' => [ 'chk_cloud_backup_subscription_frequency', 'chk_cloud_backup_subscription_weekday', @@ -203,12 +259,36 @@ final class CloudSetup ], ]; private const REQUIRED_INDEXES = [ + 'cloud_host_vpc_transports' => [ + 'PRIMARY', 'cloud_host_vpc_transport_operation', + 'cloud_host_vpc_transport_state', + ], + 'cloud_vpc_cidr_pools' => [ + 'PRIMARY', 'cloud_vpc_cidr_pool_key', + 'cloud_vpc_cidr_pool_region', 'cloud_vpc_cidr_pool_catalog', + ], + 'cloud_vpcs' => [ + 'PRIMARY', 'cloud_vpc_key', 'cloud_vpc_region_cidr', + 'cloud_vpc_owner', 'cloud_vpc_region_state', + 'cloud_vpc_pool_state', + ], + 'cloud_vpc_ports' => [ + 'PRIMARY', 'cloud_vpc_port_key', 'cloud_vpc_port_ip_claim', + 'cloud_vpc_port_instance_claim', 'cloud_vpc_port_mac', + 'cloud_vpc_port_operation', 'cloud_vpc_port_owner', + 'cloud_vpc_port_vpc_state', + ], + 'cloud_vpc_host_states' => [ + 'PRIMARY', 'cloud_vpc_host_identity', + 'cloud_vpc_host_operation', 'cloud_vpc_host_sync', + ], 'cloud_jobs' => [ 'PRIMARY', 'uniq_cloud_operation', 'uniq_cloud_job_request', 'uniq_cloud_job_completion_event', 'uniq_cloud_provision_order', 'cloud_job_poll', 'cloud_job_receipt_deadline', 'cloud_job_backup', 'cloud_job_backup_restore', 'cloud_job_backup_recovery', + 'cloud_job_vpc', 'cloud_job_vpc_port', ], 'cloud_agent_completion_events' => [ 'PRIMARY', 'uniq_cloud_completion_operation', @@ -395,6 +475,10 @@ final class CloudSetup 'name' => 'cloud_network_lines_enabled', 'value' => 'false', ]); + $connection->table('configurations')->insertOrIgnore([ + 'name' => 'cloud_vpc_enabled', + 'value' => 'false', + ]); } /** Refuse to mix the Go/Agent schema with any unmarked cloud database. */ diff --git a/web/app/Models/CloudHostVpcTransport.php b/web/app/Models/CloudHostVpcTransport.php new file mode 100644 index 00000000..7632d92a --- /dev/null +++ b/web/app/Models/CloudHostVpcTransport.php @@ -0,0 +1,22 @@ + 'int', + 'transport_prefix_length' => 'int', + 'mtu' => 'int', + 'desired_revision' => 'int', + 'observed_revision' => 'int', + 'last_verified_at' => 'int', + 'created_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Models/CloudJob.php b/web/app/Models/CloudJob.php index 927bce14..e8e6e2be 100644 --- a/web/app/Models/CloudJob.php +++ b/web/app/Models/CloudJob.php @@ -11,6 +11,8 @@ final class CloudJob extends Model 'backup_id' => 'int', 'backup_restore_id' => 'int', 'backup_recovery_id' => 'int', + 'vpc_id' => 'int', + 'vpc_port_id' => 'int', ]; /** diff --git a/web/app/Models/CloudVpc.php b/web/app/Models/CloudVpc.php new file mode 100644 index 00000000..90659446 --- /dev/null +++ b/web/app/Models/CloudVpc.php @@ -0,0 +1,21 @@ + 'int', + 'user_id' => 'int', + 'region_id' => 'int', + 'cidr_pool_id' => 'int', + 'prefix_length' => 'int', + 'desired_revision' => 'int', + 'deleted_at' => 'int', + 'created_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Models/CloudVpcCidrPool.php b/web/app/Models/CloudVpcCidrPool.php new file mode 100644 index 00000000..7a90b544 --- /dev/null +++ b/web/app/Models/CloudVpcCidrPool.php @@ -0,0 +1,18 @@ + 'int', + 'region_id' => 'int', + 'prefix_length' => 'int', + 'allocation_prefix' => 'int', + 'created_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Models/CloudVpcHostState.php b/web/app/Models/CloudVpcHostState.php new file mode 100644 index 00000000..a3526999 --- /dev/null +++ b/web/app/Models/CloudVpcHostState.php @@ -0,0 +1,21 @@ + 'int', + 'vpc_id' => 'int', + 'host_id' => 'int', + 'desired_revision' => 'int', + 'applied_revision' => 'int', + 'last_success_at' => 'int', + 'last_failure_at' => 'int', + 'created_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Models/CloudVpcPort.php b/web/app/Models/CloudVpcPort.php new file mode 100644 index 00000000..f4e16dc8 --- /dev/null +++ b/web/app/Models/CloudVpcPort.php @@ -0,0 +1,23 @@ + 'int', + 'vpc_id' => 'int', + 'instance_id' => 'int', + 'user_id' => 'int', + 'active_claim' => 'bool', + 'desired_revision' => 'int', + 'applied_revision' => 'int', + 'attached_at' => 'int', + 'released_at' => 'int', + 'created_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Services/Cloud/CloudVpcFeature.php b/web/app/Services/Cloud/CloudVpcFeature.php new file mode 100644 index 00000000..e3444a0f --- /dev/null +++ b/web/app/Services/Cloud/CloudVpcFeature.php @@ -0,0 +1,47 @@ +value('value'); + } catch (\Throwable $error) { + error_log('Unable to read the VPC feature switch: ' + . get_class($error)); + return self::$cached = false; + } + + return self::$cached = self::valueIsEnabled($value); + } + + public static function save(bool $enabled): void + { + $row = Configurations::where('name', self::CONFIG_KEY)->first(); + if ($row === null) { + $row = new Configurations(); + $row->name = self::CONFIG_KEY; + } + $row->value = $enabled ? 'true' : 'false'; + $row->save(); + self::$cached = $enabled; + } + + private static function valueIsEnabled($value): bool + { + return in_array(strtolower(trim((string) $value)), + ['1', 'true', 'yes', 'on'], true); + } +} diff --git a/web/sql/cloud.sql b/web/sql/cloud.sql index 6f7ab813..d520c698 100644 --- a/web/sql/cloud.sql +++ b/web/sql/cloud.sql @@ -147,6 +147,70 @@ CREATE TABLE cloud_host_network_attachments ( KEY cloud_network_attachment_sync (sync_state, updated_at, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +-- A host's existing private underlay remains operator-owned. The panel stores +-- only the selected address/interface and desired generation; the Agent +-- verifies the binding and applies tenant VPC routes without configuring the +-- physical address, bridge or upstream gateway itself. +CREATE TABLE cloud_host_vpc_transports ( + host_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, + transport_address VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + transport_prefix_length TINYINT UNSIGNED NOT NULL, + transport_interface VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + transport_mode VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'direct_l3', + mtu SMALLINT UNSIGNED NOT NULL DEFAULT 1500, + desired_revision BIGINT UNSIGNED NOT NULL DEFAULT 1, + observed_revision BIGINT UNSIGNED NOT NULL DEFAULT 0, + sync_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'never', + last_operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + last_verified_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_host_vpc_transport_operation (last_operation_id), + KEY cloud_host_vpc_transport_state (sync_state, updated_at, host_id), + CONSTRAINT chk_cloud_host_vpc_transport_prefix CHECK ( + transport_prefix_length BETWEEN 1 AND 32 + ), + CONSTRAINT chk_cloud_host_vpc_transport_mode CHECK ( + transport_mode = 'direct_l3' + ), + CONSTRAINT chk_cloud_host_vpc_transport_mtu CHECK ( + mtu BETWEEN 576 AND 9216 + ), + CONSTRAINT chk_cloud_host_vpc_transport_state CHECK ( + sync_state IN ('never','pending','applying','ready','failed','unknown') + ), + CONSTRAINT chk_cloud_host_vpc_transport_revision CHECK ( + observed_revision <= desired_revision + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Region-scoped IPv4 supernets are the only source of tenant VPC CIDRs. The +-- application locks one pool row while selecting a free child prefix, so +-- concurrent VPC creation cannot allocate overlapping networks. +CREATE TABLE cloud_vpc_cidr_pools ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + pool_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + region_id BIGINT UNSIGNED NOT NULL, + name VARCHAR(80) NOT NULL, + cidr VARCHAR(18) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + prefix_length TINYINT UNSIGNED NOT NULL, + allocation_prefix TINYINT UNSIGNED NOT NULL DEFAULT 24, + state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'active', + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_vpc_cidr_pool_key (pool_key), + UNIQUE KEY cloud_vpc_cidr_pool_region (region_id, cidr), + KEY cloud_vpc_cidr_pool_catalog (region_id, state, id), + CONSTRAINT chk_cloud_vpc_cidr_pool_prefix CHECK ( + prefix_length BETWEEN 8 AND 28 + AND allocation_prefix BETWEEN prefix_length AND 28 + ), + CONSTRAINT chk_cloud_vpc_cidr_pool_state CHECK ( + state IN ('active','disabled') + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + -- A public pool is an allocation boundary, not a compute resource pool. The -- child cloud_public_ips rows remain the authoritative concrete-address -- ledger used by payment-time placement and reconciliation. @@ -406,6 +470,115 @@ CREATE TABLE cloud_instances ( KEY cloud_instance_placement (resource_pool_id, host_id, status), KEY cloud_instance_lifecycle (service_status, expires_at, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- One user-owned L3 VPC belongs to exactly one commercial region. V1 assigns +-- non-overlapping IPv4 prefixes from a region pool and exposes no cross-region +-- peering, NAT or L2 broadcast contract. +CREATE TABLE cloud_vpcs ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + vpc_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + user_id BIGINT UNSIGNED NOT NULL, + region_id BIGINT UNSIGNED NOT NULL, + cidr_pool_id BIGINT UNSIGNED NOT NULL, + name VARCHAR(80) NOT NULL, + cidr VARCHAR(18) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + prefix_length TINYINT UNSIGNED NOT NULL, + gateway VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'provisioning', + desired_revision BIGINT UNSIGNED NOT NULL DEFAULT 1, + deleted_at BIGINT UNSIGNED DEFAULT NULL, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_vpc_key (vpc_key), + UNIQUE KEY cloud_vpc_region_cidr (region_id, cidr), + KEY cloud_vpc_owner (user_id, state, id), + KEY cloud_vpc_region_state (region_id, state, id), + KEY cloud_vpc_pool_state (cidr_pool_id, state, id), + CONSTRAINT chk_cloud_vpc_prefix CHECK ( + prefix_length BETWEEN 16 AND 28 + ), + CONSTRAINT chk_cloud_vpc_state CHECK ( + state IN ('provisioning','active','degraded','deleting','deleted') + ), + CONSTRAINT chk_cloud_vpc_deleted_at CHECK ( + (state = 'deleted' AND deleted_at IS NOT NULL) + OR (state <> 'deleted' AND deleted_at IS NULL) + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- VPC ports retain an immutable allocation audit. active_claim is set to NULL +-- only after every affected Agent has confirmed removal; MySQL then permits a +-- later port to reuse the released instance/IP while keeping the old row. +CREATE TABLE cloud_vpc_ports ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + port_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + vpc_id BIGINT UNSIGNED NOT NULL, + instance_id BIGINT UNSIGNED NOT NULL, + user_id BIGINT UNSIGNED NOT NULL, + private_ip VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + mac_address CHAR(17) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + active_claim TINYINT UNSIGNED DEFAULT 1, + state VARCHAR(24) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'reserved', + desired_revision BIGINT UNSIGNED NOT NULL DEFAULT 1, + applied_revision BIGINT UNSIGNED NOT NULL DEFAULT 0, + last_operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + attached_at BIGINT UNSIGNED DEFAULT NULL, + released_at BIGINT UNSIGNED DEFAULT NULL, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_vpc_port_key (port_key), + UNIQUE KEY cloud_vpc_port_ip_claim (vpc_id, private_ip, active_claim), + UNIQUE KEY cloud_vpc_port_instance_claim (instance_id, active_claim), + UNIQUE KEY cloud_vpc_port_mac (mac_address), + UNIQUE KEY cloud_vpc_port_operation (last_operation_id), + KEY cloud_vpc_port_owner (user_id, state, id), + KEY cloud_vpc_port_vpc_state (vpc_id, state, id), + CONSTRAINT chk_cloud_vpc_port_active_claim CHECK ( + active_claim IS NULL OR active_claim = 1 + ), + CONSTRAINT chk_cloud_vpc_port_state CHECK ( + state IN ('reserved','preparing','attached','detaching', + 'release_pending','released') + ), + CONSTRAINT chk_cloud_vpc_port_release CHECK ( + (state = 'released' AND active_claim IS NULL AND released_at IS NOT NULL) + OR (state <> 'released' AND active_claim IS NOT NULL + AND active_claim = 1 AND released_at IS NULL) + ), + CONSTRAINT chk_cloud_vpc_port_revision CHECK ( + applied_revision <= desired_revision + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Per-host desired/applied generations make one VPC reconciliation observable +-- without turning individual route commands into panel state. Unknown Agent +-- effects retain the original operation ID until an inspection converges. +CREATE TABLE cloud_vpc_host_states ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + vpc_id BIGINT UNSIGNED NOT NULL, + host_id BIGINT UNSIGNED NOT NULL, + desired_revision BIGINT UNSIGNED NOT NULL DEFAULT 1, + applied_revision BIGINT UNSIGNED NOT NULL DEFAULT 0, + desired_digest CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + applied_digest CHAR(64) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + sync_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'pending', + last_operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + last_success_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + last_failure_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_vpc_host_identity (vpc_id, host_id), + UNIQUE KEY cloud_vpc_host_operation (last_operation_id), + KEY cloud_vpc_host_sync (host_id, sync_state, updated_at, vpc_id), + CONSTRAINT chk_cloud_vpc_host_state CHECK ( + sync_state IN ('pending','applying','ready','failed','unknown') + ), + CONSTRAINT chk_cloud_vpc_host_revision CHECK ( + applied_revision <= desired_revision + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Desired/applied host-enforced firewall state. The policy row is the current -- tenant view, immutable versions retain idempotency/audit evidence, and rules -- contain only the typed public DTO (never hypervisor XML or host commands). @@ -1024,6 +1197,8 @@ CREATE TABLE cloud_jobs ( backup_id BIGINT UNSIGNED DEFAULT NULL, backup_restore_id BIGINT UNSIGNED DEFAULT NULL, backup_recovery_id BIGINT UNSIGNED DEFAULT NULL, + vpc_id BIGINT UNSIGNED DEFAULT NULL, + vpc_port_id BIGINT UNSIGNED DEFAULT NULL, user_id INT NOT NULL, host_id BIGINT UNSIGNED DEFAULT NULL, kind VARCHAR(32) NOT NULL, @@ -1139,6 +1314,8 @@ CREATE TABLE cloud_jobs ( KEY cloud_job_backup (backup_id, id), KEY cloud_job_backup_restore (backup_restore_id, id), KEY cloud_job_backup_recovery (backup_recovery_id, id), + KEY cloud_job_vpc (vpc_id, id), + KEY cloud_job_vpc_port (vpc_port_id, id), KEY cloud_job_overview_state (kind, order_id, phase, outcome, terminal_at, created_at), KEY cloud_job_owner (user_id, id), KEY cloud_job_parent (parent_job_id, id) diff --git a/web/tests/CloudFreshSchemaContractTest.php b/web/tests/CloudFreshSchemaContractTest.php index ea3e7e74..4a4e5a31 100644 --- a/web/tests/CloudFreshSchemaContractTest.php +++ b/web/tests/CloudFreshSchemaContractTest.php @@ -22,7 +22,7 @@ $assert(stripos($schema, 'compatibility import') === false $assert(!preg_match('/\bengine_[A-Za-z0-9_]*\b/', $schema), 'cloud.sql retained an engine-prefixed identifier.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v12-object-cleanup-v1'") + "SCHEMA_VERSION = 'go-agent-wss-v13-vpc-l3-v1'") && !str_contains($setup, "SCHEMA_VERSION = 'go-v1'"), 'Outbound Agent schema must reject the incompatible earlier Go database.'); $assert(str_contains($schema, @@ -38,7 +38,9 @@ foreach (['cloud_host_agents', 'cloud_templates', 'cloud_hosts', 'cloud_instance 'cloud_backup_subscriptions', 'cloud_backup_period_entitlements', 'cloud_backup_restores', 'cloud_backup_recoveries', 'cloud_order_items', 'cloud_invoice_items', - 'cloud_instance_migrations', 'cloud_peer_migration_artifacts'] as $table) { + 'cloud_instance_migrations', 'cloud_peer_migration_artifacts', + 'cloud_host_vpc_transports', 'cloud_vpc_cidr_pools', 'cloud_vpcs', + 'cloud_vpc_ports', 'cloud_vpc_host_states'] as $table) { $assert(str_contains($schema, 'CREATE TABLE ' . $table . ' ('), 'Missing fresh-install table: ' . $table); } diff --git a/web/tests/CloudVpcSchemaContractTest.php b/web/tests/CloudVpcSchemaContractTest.php new file mode 100644 index 00000000..56b9b229 --- /dev/null +++ b/web/tests/CloudVpcSchemaContractTest.php @@ -0,0 +1,114 @@ + 'cloud_vpc_enabled'") + && str_contains($setup, "'value' => 'false'") + && str_contains($setup, "'cloud_job_vpc', 'cloud_job_vpc_port'") + && str_contains($setup, "'chk_cloud_vpc_port_release'"), + 'cloudInit does not publish and validate the disabled VPC schema.'); + +$assert(str_contains($feature, + "public const CONFIG_KEY = 'cloud_vpc_enabled'") + && str_contains($feature, 'public static function enabled(): bool') + && str_contains($feature, 'return self::$cached = false;') + && str_contains($feature, 'public static function save(bool $enabled): void'), + 'The fail-closed VPC feature switch is incomplete.'); + +$models = [ + 'CloudHostVpcTransport' => 'cloud_host_vpc_transports', + 'CloudVpcCidrPool' => 'cloud_vpc_cidr_pools', + 'CloudVpc' => 'cloud_vpcs', + 'CloudVpcPort' => 'cloud_vpc_ports', + 'CloudVpcHostState' => 'cloud_vpc_host_states', +]; +foreach ($models as $class => $table) { + $source = file_get_contents($root . '/app/Models/' . $class . '.php'); + $assert($source !== false + && str_contains($source, 'final class ' . $class . ' extends Model') + && str_contains($source, "protected \$table = '" . $table . "';") + && str_contains($source, 'protected $casts = ['), + 'Missing or incomplete VPC model: ' . $class); +} + +$jobModel = file_get_contents($root . '/app/Models/CloudJob.php'); +$assert(str_contains($jobModel, "'vpc_id' => 'int'") + && str_contains($jobModel, "'vpc_port_id' => 'int'"), + 'CloudJob does not cast VPC correlations.'); + +echo "VPC schema, model and feature-switch contracts passed.\n";