-- Automatic VM -> Proxmox host links for one customer ($1). A link is set only for a -- unique MAC match between a host's current guest list and a current virtual device of -- the same customer. Manual decisions (hypervisor_source='manual') are never touched. WITH current_installation AS ( SELECT asset.asset_id, asset.hypervisor_asset_id, asset.hypervisor_source, inst.agent_id FROM assets asset JOIN LATERAL ( SELECT a.agent_id, a.revoked_at FROM agent_installations a WHERE a.tenant_id=asset.tenant_id AND a.asset_id=asset.asset_id AND NOT a.customer_transfer_pending AND a.last_authenticated_at IS NOT NULL ORDER BY a.enrollment_generation DESC, a.created_at DESC, a.agent_id LIMIT 1 ) inst ON inst.revoked_at IS NULL WHERE asset.tenant_id=$1 AND asset.archived_at IS NULL ), latest AS ( SELECT c.*, inventory.source_id, inventory.data FROM current_installation c JOIN LATERAL ( SELECT source_id, data FROM observations WHERE tenant_id=$1 AND agent_id=c.agent_id AND source_id IN ('linux.base','windows.base','opnsense.base') ORDER BY observed_at DESC, received_at DESC, sequence DESC LIMIT 1 ) inventory ON true ), scopes AS ( SELECT l.asset_id, o FROM latest l, jsonb_array_elements(CASE WHEN jsonb_typeof(l.data->'observations')='array' THEN l.data->'observations' ELSE '[]'::jsonb END) o WHERE o->>'quality'='valid' UNION ALL -- An OPNsense snapshot is flat: hardware and interfaces sit at the top level. SELECT l.asset_id, jsonb_build_object('scope','opnsense.hardware','quality','valid','data',l.data->'hardware') FROM latest l WHERE l.source_id='opnsense.base' AND jsonb_typeof(l.data->'hardware')='object' UNION ALL SELECT l.asset_id, jsonb_build_object('scope','opnsense.interfaces','quality','valid','data',l.data->'interfaces') FROM latest l WHERE l.source_id='opnsense.base' AND l.data#>>'{interfaces,status}'='ok' ), proxmox_hosts AS ( SELECT asset_id, o->'data' AS data FROM scopes WHERE o->>'scope'='linux.proxmox' ), guest_macs AS ( SELECT DISTINCT h.asset_id AS host_asset_id, lower(mac) AS mac FROM proxmox_hosts h, jsonb_array_elements(h.data->'guests') g, jsonb_array_elements_text(g->'macs') mac ), virtual_devices AS ( SELECT asset_id FROM scopes WHERE o->>'scope' IN ('linux.hardware','windows.hardware','opnsense.hardware') AND o#>>'{data,device_type}'='virtual' ), device_macs AS ( SELECT DISTINCT s.asset_id, lower(i->>'mac') AS mac FROM scopes s JOIN virtual_devices v ON v.asset_id=s.asset_id, jsonb_array_elements(CASE WHEN jsonb_typeof(s.o->'data')='array' THEN s.o->'data' WHEN jsonb_typeof(s.o#>'{data,entries}')='array' THEN s.o#>'{data,entries}' ELSE '[]'::jsonb END) i WHERE s.o->>'scope' IN ('linux.interfaces','windows.interfaces','opnsense.interfaces') AND i->>'mac' ~* '^[0-9a-f]{2}(:[0-9a-f]{2}){5}$' AND lower(i->>'mac') <> '00:00:00:00:00:00' ), -- A MAC that more than one device or more than one host claims proves nothing. unique_macs AS ( SELECT d.asset_id, g.host_asset_id FROM device_macs d JOIN guest_macs g ON g.mac=d.mac WHERE d.asset_id <> g.host_asset_id AND (SELECT count(*) FROM device_macs d2 WHERE d2.mac=d.mac)=1 AND (SELECT count(*) FROM guest_macs g2 WHERE g2.mac=d.mac)=1 ), matches AS ( SELECT asset_id, min(host_asset_id::text)::uuid AS host_asset_id FROM unique_macs GROUP BY asset_id HAVING count(DISTINCT host_asset_id)=1 ), desired AS ( -- New or changed automatic links. SELECT l.asset_id, l.hypervisor_asset_id AS previous, m.host_asset_id AS next FROM latest l JOIN matches m ON m.asset_id=l.asset_id WHERE l.hypervisor_source IS DISTINCT FROM 'manual' AND (l.hypervisor_asset_id IS DISTINCT FROM m.host_asset_id OR l.hypervisor_source IS NULL) UNION ALL -- The assigned host reports a valid guest list that no longer contains this device. SELECT l.asset_id, l.hypervisor_asset_id, NULL FROM latest l WHERE l.hypervisor_source='proxmox' AND NOT EXISTS (SELECT FROM matches m WHERE m.asset_id=l.asset_id) AND EXISTS (SELECT FROM proxmox_hosts h WHERE h.asset_id=l.hypervisor_asset_id) AND EXISTS (SELECT FROM device_macs d WHERE d.asset_id=l.asset_id) ), changed AS ( UPDATE assets a SET hypervisor_asset_id=d.next, hypervisor_source=CASE WHEN d.next IS NULL THEN NULL ELSE 'proxmox' END FROM desired d WHERE a.tenant_id=$1 AND a.asset_id=d.asset_id RETURNING a.asset_id, d.previous, d.next ) INSERT INTO audit_events(tenant_id,event_id,principal,event_type,data,created_at) SELECT $1, gen_random_uuid(), 'system:proxmox-guest-detection', CASE WHEN next IS NULL THEN 'device_hypervisor_released' ELSE 'device_hypervisor_detected' END, jsonb_build_object('asset_id',asset_id,'previous_hypervisor_asset_id',previous, 'hypervisor_asset_id',next,'match','mac'), now() FROM changed WHERE previous IS DISTINCT FROM next RETURNING event_id