From 1cc854ae1fa1fd6195fde4554aa302b0d60da89d Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Wed, 25 Sep 2024 22:09:13 +0200 Subject: [PATCH] Add Host Groups --- app/Http/Controllers/HostGroupController.php | 24 ++++++++ app/Http/Middleware/GenerateMenus.php | 1 + app/Livewire/Host/DataTable.php | 9 ++- app/Livewire/Host/Form.php | 11 +++- app/Livewire/HostGroup/DataTable.php | 59 +++++++++++++++++++ app/Livewire/HostGroup/Form.php | 54 +++++++++++++++++ app/Models/Host.php | 7 ++- app/Models/HostGroup.php | 21 +++++++ app/Models/HostHostGroup.php | 10 ++++ ..._09_25_170116_create_host_groups_table.php | 29 +++++++++ ...70205_add_host_group_id_to_hosts_table.php | 29 +++++++++ ...25_170527_create_host_host_group_table.php | 30 ++++++++++ resources/views/host_groups/index.blade.php | 20 +++++++ .../views/livewire/host-group/form.blade.php | 6 ++ resources/views/livewire/host/form.blade.php | 3 +- routes/web.php | 2 +- 16 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/HostGroupController.php create mode 100644 app/Livewire/HostGroup/DataTable.php create mode 100644 app/Livewire/HostGroup/Form.php create mode 100644 app/Models/HostGroup.php create mode 100644 app/Models/HostHostGroup.php create mode 100644 database/migrations/2024_09_25_170116_create_host_groups_table.php create mode 100644 database/migrations/2024_09_25_170205_add_host_group_id_to_hosts_table.php create mode 100644 database/migrations/2024_09_25_170527_create_host_host_group_table.php create mode 100644 resources/views/host_groups/index.blade.php create mode 100644 resources/views/livewire/host-group/form.blade.php diff --git a/app/Http/Controllers/HostGroupController.php b/app/Http/Controllers/HostGroupController.php new file mode 100644 index 0000000..073bf98 --- /dev/null +++ b/app/Http/Controllers/HostGroupController.php @@ -0,0 +1,24 @@ +connect(config('zabbix.username'), config('zabbix.password')); + + + return redirect()->back(); + } +} diff --git a/app/Http/Middleware/GenerateMenus.php b/app/Http/Middleware/GenerateMenus.php index 8463b84..7b67849 100644 --- a/app/Http/Middleware/GenerateMenus.php +++ b/app/Http/Middleware/GenerateMenus.php @@ -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'], ]; diff --git a/app/Livewire/Host/DataTable.php b/app/Livewire/Host/DataTable.php index ce9020d..c73e533 100644 --- a/app/Livewire/Host/DataTable.php +++ b/app/Livewire/Host/DataTable.php @@ -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 [ diff --git a/app/Livewire/Host/Form.php b/app/Livewire/Host/Form.php index 6e3cd40..753e3bc 100644 --- a/app/Livewire/Host/Form.php +++ b/app/Livewire/Host/Form.php @@ -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'); } diff --git a/app/Livewire/HostGroup/DataTable.php b/app/Livewire/HostGroup/DataTable.php new file mode 100644 index 0000000..b2183e6 --- /dev/null +++ b/app/Livewire/HostGroup/DataTable.php @@ -0,0 +1,59 @@ + '$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]); + } +} diff --git a/app/Livewire/HostGroup/Form.php b/app/Livewire/HostGroup/Form.php new file mode 100644 index 0000000..b2aef5e --- /dev/null +++ b/app/Livewire/HostGroup/Form.php @@ -0,0 +1,54 @@ + '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'); + } +} diff --git a/app/Models/Host.php b/app/Models/Host.php index 78b711a..8f81e4d 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -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); } } diff --git a/app/Models/HostGroup.php b/app/Models/HostGroup.php new file mode 100644 index 0000000..d1c18a2 --- /dev/null +++ b/app/Models/HostGroup.php @@ -0,0 +1,21 @@ +belongsToMany(Host::class)->using(HostHostGroup::class); + } +} diff --git a/app/Models/HostHostGroup.php b/app/Models/HostHostGroup.php new file mode 100644 index 0000000..56f7f1d --- /dev/null +++ b/app/Models/HostHostGroup.php @@ -0,0 +1,10 @@ +id(); + $table->string('name'); + $table->string('external_id')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('host_groups'); + } +}; diff --git a/database/migrations/2024_09_25_170205_add_host_group_id_to_hosts_table.php b/database/migrations/2024_09_25_170205_add_host_group_id_to_hosts_table.php new file mode 100644 index 0000000..792bec9 --- /dev/null +++ b/database/migrations/2024_09_25_170205_add_host_group_id_to_hosts_table.php @@ -0,0 +1,29 @@ +foreignIdFor(HostGroup::class, 'host_group_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('hosts', function (Blueprint $table) { + // + }); + } +}; diff --git a/database/migrations/2024_09_25_170527_create_host_host_group_table.php b/database/migrations/2024_09_25_170527_create_host_host_group_table.php new file mode 100644 index 0000000..86bbdb6 --- /dev/null +++ b/database/migrations/2024_09_25_170527_create_host_host_group_table.php @@ -0,0 +1,30 @@ +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'); + } +}; diff --git a/resources/views/host_groups/index.blade.php b/resources/views/host_groups/index.blade.php new file mode 100644 index 0000000..d37efb5 --- /dev/null +++ b/resources/views/host_groups/index.blade.php @@ -0,0 +1,20 @@ + +
+ + + @livewire('host_group.data-table', [], key('data-table')) +
+
diff --git a/resources/views/livewire/host-group/form.blade.php b/resources/views/livewire/host-group/form.blade.php new file mode 100644 index 0000000..dccb039 --- /dev/null +++ b/resources/views/livewire/host-group/form.blade.php @@ -0,0 +1,6 @@ +
+ + + Create + +
diff --git a/resources/views/livewire/host/form.blade.php b/resources/views/livewire/host/form.blade.php index b992a01..05c902b 100644 --- a/resources/views/livewire/host/form.blade.php +++ b/resources/views/livewire/host/form.blade.php @@ -1,6 +1,7 @@
+ Create -
\ No newline at end of file + diff --git a/routes/web.php b/routes/web.php index 40d1f55..085fc60 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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');