update fix deadcode, security
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Service\Concerns;
|
||||
|
||||
use App\Actions\VM\PowerOffVMAction;
|
||||
use App\Actions\VM\PowerOnVMAction;
|
||||
use App\Http\Services\vCenter;
|
||||
use App\Models\ModuleVmware;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
trait ManagesVmPower
|
||||
{
|
||||
/**
|
||||
* POST /v1/system/vm-power-on
|
||||
*
|
||||
* Body: { "product": <ModuleVmware.id|vmware_id|service_uuid> }
|
||||
*/
|
||||
public function powerOn(Request $request, PowerOnVMAction $action): JsonResponse
|
||||
{
|
||||
$productId = $request->input('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['success' => false, 'message' => 'Thiếu product id.'], 422);
|
||||
}
|
||||
|
||||
$vm = $this->resolveVmById($productId, $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$lock = $this->acquireVmLock($vm);
|
||||
if (!$lock) {
|
||||
return $this->vmLocked();
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $action->execute($vm);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'power_on',
|
||||
logName: 'VM',
|
||||
description: "Customer {$request->user()->email} bật VM: {$vm->name} (ID: {$vm->vmware_id})",
|
||||
subject: $vm,
|
||||
);
|
||||
|
||||
return $this->vmResponse($result, 'Đã gửi lệnh bật VM.');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /v1/system/vm-power-off
|
||||
*
|
||||
* Body: { "product": <ModuleVmware.id|vmware_id|service_uuid> }
|
||||
*/
|
||||
public function powerOff(Request $request, PowerOffVMAction $action): JsonResponse
|
||||
{
|
||||
$productId = $request->input('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['success' => false, 'message' => 'Thiếu product id.'], 422);
|
||||
}
|
||||
|
||||
$vm = $this->resolveVmById($productId, $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$lock = $this->acquireVmLock($vm);
|
||||
if (!$lock) {
|
||||
return $this->vmLocked();
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $action->execute($vm);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'power_off',
|
||||
logName: 'VM',
|
||||
description: "Customer {$request->user()->email} tắt VM: {$vm->name} (ID: {$vm->vmware_id})",
|
||||
subject: $vm,
|
||||
);
|
||||
|
||||
return $this->vmResponse($result, 'Đã gửi lệnh tắt VM.');
|
||||
}
|
||||
|
||||
public function restart(Request $request, vCenter $serviceVCenter): JsonResponse
|
||||
{
|
||||
$productId = $request->input('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['error' => true, 'message' => 'Thiếu product id'], 422);
|
||||
}
|
||||
|
||||
$vm = $this->resolveVmById($productId, $request);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$lock = $this->acquireVmLock($vm);
|
||||
if (!$lock) {
|
||||
return $this->vmLocked();
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $serviceVCenter->restartVM($vm, $vm->vmware_id);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
|
||||
if (is_string($result) || ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => is_string($result) ? $result : ($result['data'] ?? 'Không thể restart VM'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'restart',
|
||||
logName: 'VM',
|
||||
description: "Customer {$request->user()->email} restart VM: {$vm->name} (ID: {$vm->vmware_id})",
|
||||
subject: $vm,
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đang restart máy ảo vui lòng chờ!',
|
||||
'data' => $result['data'] ?? 'success',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /v1/system/vm-power-state?product={id|uuid|vmware_id}
|
||||
*
|
||||
* Calls vSphere REST API directly: GET /rest/vcenter/vm/{vmware_id}/power
|
||||
* Returns: { success: true, data: { state: "POWERED_ON"|"POWERED_OFF"|"SUSPENDED", clean_power_off: bool } }
|
||||
*/
|
||||
public function powerState(Request $request, vCenter $serviceVCenter): JsonResponse
|
||||
{
|
||||
$productId = $request->input('product');
|
||||
if (!$productId) {
|
||||
return response()->json(['success' => false, 'message' => 'Thiếu product id.'], 422);
|
||||
}
|
||||
|
||||
$vm = $this->resolveVmById($productId, $request, requireActiveService: false);
|
||||
if ($vm instanceof JsonResponse) {
|
||||
return $vm;
|
||||
}
|
||||
|
||||
$result = $serviceVCenter->getVmPowerState($vm, $vm->vmware_id);
|
||||
|
||||
if ($result === 'Login_Again') {
|
||||
return response()->json(['success' => false, 'message' => 'Không thể đăng nhập vào vCenter.'], 400);
|
||||
}
|
||||
|
||||
if (is_array($result) && ($result['error'] ?? true)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $result['data'] ?? 'Không thể lấy trạng thái VM.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'state' => $result['data']['state'] ?? null,
|
||||
'clean_power_off' => $result['data']['clean_power_off'] ?? null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user