diff --git a/api.php b/api.php index c1a8f49..091d85b 100644 --- a/api.php +++ b/api.php @@ -1750,6 +1750,48 @@ switch ($action) { break; } dbSaveDevice($companyId, $device); + // Auto-IPAM: varaa hallintaosoite laitteelle IPAM:iin + $mgmtIp = $device['hallintaosoite']; + if ($mgmtIp) { + // Normalisoi: poista mahdollinen /32 suffix + $cleanIp = explode('/', $mgmtIp)[0]; + // Etsi löytyykö jo IPAM:ista + $ipamEntries = dbLoadIpam($companyId); + $existing = null; + foreach ($ipamEntries as $ie) { + $ieClean = explode('/', $ie['verkko'])[0]; + if ($ieClean === $cleanIp && $ie['tyyppi'] === 'ip') { + $existing = $ie; + break; + } + } + if ($existing) { + // Päivitä olemassa oleva: merkitse varatuksi laitteelle + $existing['tila'] = 'varattu'; + $existing['nimi'] = $device['nimi']; + $existing['site_id'] = $device['site_id']; + $existing['muokattu'] = date('Y-m-d H:i:s'); + $existing['muokkaaja'] = currentUser(); + dbSaveIpam($companyId, $existing); + } else { + // Luo uusi IPAM-merkintä + $newIpam = [ + 'id' => generateId(), + 'tyyppi' => 'ip', + 'nimi' => $device['nimi'], + 'verkko' => $cleanIp, + 'vlan_id' => null, + 'site_id' => $device['site_id'], + 'tila' => 'varattu', + 'asiakas' => '', + 'lisatiedot' => 'Automaattinen varaus laitteelta: ' . $device['nimi'], + 'luotu' => date('Y-m-d H:i:s'), + 'muokattu' => null, + 'muokkaaja' => currentUser(), + ]; + dbSaveIpam($companyId, $newIpam); + } + } dbAddLog($companyId, currentUser(), $isNew ? 'device_create' : 'device_update', $device['id'], $device['nimi'], ($isNew ? 'Lisäsi' : 'Muokkasi') . ' laitteen'); echo json_encode($device); break;