From 5d2b892eabb0bfdfdd62b36806710dbb434df0c2 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 8 Dec 2024 16:19:04 -0500 Subject: [PATCH] Better IP addresses (#800) * Unique ip addresses * Only ipv4 addresses for now * Switch to selects --- .../RelationManagers/AllocationsRelationManager.php | 5 +++-- .../Admin/Resources/ServerResource/Pages/CreateServer.php | 5 ++--- .../RelationManagers/AllocationsRelationManager.php | 5 +++-- app/Models/Node.php | 5 ++++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Filament/Admin/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php b/app/Filament/Admin/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php index cf820b699..ba7ee06b0 100644 --- a/app/Filament/Admin/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php +++ b/app/Filament/Admin/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php @@ -5,6 +5,7 @@ namespace App\Filament\Admin\Resources\NodeResource\RelationManagers; use App\Models\Allocation; use App\Models\Node; use App\Services\Allocations\AssignmentService; +use Filament\Forms\Components\Select; use Filament\Forms\Components\TagsInput; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; @@ -73,8 +74,8 @@ class AllocationsRelationManager extends RelationManager ->headerActions([ Tables\Actions\Action::make('create new allocation')->label('Create Allocations') ->form(fn () => [ - TextInput::make('allocation_ip') - ->datalist($this->getOwnerRecord()->ipAddresses()) + Select::make('allocation_ip') + ->options(collect($this->getOwnerRecord()->ipAddresses())->mapWithKeys(fn (string $ip) => [$ip => $ip])) ->label('IP Address') ->inlineLabel() ->ipv4() diff --git a/app/Filament/Admin/Resources/ServerResource/Pages/CreateServer.php b/app/Filament/Admin/Resources/ServerResource/Pages/CreateServer.php index b6e9183b9..4828a473e 100644 --- a/app/Filament/Admin/Resources/ServerResource/Pages/CreateServer.php +++ b/app/Filament/Admin/Resources/ServerResource/Pages/CreateServer.php @@ -192,13 +192,12 @@ class CreateServer extends CreateRecord ->whereNull('server_id'), ) ->createOptionForm(fn (Get $get) => [ - TextInput::make('allocation_ip') - ->datalist(Node::find($get('node_id'))?->ipAddresses() ?? []) + Select::make('allocation_ip') + ->options(collect(Node::find($get('node_id'))?->ipAddresses())->mapWithKeys(fn (string $ip) => [$ip => $ip])) ->label('IP Address') ->inlineLabel() ->ipv4() ->helperText("Usually your machine's public IP unless you are port forwarding.") - // ->selectablePlaceholder(false) ->required(), TextInput::make('allocation_alias') ->label('Alias') diff --git a/app/Filament/Admin/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php b/app/Filament/Admin/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php index 522b408df..76429391f 100644 --- a/app/Filament/Admin/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php +++ b/app/Filament/Admin/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php @@ -5,6 +5,7 @@ namespace App\Filament\Admin\Resources\ServerResource\RelationManagers; use App\Models\Allocation; use App\Models\Server; use App\Services\Allocations\AssignmentService; +use Filament\Forms\Components\Select; use Filament\Forms\Components\TagsInput; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; @@ -71,8 +72,8 @@ class AllocationsRelationManager extends RelationManager CreateAction::make()->label('Create Allocation') ->createAnother(false) ->form(fn () => [ - TextInput::make('allocation_ip') - ->datalist($this->getOwnerRecord()->node->ipAddresses()) + Select::make('allocation_ip') + ->options(collect($this->getOwnerRecord()->node->ipAddresses())->mapWithKeys(fn (string $ip) => [$ip => $ip])) ->label('IP Address') ->inlineLabel() ->ipv4() diff --git a/app/Models/Node.php b/app/Models/Node.php index 7d10006f1..923c5278d 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -384,7 +384,10 @@ class Node extends Model // pass } - return $ips->all(); + // Only IPV4 + $ips = $ips->filter(fn (string $ip) => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false); + + return $ips->unique()->all(); }); } }