Files
Virtnet.bond/extensions/Servers/Proxmox/Admin/Resources/IPPoolResource/RelationManagers/IPAddressRelationManager.php
Joseph.Rawlings 16cd5f05de Enhance Proxmox views with improved loading states and UI updates
- Updated serverplaceholder.blade.php to enhance loading skeletons for server details.
- Added navigation breadcrumbs to statistics, subdomain, and system views for better user experience.
- Improved loading indicators for CPU, network, memory, and disk charts in statistics.blade.php.
- Refined storage.blade.php layout with section dividers and improved drive display, including bus type indicators and quota usage.
- Enhanced VNC and Xterm popups with a new header design, loading animations, and reconnect functionality.
- Added error handling for VNC and Xterm connections to inform users when unable to connect.
2026-02-24 19:51:46 -05:00

199 lines
9.5 KiB
PHP

<?php
namespace Paymenter\Extensions\Servers\Proxmox\Admin\Resources\IPPoolResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class IPAddressRelationManager extends RelationManager
{
protected static string $relationship = 'ipAddresses';
// Set label to IP Addresses
protected static ?string $label = 'IP Address';
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('type')
->label('Type')
->options([
'ipv4' => 'IPv4',
'ipv6' => 'IPv6',
])
->default('ipv4')
->disabledOn('edit')
->reactive()
->required(),
Forms\Components\TextInput::make('ip')
->label('IP Address')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->disabledOn('edit')
->unique(ignoreRecord: true)
->placeholder('Enter the IP address'),
Forms\Components\TextInput::make('gateway')
->label('Gateway')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the gateway'),
Forms\Components\TextInput::make('netmask')
->label('Netmask')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the netmask'),
Forms\Components\TextInput::make('mac_address')
->label('MAC Address')
->maxLength(255)
->placeholder('Enter the MAC address'),
Forms\Components\Select::make('server_id')
->label('Server')
->relationship('server', 'hostname')
->searchable()
->preload()
->helperText('Select a server to assign this IP address')
->placeholder('Select a server'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('ip')
->columns([
Tables\Columns\TextColumn::make('ip')->label('IP Address')->searchable(),
Tables\Columns\TextColumn::make('server.hostname')->label('Server')->sortable()->searchable(),
Tables\Columns\TextColumn::make('gateway'),
Tables\Columns\TextColumn::make('mac_address')->label('MAC Address'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\Action::make('bulkaddaddress')
->label('Bulk Add IP Addresses')
->icon('ri-add-line')
->form(function () {
return [
Forms\Components\Select::make('type')
->label('Type')
->options([
'ipv4' => 'IPv4',
'ipv6' => 'IPv6',
])
->default('ipv4')
->reactive()
->required(),
Forms\Components\TextInput::make('start_ip')
->label('Start IP Address')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the start IP address'),
Forms\Components\TextInput::make('end_ip')
->label('End IP Address')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the end IP address'),
Forms\Components\TextInput::make('gateway')
->label('Gateway')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the gateway'),
Forms\Components\TextInput::make('netmask')
->label('Netmask')
->required()
->maxLength(fn (Get $get) => $get('type') === 'ipv4' ? 15 : 39) // 15 for IPv4, 39 for IPv6
->placeholder('Enter the netmask'),
];
})
->action(function (array $data, $record) {
// Get start IP and end IP
if ($data['type'] === 'ipv4') {
// Validate and convert IP addresses
$startIp = ip2long($data['start_ip']);
$endIp = ip2long($data['end_ip']);
if ($startIp === false || $endIp === false || $startIp > $endIp) {
throw new \InvalidArgumentException('Invalid IP address range.');
}
// Generate IP addresses in the range
$ipAddresses = [];
for ($ip = $startIp; $ip <= $endIp; $ip++) {
$ipAddresses[] = long2ip($ip);
}
} else {
$startIp = gmp_import(inet_pton($data['start_ip']));
$endIp = gmp_import(inet_pton($data['end_ip']));
if ($startIp === false || $endIp === false || gmp_cmp($startIp, $endIp) > 0) {
throw new \InvalidArgumentException('Invalid IP address range.');
}
// Generate IPv6 addresses in the range
$ipAddresses = [];
for ($ip = $startIp; gmp_cmp($ip, $endIp) <= 0; $ip = gmp_add($ip, 1)) {
$ipAddresses[] = inet_ntop(str_pad(gmp_export($ip), 16, "\0", STR_PAD_LEFT));
}
// Remove duplicate IP addresses
$ipAddresses = array_unique($ipAddresses);
sort($ipAddresses);
}
// Create IP addresses in the pool
$created = 0;
$skipped = 0;
foreach ($ipAddresses as $ip) {
// Skip if IP already exists in this pool
if ($this->getOwnerRecord()->ipAddresses()->where('ip', $ip)->exists()) {
$skipped++;
continue;
}
$this->getOwnerRecord()->ipAddresses()->create([
'type' => $data['type'],
'ip' => $ip,
'gateway' => $data['gateway'],
'netmask' => $data['netmask'],
]);
$created++;
}
if ($skipped > 0) {
Notification::make()
->title('Bulk Add Complete')
->body("Created {$created} IPs, skipped {$skipped} duplicates.")
->warning()
->send();
}
$this->dispatch('refresh');
}),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()->disabled(fn ($record) => $record->server_id !== null),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make()->before(function (Tables\Actions\DeleteBulkAction $action) {
foreach ($action->getRecords() as $record) {
if ($record->server_id !== null) {
Notification::make()
->title('Cannot delete IP Address')
->body('The IP Address is assigned to a server.')
->duration(5000)
->icon('ri-error-warning-line')
->danger()
->send();
$action->cancel();
}
}
}),
]),
])->defaultSort('server_id', 'desc');
}
}