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

80 lines
2.5 KiB
PHP

<?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,
]);
}
}