72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Services\vCenter;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class VpsActionController extends Controller
|
|
{
|
|
protected $vCenterService;
|
|
|
|
public function __construct(vCenter $vCenterService)
|
|
{
|
|
$this->vCenterService = $vCenterService;
|
|
}
|
|
|
|
public function rebuildVps(Request $request)
|
|
{
|
|
set_time_limit(300); // Increase timeout to 5 minutes for slow vCenter connection
|
|
$request->validate([
|
|
'vm_name' => 'required|string',
|
|
'vcenter_id' => 'required|integer' // We need to know which vCenter to connect to
|
|
]);
|
|
|
|
$vmName = $request->input('vm_name');
|
|
$vcenterId = $request->input('vcenter_id');
|
|
|
|
// Retrieve vCenter details from DB to authenticate
|
|
$vcenterModel = \App\Models\vCenter::find($vcenterId);
|
|
|
|
if (!$vcenterModel) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'vCenter ID not found'
|
|
], 404);
|
|
}
|
|
|
|
// Authenticate with vCenter
|
|
$token = $this->vCenterService->loginServer($vcenterModel);
|
|
if (!$token) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Failed to login to vCenter'
|
|
], 401);
|
|
}
|
|
|
|
Log::info("API Rebuild VPS requested for VM: {$vmName} on vCenter: {$vcenterModel->host}");
|
|
|
|
// Call the service function
|
|
$result = $this->vCenterService->reBuildVPS($vmName);
|
|
|
|
if (is_array($result) && isset($result['error']) && $result['error']) {
|
|
return response()->json($result, 500);
|
|
}
|
|
|
|
if ($result === "Login_Again") { // Double check handled in service but good to catch
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Session expired, login again'
|
|
], 401);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Rebuild command executed',
|
|
'data' => $result
|
|
]);
|
|
}
|
|
}
|