- detail response drops id and vm.id/vmware_id, password returns *****
- GET client-service/{uuid}/vm-password returns the decrypted password to the owner
- renew accepts service uuid
- EncryptedOrLegacy cast for module_vmware.password
- vps specs: addon ram is stored in MB, convert to GB
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
122 lines
3.5 KiB
PHP
122 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\LogsActivity;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ModuleVmware extends Model
|
|
{
|
|
use HasFactory, LogsActivity;
|
|
|
|
protected $table = 'module_vmware';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'module_ip_address',
|
|
'port',
|
|
'order_id',
|
|
'first_payment',
|
|
'next_payment',
|
|
'next_due_date',
|
|
'next_invoice',
|
|
'user',
|
|
'password',
|
|
'hostname',
|
|
'vmware_id',
|
|
'vcenter_id',
|
|
'os_id',
|
|
'customer_id',
|
|
'status',
|
|
'service_id',
|
|
'ip',
|
|
'dns',
|
|
'plain_password',
|
|
'virtual_machine_id',
|
|
'cpu',
|
|
'ram_mb',
|
|
'disk_gb',
|
|
];
|
|
|
|
protected $casts = [
|
|
'password' => \App\Casts\EncryptedOrLegacy::class,
|
|
'plain_password' => '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;
|
|
}
|
|
}
|
|
}
|