Add bulk IP update action for node allocations (#1845)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: notAreYouScared <1757840+notAreYouScared@users.noreply.github.com>
Co-authored-by: Charles <charles@pelican.dev>
This commit is contained in:
Copilot 2025-11-08 16:53:12 -05:00 committed by GitHub
parent 1ff965611e
commit b06df23823
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 121 additions and 4 deletions

View File

@ -3,6 +3,7 @@
namespace App\Filament\Admin\Resources\Nodes\RelationManagers;
use App\Filament\Admin\Resources\Servers\Pages\CreateServer;
use App\Filament\Components\Actions\UpdateNodeAllocations;
use App\Models\Allocation;
use App\Models\Node;
use App\Services\Allocations\AssignmentService;
@ -80,7 +81,9 @@ class AllocationsRelationManager extends RelationManager
->searchable()
->label(trans('admin/node.table.ip')),
])
->headerActions([
->toolbarActions([
DeleteBulkAction::make()
->authorize(fn () => user()?->can('update', $this->getOwnerRecord())),
Action::make('create new allocation')
->label(trans('admin/node.create_allocation'))
->schema(fn () => [
@ -118,9 +121,8 @@ class AllocationsRelationManager extends RelationManager
->required(),
])
->action(fn (array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord(), $data)),
])
->groupedBulkActions([
DeleteBulkAction::make()
UpdateNodeAllocations::make()
->nodeRecord($this->getOwnerRecord())
->authorize(fn () => user()?->can('update', $this->getOwnerRecord())),
]);
}

View File

@ -0,0 +1,106 @@
<?php
namespace App\Filament\Components\Actions;
use App\Models\Allocation;
use App\Models\Node;
use Exception;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
class UpdateNodeAllocations extends Action
{
public static function getDefaultName(): ?string
{
return 'bulk_update_ip';
}
protected function setUp(): void
{
parent::setUp();
$this->label(trans('admin/node.bulk_update_ip'));
$this->icon('tabler-replace');
$this->color('warning');
$this->requiresConfirmation();
$this->modalHeading(trans('admin/node.bulk_update_ip'));
$this->modalDescription(trans('admin/node.bulk_update_ip_description'));
$this->modalIconColor('warning');
$this->modalSubmitActionLabel(trans('admin/node.update_ip'));
$this->schema(function () {
/** @var Node $node */
$node = $this->record;
$currentIps = Allocation::where('node_id', $node->id)
->pluck('ip')
->unique()
->values()
->all();
return [
Select::make('old_ip')
->label(trans('admin/node.old_ip'))
->options(array_combine($currentIps, $currentIps))
->selectablePlaceholder(false)
->required()
->live(),
Select::make('new_ip')
->label(trans('admin/node.new_ip'))
->options(fn () => array_combine($node->ipAddresses(), $node->ipAddresses()) ?: [])
->required()
->different('old_ip'),
];
});
$this->action(function (array $data) {
/** @var Node $node */
$node = $this->record;
$allocations = Allocation::where('node_id', $node->id)->where('ip', $data['old_ip'])->get();
if ($allocations->count() === 0) {
Notification::make()
->title(trans('admin/node.no_allocations_to_update'))
->warning()
->send();
return;
}
$updated = 0;
$failed = 0;
foreach ($allocations as $allocation) {
try {
$allocation->update(['ip' => $data['new_ip']]);
$updated++;
} catch (Exception $exception) {
$failed++;
report($exception);
}
}
Notification::make()
->title(trans('admin/node.ip_updated', ['count' => $updated, 'total' => $allocations->count()]))
->body($failed > 0 ? trans('admin/node.ip_update_failed', ['count' => $failed]) : null)
->status($failed > 0 ? 'warning' : 'success')
->persistent()
->send();
});
}
public function nodeRecord(Node $node): static
{
$this->record = $node;
return $this;
}
}

View File

@ -140,4 +140,13 @@ return [
'title' => 'Cloudflare Issue',
'body' => 'Your Node is not accessible by Cloudflare',
],
'bulk_update_ip' => 'Update IPs',
'bulk_update_ip_description' => 'Replace an old IP address with a new one for allocations. This is useful when a node\'s IP address changes',
'update_ip' => 'Update IP',
'old_ip' => 'Old IP Address',
'new_ip' => 'New IP Address',
'no_allocations_to_update' => 'No allocations with the selected old IP address were found',
'ip_updated' => 'Successfully updated :count of :total allocation(s)',
'ip_update_failed' => ':count allocation(s) failed to update',
];