Add Host Groups

This commit is contained in:
JonatanRek 2024-09-25 22:09:13 +02:00
parent b749cdf591
commit 1cc854ae1f
16 changed files with 310 additions and 5 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers;
use App\Models\Host;
use App\Services\ZabbixService;
use Carbon\Carbon;
class HostGroupController extends BaseController
{
public function index()
{
return view('host_groups.index');
}
public function sync()
{
$zabbix = new ZabbixService(config('zabbix.url'));
$zabbix->connect(config('zabbix.username'), config('zabbix.password'));
return redirect()->back();
}
}

View File

@ -45,6 +45,7 @@ class GenerateMenus
Menu::make('settings-menu', function ($menu) {
$systemRoutes = [
'Host' => [' fas fa-server', 'host'],
'Host Groups' => [' fas fa-server', 'host_groups'],
'Maintenance' => [' fas fa-calendar', 'maintenance'],
'Tasks' => [' fas fa-list', 'tasks'],
];

View File

@ -18,7 +18,7 @@ class DataTable extends DataTableComponent
public function query(): Builder
{
return Host::query();
return Host::query()->with(['hostGroups']);
}
public function headers(): array
@ -26,6 +26,7 @@ class DataTable extends DataTableComponent
return [
'display_name' => 'display_name',
'hostname' => 'hostname',
'host_groups' => 'host_groups',
];
}
@ -33,6 +34,12 @@ class DataTable extends DataTableComponent
Host::find($host_id)->delete();
}
public function columnHostGroups(mixed $column): mixed
{
return implode(", " , $column->pluck('name')->toArray());
}
public function actions($item)
{
return [

View File

@ -3,12 +3,16 @@ namespace App\Livewire\Host;
use Livewire\Component;
use App\Models\Host;
use App\Models\HostGroup;
class Form extends Component
{
public $model;
public string $hostname;
public $host_groups = [];
public $host_groups_available = [];
public $action = 'store';
protected function rules()
@ -19,12 +23,15 @@ class Form extends Component
}
public function mount ($model = null){
$this->host_groups_available = HostGroup::pluck('name', 'id')->toArray();
if (!empty($model)) {
$host = Host::find($model);
$this->model = $model;
$this->hostname = $host->hostname;
$this->host_groups = $host->hostGroups()->pluck('host_groups.id')->toArray();
$this->action = 'update';
}
@ -33,7 +40,8 @@ class Form extends Component
public function store()
{
$validatedData = $this->validate();
Host::create($validatedData);
$host = Host::create($validatedData);
$host->hostGroups()->sync($this->host_groups);
$this->dispatch('closeModal');
}
@ -43,6 +51,7 @@ class Form extends Component
$host = Host::find($this->model);
if (!empty($host)) {
$host->update($validatedData);
$host->hostGroups()->sync($this->host_groups);
}
$this->dispatch('closeModal');
}

View File

@ -0,0 +1,59 @@
<?php
namespace App\Livewire\HostGroup;
use App\Models\HostGroup;
use SteelAnts\DataTable\Livewire\DataTableComponent;
use Illuminate\Database\Eloquent\Builder;
class DataTable extends DataTableComponent
{
public $listeners = [
'hostgroupAdded' => '$refresh',
'closeModal' => '$refresh',
];
public function query(): Builder
{
return HostGroup::query()->with(['hosts:id']);
}
public function headers(): array
{
return [
'name' => 'name',
'hosts_count' => 'hosts_count',
];
}
public function row($row){
$row['hosts_count'] = $row->hosts->count();
return $row;
}
public function remove($hostgroup_id){
HostGroup::find($hostgroup_id)->delete();
}
public function actions($item)
{
return [
[
'type' => "livewire",
'action' => "edit",
'text' => "edit",
'parameters' => $item['id']
],
[
'type' => "livewire",
'action' => "remove",
'text' => "remove",
'parameters' => $item['id']
]
];
}
public function edit($host_id)
{
$this->dispatch('openModal', 'host.form', __('boilerplate::host.edit'), ['model' => $host_id]);
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace App\Livewire\HostGroup;
use Livewire\Component;
use App\Models\HostGroup;
class Form extends Component
{
public $model;
public string $name;
public $action = 'store';
protected function rules()
{
return [
'name' => 'required',
];
}
public function mount ($model = null){
if (!empty($model)) {
$hostGroup = HostGroup::find($model);
$this->model = $model;
$this->name = $hostGroup->name;
$this->action = 'update';
}
}
public function store()
{
$validatedData = $this->validate();
HostGroup::create($validatedData);
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
$hostGroup = HostGroup::find($this->model);
if (!empty($hostGroup)) {
$hostGroup->update($validatedData);
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.host-group.form');
}
}

View File

@ -17,6 +17,11 @@ class Host extends Model
public function tasks()
{
return $this->hasMany(MaintenanceTask::class,);
return $this->hasMany(MaintenanceTask::class);
}
public function hostGroups()
{
return $this->belongsToMany(HostGroup::class)->using(HostHostGroup::class);
}
}

21
app/Models/HostGroup.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class HostGroup extends Model
{
use HasFactory;
protected $fillable = [
'name',
'external_id',
];
public function hosts()
{
return $this->belongsToMany(Host::class)->using(HostHostGroup::class);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class HostHostGroup extends Pivot
{
//
}

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('host_groups', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('external_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('host_groups');
}
};

View File

@ -0,0 +1,29 @@
<?php
use App\Models\HostGroup;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
$table->foreignIdFor(HostGroup::class, 'host_group_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
});
}
};

View File

@ -0,0 +1,30 @@
<?php
use App\Models\Host;
use App\Models\HostGroup;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('host_host_group', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Host::class, 'host_id')->nullable();
$table->foreignIdFor(HostGroup::class, 'host_group_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('host_host_group');
}
};

View File

@ -0,0 +1,20 @@
<x-layout-app>
<div class="container-xl">
<div class="page-header">
<h1>{{ __('boilerplate::host_groups.title') }}</h1>
<div>
<a class="btn btn-primary" href="{{ route('host.sync') }}">
<i class="me-2 fas fa-sync-alt"></i><span>{{ __('boilerplate::ui.sync') }}</span>
</a>
<button class="btn btn-primary"
onclick="Livewire.dispatch('openModal', {livewireComponents: 'host_group.form', title: '{{ __('boilerplate::host_group.create') }}'})">
<i class="me-2 fas fa-plus"></i><span>{{ __('boilerplate::ui.add') }}</span>
</button>
</div>
</div>
@livewire('host_group.data-table', [], key('data-table'))
</div>
</x-layout-app>

View File

@ -0,0 +1,6 @@
<div>
<x-form::form wire:submit.prevent="{{$action}}">
<x-form::input group-class="mb-3" type="text" wire:model="name" id="name" label="name"/>
<x-form::button class="btn-primary" type="submit">Create</x-form::button>
</x-form::form>
</div>

View File

@ -1,6 +1,7 @@
<div>
<x-form::form wire:submit.prevent="{{$action}}">
<x-form::input group-class="mb-3" type="text" wire:model="hostname" id="hostname" label="hostname"/>
<x-form::select group-class="mb-3" wire:model.live="host_groups" label="Select groups for Host" :options="$host_groups_available" placeholder="Select value..." multiple />
<x-form::button class="btn-primary" type="submit">Create</x-form::button>
</x-form::form>
</div>

View File

@ -18,7 +18,7 @@ Route::get('/maintenance/history/{maintenance_history}', [App\Http\Controllers\M
Route::get('/host', [App\Http\Controllers\HostController::class, 'index'])->name('host');
Route::get('/host/sync', [App\Http\Controllers\HostController::class, 'sync'])->name('host.sync');
Route::get('/host_groups', [App\Http\Controllers\HostGroupController::class, 'index'])->name('host_groups');
Route::get('/maintenance', [App\Http\Controllers\MaintenanceController::class, 'index'])->name('maintenance');
Route::get('/tasks', [App\Http\Controllers\TaskController::class, 'index'])->name('tasks');