'encrypted', ]; public function customer() { return $this->belongsTo(\App\Models\Customer::class); } public function ipAddress() { return $this->belongsTo(\App\Models\ModuleIpAddress::class, 'module_ip_address', 'id'); } public function vCenter() { return $this->belongsTo(\App\Models\vCenter::class, 'vcenter_id', 'id'); } public function os() { return $this->belongsTo(\App\Models\Os::class); } public function osGroup() { return $this->belongsTo(\App\Models\OsGroup::class); } public function order() { return $this->belongsTo(\App\Models\Order::class); } public function virtualMachine() { return $this->hasOne(\App\Models\VirtualMachines::class, 'vm_id', 'vmware_id'); } public function services() { return $this->hasMany(\App\Models\Service::class, 'vm_id', 'vmware_id'); } /** Direct link via service_id FK (new). */ public function service() { return $this->belongsTo(\App\Models\Service::class, 'service_id'); } /** * `virtual_machine_id`/`cpu`/`ram_mb` are only written when a service is * created directly against a known vCenter inventory row (admin "create * service" flow) or after a manual config change (UpdateVmConfigJob) — * the normal auto-provisioning pipeline never sets them, since the VM * isn't synced into `virtual_machines` yet at creation time. Backfill * them here from the live `vmware_id` ↔ `virtual_machines.vm_id` match * so API responses always expose the real link/spec (needed by the * admin UI to show CPU/RAM and enable power on/off/restart/console). * * `disk_gb` has no source anywhere in the app (vCenter sync doesn't * track disk size) — it stays null until captured some other way. */ public static function resolveVirtualMachineIds(iterable $vmwares): void { $candidates = collect($vmwares)->filter(fn ($vm) => $vm && $vm->vmware_id && (!$vm->virtual_machine_id || !$vm->cpu || !$vm->ram_mb)); if ($candidates->isEmpty()) { return; } $inventory = \App\Models\VirtualMachines::whereIn('vm_id', $candidates->pluck('vmware_id')->unique()) ->get(['id', 'vm_id', 'cpu', 'ram_mb']) ->keyBy('vm_id'); foreach ($candidates as $vm) { $match = $inventory->get($vm->vmware_id); if (!$match) { continue; } $vm->virtual_machine_id ??= $match->id; $vm->cpu ??= $match->cpu; $vm->ram_mb ??= $match->ram_mb; } } }