148 lines
4.8 KiB
PHP
148 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\vm;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\VirtualMachine\Sync\SyncVirtualMachine;
|
|
use App\Repositories\Module\VirtualMachine\VirtualMachineRepositoryInterface;
|
|
use App\Repositories\vCenter\vCenterRepositoryInterface;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class VirtualMachineController extends Controller
|
|
{
|
|
public function __construct(
|
|
public VirtualMachineRepositoryInterface $virtualMachineRepository,
|
|
public vCenterRepositoryInterface $vCenterRepository
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$vms = $this->virtualMachineRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
]
|
|
);
|
|
$title = 'List all Virtual Machine';
|
|
return view('administrator.pages.package.vm.index', compact('vms', 'title'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'Add Virtual Machine';
|
|
return view('administrator.pages.package.vm.add', compact('title'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'vm_id' => 'required|unique:virtual_machines,vm_id',
|
|
'name' => 'required',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$data = $request->all();
|
|
if (empty($data['uuid'])) {
|
|
$data['uuid'] = Str::uuid();
|
|
}
|
|
$this->virtualMachineRepository->create($data);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.vm.index')->with('success', 'Add new VM successfully!');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$vm = $this->virtualMachineRepository->find($id);
|
|
if (!$vm) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
$title = 'Edit Virtual Machine';
|
|
return view('administrator.pages.package.vm.edit', compact('title', 'vm'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$vm = $this->virtualMachineRepository->find($id);
|
|
if (!$vm) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'vm_id' => 'required|unique:virtual_machines,vm_id,' . $id,
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->virtualMachineRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.vm.index')->with('success', 'Update VM successfully!');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$vm = $this->virtualMachineRepository->find($id);
|
|
if (!$vm) {
|
|
return back()->withErrors('Invalid request.');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->virtualMachineRepository->delete($id);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.vm.index')->with('success', 'Deleted VM successfully!');
|
|
}
|
|
|
|
public function sync(SyncVirtualMachine $syncVM)
|
|
{
|
|
dispatch($syncVM)->delay(now()->addSeconds(5));
|
|
return back()->with('success', 'The process is being established, please wait');
|
|
}
|
|
|
|
public function datatable(Request $request)
|
|
{
|
|
$query = $this->virtualMachineRepository->filter(
|
|
$request->all(),
|
|
['datacenter', 'host'],
|
|
[],
|
|
['*'],
|
|
true
|
|
);
|
|
|
|
return DataTables::eloquent($query)
|
|
->addColumn(
|
|
'action',
|
|
function ($d) {
|
|
return '<a href="' . route('package.vm.edit', $d->id) . '" class="btn btn-warning btn-sm"><i class="fas fa-search"></i></a>
|
|
<form method="post" class="d-inline" action="' . route('package.vm.destroy', $d->id) . '">
|
|
' . csrf_field() . '
|
|
' . method_field('delete') . '
|
|
<button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
|
|
</form>';
|
|
}
|
|
)
|
|
->editColumn('updated_at', function ($d) {
|
|
return $d->updated_at ? $d->updated_at->format('H:i:s d/m/Y') : '';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
} |