Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Filament/Admin/Resources/Eggs/Pages/ListEggs.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function table(Table $table): Table
->successRedirectUrl(fn (Egg $replica) => EditEgg::getUrl(['record' => $replica])),
])
->toolbarActions([
CreateAction::make(),
ImportEggAction::make()
->multiple(),
CreateAction::make(),
BulkActionGroup::make([
DeleteBulkAction::make()
->before(function (Collection &$records) {
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Admin/Resources/Plugins/PluginResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public static function table(Table $table): Table
}),
]),
])
->headerActions([
->toolbarActions([
Action::make('import_from_file')
->hiddenLabel()
->tooltip(trans('admin/plugin.import_from_file'))
Expand Down
11 changes: 11 additions & 0 deletions app/Filament/Admin/Resources/Servers/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enums\TablerIcon;
use App\Filament\Admin\Resources\Servers\ServerResource;
use App\Filament\Components\Actions\DeleteServerIcon;
use App\Filament\Components\Actions\ExportServerConfigAction;
use App\Filament\Components\Actions\PreviewStartupAction;
use App\Filament\Components\Forms\Fields\MonacoEditor;
use App\Filament\Components\Forms\Fields\StartupVariable;
Expand Down Expand Up @@ -1004,6 +1005,16 @@ protected function getDefaultTabs(): array
->hiddenLabel()
->hint(new HtmlString(trans('admin/server.transfer_help'))),
]),
Grid::make()
->columnSpan(3)
->schema([
Actions::make([
ExportServerConfigAction::make(),
])->fullWidth(),
ToggleButtons::make('export_help')
->hiddenLabel()
->hint(trans('admin/server.import_export.export_description')),
]),
Grid::make()
->columnSpan(3)
->schema([
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Admin/Resources/Servers/Pages/ListServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\TablerIcon;
use App\Filament\Admin\Resources\Servers\ServerResource;
use App\Filament\Components\Actions\ImportServerConfigAction;
use App\Filament\Server\Pages\Console;
use App\Models\Server;
use App\Traits\Filament\CanCustomizeHeaderActions;
Expand Down Expand Up @@ -99,6 +100,7 @@ public function table(Table $table): Table
])
->toolbarActions([
CreateAction::make(),
ImportServerConfigAction::make(),
])
->searchable()
->emptyStateIcon(TablerIcon::BrandDocker)
Expand Down
60 changes: 60 additions & 0 deletions app/Filament/Components/Actions/ExportServerConfigAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Filament\Components\Actions;

use App\Models\Server;
use App\Services\Servers\Sharing\ServerConfigExporterService;
use Filament\Actions\Action;
use Filament\Forms\Components\Toggle;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\IconSize;

class ExportServerConfigAction extends Action
{
public static function getDefaultName(): ?string
{
return 'export_config';
}

protected function setUp(): void
{
parent::setUp();

$this->label(trans('filament-actions::export.modal.actions.export.label'));

$this->iconSize(IconSize::ExtraLarge);

$this->authorize(fn () => user()?->can('view server'));

$this->modalHeading(fn (Server $server) => trans('admin/server.import_export.export_heading', ['name' => $server->name]));

$this->modalDescription(trans('admin/server.import_export.export_description'));

$this->modalFooterActionsAlignment(Alignment::Center);

$this->schema([
Toggle::make('include_description')
->label(trans('admin/server.import_export.include_description'))
->helperText(trans('admin/server.import_export.include_description_help'))
->default(true),
Toggle::make('include_allocations')
->label(trans('admin/server.import_export.include_allocations'))
->helperText(trans('admin/server.import_export.include_allocations_help'))
->default(true),
Toggle::make('include_variable_values')
->label(trans('admin/server.import_export.include_variables'))
->helperText(trans('admin/server.import_export.include_variables_help'))
->default(true),
]);

$this->action(fn (ServerConfigExporterService $service, Server $server, array $data) => response()->streamDownload(
function () use ($service, $server, $data) {
echo $service->handle($server, $data);
},
'server-' . str($server->name)->kebab()->lower()->trim() . '.yaml',
[
'Content-Type' => 'application/x-yaml',
]
));
}
}
87 changes: 87 additions & 0 deletions app/Filament/Components/Actions/ImportServerConfigAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Filament\Components\Actions;

use App\Exceptions\Service\InvalidFileUploadException;
use App\Services\Servers\Sharing\ServerConfigCreatorService;
use Filament\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Illuminate\Http\UploadedFile;

class ImportServerConfigAction extends Action
{
public static function getDefaultName(): ?string
{
return 'import_config';
}

protected function setUp(): void
{
parent::setUp();

$this->hiddenLabel();

$this->icon('tabler-file-import');

$this->tooltip(trans('admin/server.import_export.import_tooltip'));

$this->authorize(fn () => user()?->can('create server'));

$this->modalHeading(trans('admin/server.import_export.import_heading'));

$this->modalDescription(trans('admin/server.import_export.import_description'));

$this->schema([
FileUpload::make('file')
->label(trans('admin/server.import_export.config_file'))
->hint(trans('admin/server.import_export.config_file_hint'))
->acceptedFileTypes(['application/x-yaml', 'text/yaml', 'text/x-yaml', '.yaml', '.yml'])
->preserveFilenames()
->previewable(false)
->storeFiles(false)
->required()
->maxSize(1024), // 1MB max
Select::make('node_id')
->label(trans('admin/server.import_export.node_select'))
->hint(trans('admin/server.import_export.node_select_hint'))
->options(fn () => user()?->accessibleNodes()->pluck('name', 'id') ?? [])
->searchable()
->required()
->visible(fn () => (user()?->accessibleNodes()->count() ?? 0) > 1),
]);

$this->action(function (ServerConfigCreatorService $createService, array $data): void {
/** @var UploadedFile $file */
$file = $data['file'];
$nodeId = $data['node_id'] ?? user()->accessibleNodes()->first()->id;

try {
$server = $createService->fromFile($file, $nodeId);

Notification::make()
->title(trans('admin/server.notifications.import_created'))
->body(trans('admin/server.notifications.import_created_body', ['name' => $server->name]))
->success()
->send();

redirect()->route('filament.admin.resources.servers.edit', ['record' => $server]);
} catch (InvalidFileUploadException $exception) {
Notification::make()
->title(trans('admin/server.notifications.import_failed'))
->body($exception->getMessage())
->danger()
->send();
} catch (\Exception $exception) {
Notification::make()
->title(trans('admin/server.notifications.import_failed'))
->body(trans('admin/server.notifications.import_failed_body', ['error' => $exception->getMessage()]))
->danger()
->send();

report($exception);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers\Api\Application\Servers;

use App\Exceptions\Service\InvalidFileUploadException;
use App\Http\Controllers\Api\Application\ApplicationApiController;
use App\Http\Requests\Api\Application\Servers\GetServerRequest;
use App\Models\Server;
use App\Services\Servers\Sharing\ServerConfigCreatorService;
use App\Services\Servers\Sharing\ServerConfigExporterService;
use App\Transformers\Api\Application\ServerTransformer;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

#[Group('Server Config', weight: 1)]
class ServerConfigController extends ApplicationApiController
{
public function __construct(
private ServerConfigExporterService $exporterService,
private ServerConfigCreatorService $creatorService
) {
parent::__construct();
}

/**
* Export server configuration
*
* Export a server's configuration to YAML format. Returns the configuration as a
* downloadable YAML file containing settings, limits, allocations, and variable values.
*/
public function export(GetServerRequest $request, Server $server): Response
{
$options = [
'include_description' => $request->boolean('include_description', true),
'include_allocations' => $request->boolean('include_allocations', true),
'include_variable_values' => $request->boolean('include_variable_values', true),
];

$yaml = $this->exporterService->handle($server, $options);

$filename = 'server-config-' . str($server->name)->kebab()->lower()->trim() . '.yaml';

return response($yaml, 200, [
'Content-Type' => 'application/x-yaml',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}

/**
* Create server from configuration
*
* Create a new server from a YAML configuration file. The configuration must
* include a valid egg UUID that exists in the system. Optionally specify a
* node_id to create the server on a specific node.
*
* @throws InvalidFileUploadException
*/
public function create(Request $request): JsonResponse
{
$request->validate([
'file' => 'required|file|mimes:yaml,yml|max:1024',
'node_id' => 'required|integer|exists:nodes,id',
]);

$file = $request->file('file');
$nodeId = $request->input('node_id');

$server = $this->creatorService->fromFile($file, $nodeId);

return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->respond(201);
}
}
Loading