60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Datacenter;
|
|
use App\Models\Host;
|
|
use Illuminate\Support\Facades\DB;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
/**
|
|
* Confirmation for deleting a datacenter (R5). Guarded: a datacenter that still
|
|
* has hosts cannot be deleted — it would orphan host placement — so the modal
|
|
* blocks and explains instead. Otherwise it is safe to hard-delete an unused code.
|
|
*/
|
|
class ConfirmDeleteDatacenter extends ModalComponent
|
|
{
|
|
public string $uuid;
|
|
|
|
public string $name = '';
|
|
|
|
public int $hostCount = 0;
|
|
|
|
public function mount(string $uuid): void
|
|
{
|
|
$dc = Datacenter::query()->where('uuid', $uuid)->withCount('hosts')->firstOrFail();
|
|
$this->uuid = $uuid;
|
|
$this->name = $dc->name;
|
|
$this->hostCount = $dc->hosts_count;
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
// Serialize the guard with the delete: lock the datacenter row and
|
|
// re-check host membership inside the same transaction so a host created
|
|
// concurrently can't be orphaned (hosts.datacenter is a bare code).
|
|
DB::transaction(function () {
|
|
$dc = Datacenter::query()->where('uuid', $this->uuid)->lockForUpdate()->first();
|
|
if ($dc === null) {
|
|
return;
|
|
}
|
|
// Deactivate first so HostCreate's `exists:…,active,1` rule blocks any
|
|
// new host for this code while we finish deleting.
|
|
$dc->update(['active' => false]);
|
|
|
|
if (Host::query()->where('datacenter', $dc->code)->exists()) {
|
|
return; // still has hosts — leave it (deactivated)
|
|
}
|
|
|
|
$dc->delete();
|
|
});
|
|
|
|
return $this->redirectRoute('admin.datacenters', navigate: true);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.confirm-delete-datacenter');
|
|
}
|
|
}
|