105 lines
3.9 KiB
PHP
105 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreServiceSliceRequest;
|
|
use App\Models\Organisation;
|
|
use App\Models\ServiceSlice;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Response;
|
|
|
|
class ServiceSliceController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$server = $organisation->servers()->findOrFail($request->route('server'));
|
|
$service = $server->services()
|
|
->with('environment.application')
|
|
->findOrFail($request->route('service'));
|
|
|
|
return inertia('service-slices/Index', [
|
|
'server' => $server,
|
|
'service' => $service,
|
|
'slices' => $service->slices()
|
|
->with(['environment.application', 'attachments', 'operations.steps', 'operations.children.target'])
|
|
->latest()
|
|
->get(),
|
|
]);
|
|
}
|
|
|
|
public function show(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$server = $organisation->servers()->findOrFail($request->route('server'));
|
|
$service = $server->services()->with('environment.application')->findOrFail($request->route('service'));
|
|
$slice = $service->slices()
|
|
->with(['environment.application', 'attachments.environment.application', 'operations.steps', 'operations.children.target'])
|
|
->findOrFail($request->route('slice'));
|
|
|
|
return inertia('service-slices/Show', [
|
|
'server' => $server,
|
|
'service' => $service,
|
|
'slice' => $slice,
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$server = $organisation->servers()->findOrFail($request->route('server'));
|
|
$service = $server->services()->findOrFail($request->route('service'));
|
|
|
|
return inertia('service-slices/Create', [
|
|
'server' => $server,
|
|
'service' => $service,
|
|
'environments' => $organisation->applications()
|
|
->with('environments')
|
|
->get()
|
|
->pluck('environments')
|
|
->flatten()
|
|
->values(),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreServiceSliceRequest $request): RedirectResponse
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$server = $organisation->servers()->findOrFail($request->route('server'));
|
|
$service = $server->services()->findOrFail($request->route('service'));
|
|
$environmentId = $request->integer('environment_id') ?: null;
|
|
|
|
if ($environmentId !== null) {
|
|
$belongsToOrganisation = $organisation->applications()
|
|
->whereHas('environments', fn ($query) => $query->whereKey($environmentId))
|
|
->exists();
|
|
|
|
abort_unless($belongsToOrganisation, 422);
|
|
}
|
|
|
|
$config = $request->filled('config')
|
|
? json_decode($request->string('config')->toString(), true, flags: JSON_THROW_ON_ERROR)
|
|
: [];
|
|
|
|
/** @var ServiceSlice $slice */
|
|
$slice = $service->slices()->create([
|
|
'environment_id' => $environmentId,
|
|
'name' => $request->string('name')->toString(),
|
|
'type' => $request->string('type')->toString(),
|
|
'status' => $request->string('status')->toString(),
|
|
'config' => $config,
|
|
'credentials' => [],
|
|
]);
|
|
|
|
return redirect()
|
|
->route('service-slices.show', [
|
|
'organisation' => $organisation->id,
|
|
'server' => $server->id,
|
|
'service' => $service->id,
|
|
'slice' => $slice->id,
|
|
])
|
|
->with('success', 'Slice created.');
|
|
}
|
|
}
|