This commit is contained in:
baodev0
2026-06-22 08:43:32 +07:00
parent 3243473843
commit 0c436153c6
22 changed files with 1389 additions and 95 deletions
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin\Provider;
use App\Http\Controllers\Controller;
use App\Services\VirtualMachine\VmActiveProviderService;
use Illuminate\Http\JsonResponse;
use Throwable;
/**
* Admin endpoints to toggle which row in the
* vcenter / datacenter / host / datastore / network chain is active.
*
* Only ONE row per parent scope can be active at a time — enforced by
* VmActiveProviderService::activate() inside a transaction. Deactivating
* a row cascade-deactivates its descendants.
*/
class ActiveInfraController extends Controller
{
public function __construct(private readonly VmActiveProviderService $service)
{
}
/** GET /administrator/provider/active-chain — returns the current active chain (or error). */
public function chain(): JsonResponse
{
try {
$chain = $this->service->resolveChain();
} catch (Throwable $e) {
return response()->json([
'error' => true,
'message' => $e->getMessage(),
], 422);
}
return response()->json([
'error' => false,
'data' => [
'vcenter' => $chain['vcenter']->only(['id', 'host', 'user', 'status']),
'datacenter' => $chain['datacenter']->only(['id', 'name', 'datacenter_id', 'status']),
'host' => $chain['host']->only(['id', 'name', 'host_id', 'status']),
'datastore' => $chain['datastore']->only(['id', 'name', 'datastore', 'status']),
'network' => $chain['network']->only(['id', 'name', 'network', 'status']),
],
]);
}
/** POST /administrator/provider/{type}/{id}/activate */
public function activate(string $type, int $id): JsonResponse
{
try {
$row = $this->service->activate($type, $id);
} catch (Throwable $e) {
return response()->json(['error' => true, 'message' => $e->getMessage()], 422);
}
return response()->json([
'error' => false,
'data' => $row,
]);
}
/** POST /administrator/provider/{type}/{id}/deactivate */
public function deactivate(string $type, int $id): JsonResponse
{
try {
$row = $this->service->deactivate($type, $id);
} catch (Throwable $e) {
return response()->json(['error' => true, 'message' => $e->getMessage()], 422);
}
return response()->json([
'error' => false,
'data' => $row,
]);
}
}
@@ -0,0 +1,73 @@
<?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,
]);
}
}