Compare commits

...

2 Commits

Author SHA1 Message Date
JonatanRek
0b29730e7f Merge branch 'main' of https://git.steelants.cz/JonatanRek/LAR_Maintenance 2024-09-25 22:09:17 +02:00
JonatanRek
1cc854ae1f Add Host Groups 2024-09-25 22:09:13 +02:00
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 @@ public function handle(Request $request, Closure $next): Response
Menu::make('settings-menu', function ($menu) { Menu::make('settings-menu', function ($menu) {
$systemRoutes = [ $systemRoutes = [
'Host' => [' fas fa-server', 'host'], 'Host' => [' fas fa-server', 'host'],
'Host Groups' => [' fas fa-server', 'host_groups'],
'Maintenance' => [' fas fa-calendar', 'maintenance'], 'Maintenance' => [' fas fa-calendar', 'maintenance'],
'Tasks' => [' fas fa-list', 'tasks'], 'Tasks' => [' fas fa-list', 'tasks'],
]; ];

View File

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

View File

@ -3,12 +3,16 @@
use Livewire\Component; use Livewire\Component;
use App\Models\Host; use App\Models\Host;
use App\Models\HostGroup;
class Form extends Component class Form extends Component
{ {
public $model; public $model;
public string $hostname; public string $hostname;
public $host_groups = [];
public $host_groups_available = [];
public $action = 'store'; public $action = 'store';
protected function rules() protected function rules()
@ -19,12 +23,15 @@ protected function rules()
} }
public function mount ($model = null){ public function mount ($model = null){
$this->host_groups_available = HostGroup::pluck('name', 'id')->toArray();
if (!empty($model)) { if (!empty($model)) {
$host = Host::find($model); $host = Host::find($model);
$this->model = $model; $this->model = $model;
$this->hostname = $host->hostname; $this->hostname = $host->hostname;
$this->host_groups = $host->hostGroups()->pluck('host_groups.id')->toArray();
$this->action = 'update'; $this->action = 'update';
} }
@ -33,7 +40,8 @@ public function mount ($model = null){
public function store() public function store()
{ {
$validatedData = $this->validate(); $validatedData = $this->validate();
Host::create($validatedData); $host = Host::create($validatedData);
$host->hostGroups()->sync($this->host_groups);
$this->dispatch('closeModal'); $this->dispatch('closeModal');
} }
@ -43,6 +51,7 @@ public function update()
$host = Host::find($this->model); $host = Host::find($this->model);
if (!empty($host)) { if (!empty($host)) {
$host->update($validatedData); $host->update($validatedData);
$host->hostGroups()->sync($this->host_groups);
} }
$this->dispatch('closeModal'); $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() 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> <div>
<x-form::form wire:submit.prevent="{{$action}}"> <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::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::button class="btn-primary" type="submit">Create</x-form::button>
</x-form::form> </x-form::form>
</div> </div>

View File

@ -22,7 +22,7 @@
Route::get('/host', [App\Http\Controllers\HostController::class, 'index'])->name('host'); 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/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('/maintenance', [App\Http\Controllers\MaintenanceController::class, 'index'])->name('maintenance');
Route::get('/tasks', [App\Http\Controllers\TaskController::class, 'index'])->name('tasks'); Route::get('/tasks', [App\Http\Controllers\TaskController::class, 'index'])->name('tasks');