coolify/app/Livewire/Server/Delete.php

76 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server;
2024-10-17 12:56:36 +00:00
use App\Actions\Server\DeleteServer;
2024-10-30 13:54:27 +00:00
use App\Models\Server;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2024-09-23 17:51:31 +00:00
use Livewire\Component;
class Delete extends Component
{
use AuthorizesRequests;
2024-10-30 13:54:27 +00:00
public Server $server;
public bool $delete_from_hetzner = false;
2024-10-30 13:54:27 +00:00
public function mount(string $server_uuid)
{
try {
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function delete($password, $selectedActions = [])
{
if (! verifyPasswordConfirmation($password, $this)) {
return 'The provided password is incorrect.';
}
if (! empty($selectedActions)) {
$this->delete_from_hetzner = in_array('delete_from_hetzner', $selectedActions);
2024-09-04 18:06:22 +00:00
}
try {
$this->authorize('delete', $this->server);
2023-11-06 17:04:18 +00:00
if ($this->server->hasDefinedResources()) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Server has defined resources. Please delete them first.');
2024-06-10 20:43:34 +00:00
return;
}
2024-10-17 12:56:36 +00:00
$this->server->delete();
2025-10-09 14:54:13 +00:00
DeleteServer::dispatch(
$this->server->id,
$this->delete_from_hetzner,
$this->server->hetzner_server_id,
$this->server->cloud_provider_token_id,
$this->server->team_id
);
2024-10-17 20:08:23 +00:00
return redirectRoute($this, 'server.index');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function render()
{
$checkboxes = [];
if ($this->server->hetzner_server_id) {
$checkboxes[] = [
'id' => 'delete_from_hetzner',
'label' => 'Also delete server from Hetzner Cloud',
'default_warning' => 'The actual server on Hetzner Cloud will NOT be deleted.',
];
}
return view('livewire.server.delete', [
'checkboxes' => $checkboxes,
]);
}
}