KEA Setup
All checks were successful
Deploy / deploy-staging (push) Successful in 6s
Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-04-13 02:13:43 +02:00
parent 4d73eec687
commit 677f9314f5
8 changed files with 285 additions and 73 deletions

View File

@@ -7,11 +7,14 @@ use PDO;
/**
* Repository für das KEA DHCP Host-Management.
* Interagiert direkt mit der 'hosts' Tabelle in der PostgreSQL-Datenbank.
* Interagiert direkt mit der 'hosts' Tabelle in der KEA-Datenbank.
*/
final class KeaHostRepository
{
public function __construct(private PDO $pdo) {}
public function __construct(
private PDO $pdo,
private ?KeaHostMetadataRepository $metadata = null
) {}
/**
* Ruft eine Liste aller Host-Reservierungen ab (Geräte-Inventar).
@@ -29,7 +32,7 @@ final class KeaHostRepository
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
return $this->withMetadata($stmt->fetchAll(PDO::FETCH_ASSOC));
} catch (\PDOException $e) {
if ($this->isMissingTable($e)) {
throw new \RuntimeException(
@@ -85,28 +88,45 @@ final class KeaHostRepository
* @param string $ip IPv4-Adresse
* @param int $subnetId Die ID des Subnets (dhcp4_subnet_id), in dem die IP liegt
* @param string|null $hostname Optionaler Hostname
* @param array $metadata Zusätzliche Infos (Standort, Verantwortlicher etc.) für 'user_context'
* @param array $metadata Zusätzliche Infos fuer die separate Nexus-DHCP-Metadatenbank.
*/
public function create(string $mac, string $ip, int $subnetId, ?string $hostname = null, array $metadata = []): int
{
// Metadaten werden im KEA-Standardfeld 'user_context' als JSON gespeichert
$userContextJson = json_encode($metadata, JSON_THROW_ON_ERROR);
// dhcp_identifier_type 1 = HW_ADDRESS (Ethernet)
$stmt = $this->pdo->prepare(
'INSERT INTO hosts (dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, ipv4_address, hostname, user_context)
VALUES (:mac, 1, :subnetId, :ip, :hostname, :user_context)'
'INSERT INTO hosts (dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, ipv4_address, hostname)
VALUES (:mac, 1, :subnetId, :ip, :hostname)'
);
$stmt->execute([
'mac' => $mac,
'subnetId' => $subnetId,
'ip' => $ip,
'hostname' => $hostname,
'user_context' => $userContextJson,
'mac' => $mac,
'subnetId' => $subnetId,
'ip' => $ip,
'hostname' => $hostname,
]);
return $this->lastInsertIdSafe();
$hostId = $this->lastInsertIdSafe();
if ($this->metadata !== null && $metadata !== []) {
$this->metadata->saveForHost($hostId, $mac, $ip, $metadata);
}
return $hostId;
}
private function withMetadata(array $hosts): array
{
if ($hosts === [] || $this->metadata === null) {
return $hosts;
}
$metadataByHost = $this->metadata->findByHostIds(array_column($hosts, 'host_id'));
foreach ($hosts as &$host) {
$hostId = (int)($host['host_id'] ?? 0);
$host['metadata'] = $metadataByHost[$hostId] ?? [];
}
unset($host);
return $hosts;
}
private function driver(): string