-
-
Notifications
You must be signed in to change notification settings - Fork 257
Allow Exporting and Importing of servers #2144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
158a5bc
21ca789
ebc5164
2ce53b2
826701c
5a56af4
adb0f12
bb4d55c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?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->tooltip(trans('admin/server.import_export.export_tooltip')); | ||
|
|
||
| $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', | ||
| ] | ||
| )); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?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 Filament\Support\Enums\IconSize; | ||
| use Illuminate\Http\UploadedFile; | ||
|
|
||
| class ImportServerConfigAction extends Action | ||
| { | ||
| public static function getDefaultName(): ?string | ||
| { | ||
| return 'import_config'; | ||
| } | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->label(trans('filament-actions::import.modal.actions.import.label')); | ||
|
|
||
| $this->iconButton(); | ||
|
|
||
| $this->icon('tabler-file-import'); | ||
|
|
||
| $this->iconSize(IconSize::ExtraLarge); | ||
|
|
||
| $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) | ||
| ->default(fn () => user()?->accessibleNodes()->first()?->id), | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]); | ||
|
|
||
| $this->action(function (ServerConfigCreatorService $createService, array $data): void { | ||
| /** @var UploadedFile $file */ | ||
| $file = $data['file']; | ||
| $nodeId = $data['node_id'] ?? null; | ||
|
|
||
| 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' => 'sometimes|integer|exists:nodes,id', | ||
| ]); | ||
|
Comment on lines
60
to
65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing authorization check for server creation. The Additionally, the Suggested fixCreate a dedicated request class with proper authorization: // app/Http/Requests/Api/Application/Servers/CreateServerFromConfigRequest.php
class CreateServerFromConfigRequest extends ApplicationApiRequest
{
protected ?string $resource = Server::RESOURCE_NAME;
protected int $permission = AdminAcl::WRITE;
public function rules(): array
{
return [
'file' => 'required|file|max:1024',
'node_id' => 'sometimes|integer|exists:nodes,id',
];
}
}Then use it in the controller: - public function create(Request $request): JsonResponse
+ public function create(CreateServerFromConfigRequest $request): JsonResponse
{
- $request->validate([
- 'file' => 'required|file|mimes:yaml,yml|max:1024',
- 'node_id' => 'sometimes|integer|exists:nodes,id',
- ]);🤖 Prompt for AI Agents |
||
|
|
||
| $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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.