Pull ip addresses into selector
This commit is contained in:
		
							parent
							
								
									89b6f70cde
								
							
						
					
					
						commit
						2812129d00
					
				| @ -171,23 +171,11 @@ class ServerResource extends Resource | ||||
|                             ->whereNull('server_id'), | ||||
|                     ) | ||||
|                     ->createOptionForm(fn (Forms\Get $get) => [ | ||||
|                         Forms\Components\TextInput::make('allocation_ip') | ||||
|                             ->ipv4() | ||||
|                             ->datalist(function () use ($get) { | ||||
|                                 $node = Node::find($get('node_id')); | ||||
|                                 if (is_ip($node->fqdn)) { | ||||
|                                     return [$node->fqdn]; | ||||
|                                 } | ||||
| 
 | ||||
|                                 $validRecords = gethostbynamel($node->fqdn); | ||||
|                                 if (!$validRecords) { | ||||
|                                     return []; | ||||
|                                 } | ||||
| 
 | ||||
|                                 return $validRecords ?: []; | ||||
|                             }) | ||||
|                         Forms\Components\Select::make('allocation_ip') | ||||
|                             ->options(Node::find($get('node_id'))?->ipAddresses() ?? []) | ||||
|                             ->label('IP Address') | ||||
|                             ->helperText("Usually your machine's public IP unless you are port forwarding.") | ||||
|                             ->selectablePlaceholder(false) | ||||
|                             ->required(), | ||||
|                         Forms\Components\TextInput::make('allocation_alias') | ||||
|                             ->label('Alias') | ||||
|  | ||||
| @ -300,4 +300,25 @@ class Node extends Model | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     public function ipAddresses(): array | ||||
|     { | ||||
|         return cache()->remember("nodes.$this->id.servers", now()->addHour(), function () { | ||||
|             $ips = collect(); | ||||
|             if (is_ip($this->fqdn)) { | ||||
|                 $ips = $ips->add($this->fqdn); | ||||
|             } else if ($dnsRecords = gethostbynamel($this->fqdn)) { | ||||
|                 $ips = $ips->merge($dnsRecords); | ||||
|             } | ||||
| 
 | ||||
|             try { | ||||
|                 $addresses = Http::daemon($this)->connectTimeout(1)->timeout(1)->get('/api/system/ips')->json(); | ||||
|                 $ips = $ips->merge(fluent($addresses)->get('ip_addresses')); | ||||
|             } catch (Exception) { | ||||
|                 // pass
 | ||||
|             } | ||||
| 
 | ||||
|             return $ips->all(); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Lance Pioch
						Lance Pioch