280 lines
7.9 KiB
PHP
280 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\VPS;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Services\vCenter;
|
|
use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class VPSController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly VmwareRepositoryInterface $vmwareRepository,
|
|
private readonly vCenter $vCenterService
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$vpsList = $this->vmwareRepository->filter(
|
|
$request->all(),
|
|
['ipaddress.host.datacenter.vCenter', 'os.osGroup', 'order']
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $vpsList
|
|
]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->findWithRelationships(
|
|
$id,
|
|
['ipaddress.host.datacenter.vCenter', 'os.osGroup', 'order']
|
|
);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $vps
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->find($id);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->vmwareRepository->update($id, $request->all());
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'VPS updated successfully',
|
|
'data' => $this->vmwareRepository->find($id)
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function powerOn(int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->findWithRelationships(
|
|
$id,
|
|
['vCenter', 'ipaddress.host.datacenter.vCenter']
|
|
);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
$vcenter = $vps->vCenter ?? $vps->ipaddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter || !$this->vCenterService->loginServer($vcenter)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't connect to vCenter"
|
|
], 400);
|
|
}
|
|
|
|
$result = $this->vCenterService->StartVirtualMachine($vps, $vps->vmware_id);
|
|
|
|
if (is_string($result)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result
|
|
], 400);
|
|
}
|
|
|
|
if ($result['error'] ?? true) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result['data'] ?? 'Failed to power on VM'
|
|
], 400);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'VM powered on successfully'
|
|
]);
|
|
}
|
|
|
|
public function powerOff(int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->findWithRelationships(
|
|
$id,
|
|
['vCenter', 'ipaddress.host.datacenter.vCenter']
|
|
);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
$vcenter = $vps->vCenter ?? $vps->ipaddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter || !$this->vCenterService->loginServer($vcenter)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't connect to vCenter"
|
|
], 400);
|
|
}
|
|
|
|
$result = $this->vCenterService->ShutdownGuestVirtualMachine($vps, $vps->vmware_id);
|
|
|
|
if (is_string($result)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result
|
|
], 400);
|
|
}
|
|
|
|
if ($result['error'] ?? true) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result['data'] ?? 'Failed to power off VM'
|
|
], 400);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'VM powered off successfully'
|
|
]);
|
|
}
|
|
|
|
public function restart(int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->findWithRelationships(
|
|
$id,
|
|
['vCenter', 'ipaddress.host.datacenter.vCenter']
|
|
);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
$vcenter = $vps->vCenter ?? $vps->ipaddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter || !$this->vCenterService->loginServer($vcenter)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't connect to vCenter"
|
|
], 400);
|
|
}
|
|
|
|
$result = $this->vCenterService->restartVM($vps, $vps->vmware_id);
|
|
|
|
if (is_string($result)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result
|
|
], 400);
|
|
}
|
|
|
|
if ($result['error']) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $result['message'] ?? 'Failed to restart VM'
|
|
], 400);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'VM restart command sent successfully'
|
|
]);
|
|
}
|
|
|
|
public function getConsoleTicket(int $id): JsonResponse
|
|
{
|
|
$vps = $this->vmwareRepository->findWithRelationships(
|
|
$id,
|
|
['ipaddress.host.datacenter.vCenter', 'vCenter']
|
|
);
|
|
|
|
if (!$vps) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find VPS by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
$vcenter = $vps->vCenter ?? $vps->ipaddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find vCenter information"
|
|
], 404);
|
|
}
|
|
|
|
if (!$this->vCenterService->loginServer($vcenter)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't connect to vCenter"
|
|
], 400);
|
|
}
|
|
|
|
$ticket = $this->vCenterService->GetConsoleTickets($vps->vmware_id);
|
|
|
|
if ($ticket === 'Login_Again') {
|
|
if ($this->vCenterService->loginServer($vcenter)) {
|
|
$ticket = $this->vCenterService->GetConsoleTickets($vps->vmware_id);
|
|
} else {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't reconnect to vCenter"
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
if (is_string($ticket)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Failed to get console ticket: $ticket"
|
|
], 400);
|
|
}
|
|
|
|
if ($ticket['error']) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $ticket['data']
|
|
], 400);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $ticket['data'],
|
|
]);
|
|
}
|
|
}
|