Files
portal-api/app/Http/Controllers/Api/Admin/Provider/ProviderSettingController.php
T
2026-06-22 08:43:32 +07:00

74 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin\Provider;
use App\Http\Controllers\Controller;
use App\Models\vCenter;
use App\Services\VirtualMachine\VmProviderSettingsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
/**
* Reads / writes the provider config blob in setting key=`provider`.
*
* active_vcenter_id is read-only here — it mirrors module_vcenter.status=1.
* To switch the active vCenter, call ActiveInfraController::activate().
*/
class ProviderSettingController extends Controller
{
public function __construct(private readonly VmProviderSettingsService $service)
{
}
/** GET /administrator/provider/settings */
public function index(): JsonResponse
{
$data = $this->service->all();
// Mirror current truth: active vCenter id from module_vcenter
$data['active_vcenter_id'] = vCenter::where('status', 1)->value('id');
return response()->json([
'error' => false,
'data' => $data,
]);
}
/** PUT /administrator/provider/settings */
public function update(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'default_folder' => 'sometimes|string|max:100',
'default_resource_pool' => 'sometimes|string|max:100',
'default_datastore' => 'sometimes|string|max:100',
'name' => 'sometimes|nullable|string|max:120',
'region' => 'sometimes|nullable|string|max:60',
'tags' => 'sometimes|array',
'tags.*' => 'string|max:40',
]);
if ($validator->fails()) {
return response()->json([
'error' => true,
'message' => $validator->errors()->first(),
], 422);
}
$patch = $validator->validated();
// active_vcenter_id is read-only — strip if client sent it
unset($patch['active_vcenter_id']);
$merged = $this->service->merge($patch);
$merged['active_vcenter_id'] = vCenter::where('status', 1)->value('id');
return response()->json([
'error' => false,
'data' => $merged,
]);
}
}