Skip to content
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

Allow importing of multiple eggs at once #115

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions app/Filament/Resources/EggResource/Pages/ListEggs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ protected function getHeaderActions(): array
Actions\CreateAction::make(),

Actions\Action::make('import')
->label('Import Egg')
->label('Import')
->form([
Forms\Components\FileUpload::make('egg')
->acceptedFileTypes(['application/json'])
->storeFiles(false),
->storeFiles(false)
->multiple(),
])
->action(function (array $data): void {
/** @var TemporaryUploadedFile $eggFile */
Expand All @@ -35,7 +36,7 @@ protected function getHeaderActions(): array
$eggImportService = resolve(EggImporterService::class);

try {
$newEgg = $eggImportService->handle($eggFile);
$eggImportService->handle($eggFile);
} catch (Exception $exception) {
Notification::make()
->title('Egg Import Failed')
Expand All @@ -48,11 +49,9 @@ protected function getHeaderActions(): array
}

Notification::make()
->title("Egg Import Success: $newEgg->name")
->title('Egg Import Success')
->success()
->send();

redirect()->route('filament.admin.resources.eggs.edit', [$newEgg]);
}),
];
}
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/ServerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public static function form(Form $form): Form

// Do not add non numerical ports
$update = true;

continue;
}

Expand All @@ -217,7 +218,7 @@ public static function form(Form $form): Form
}

$start = max((int) $start, 0);
$end = min((int) $end, 2**16-1);
$end = min((int) $end, 2 ** 16 - 1);
for ($i = $start; $i <= $end; $i++) {
$ports->push($i);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function ipAddresses(): array
$ips = collect();
if (is_ip($this->fqdn)) {
$ips->push($this->fqdn);
} else if ($dnsRecords = gethostbynamel($this->fqdn)) {
} elseif ($dnsRecords = gethostbynamel($this->fqdn)) {
$ips->concat($dnsRecords);
}

Expand Down
37 changes: 21 additions & 16 deletions app/Services/Eggs/Sharing/EggImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use App\Models\Egg;
use Illuminate\Http\UploadedFile;
use App\Models\EggVariable;
use Illuminate\Database\ConnectionInterface;
use App\Services\Eggs\EggParserService;
Expand All @@ -21,25 +20,31 @@ public function __construct(protected ConnectionInterface $connection, protected
*
* @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable
*/
public function handle(UploadedFile $file): Egg
public function handle(array $files): array
{
$parsed = $this->parser->handle($file);
$eggs = [];

return $this->connection->transaction(function () use ($parsed) {
$egg = (new Egg())->forceFill([
'uuid' => Uuid::uuid4()->toString(),
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);
foreach ($files as $file) {
$parsed = $this->parser->handle($file);

$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();
$egg = $this->connection->transaction(function () use ($parsed) {
$egg = (new Egg())->forceFill([
'uuid' => Uuid::uuid4()->toString(),
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);

foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}
$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();

return $egg;
});
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}

return $egg;
});
}

return $eggs;
}
}
12 changes: 2 additions & 10 deletions resources/views/filament/pages/dashboard.blade.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
<x-filament-panels::page>

<x-filament::tabs label="Content tabs">
<x-filament::tabs.item disabled>Panel's Resources: </x-filament::tabs.item>
<x-filament::tabs disabled>
<x-filament::tabs.item disabled>Overview: </x-filament::tabs.item>

<x-filament::tabs.item
icon="tabler-server-2"
:active="$activeTab === 'nodes'"
wire:click="$set('activeTab', 'nodes')"
>
Nodes
<x-slot name="badge">{{ $nodesCount }}</x-slot>
</x-filament::tabs.item>

<x-filament::tabs.item
icon="tabler-brand-docker"
:active="$activeTab === 'servers'"
wire:click="$set('activeTab', 'servers')"
>
Servers
<x-slot name="badge">{{ $serversCount }}</x-slot>
</x-filament::tabs.item>

<x-filament::tabs.item
icon="tabler-eggs"
:active="$activeTab === 'eggs'"
wire:click="$set('activeTab', 'eggs')"
>
Eggs
<x-slot name="badge">{{ $eggsCount }}</x-slot>
</x-filament::tabs.item>

<x-filament::tabs.item
icon="tabler-users"
:active="$activeTab === 'users'"
wire:click="$set('activeTab', 'users')"
>
Users
<x-slot name="badge">{{ $usersCount }}</x-slot>
Expand Down
Loading