Pārlūkot izejas kodu

FD3-755 se corrige insert en tabla hosts de kea

Espinoza Guillermo 6 gadi atpakaļ
vecāks
revīzija
3add1f26b3

+ 79 - 71
src/HostBundle/EventListener/KEAHostReservationSubscriber.php

@@ -66,82 +66,90 @@ class KEAHostReservationSubscriber implements EventSubscriber
      */
     public function execute(LifecycleEventArgs $args, $remove = false)
     {
-        $entity = $args->getEntity();
-        if ($entity instanceof Host) {
-            $query = "SELECT COUNT(*) AS `exists`
-                    FROM information_schema.tables 
-                    WHERE table_schema = 'kea' 
-                    AND table_name = 'hosts';";
-            
-            $databaseConnection = $this->serviceContainer->get('database_connection');
-            $stmt = $databaseConnection->prepare($query);
-            $stmt->execute();
-            $tableExists = $stmt->fetchAll();
-            if (!isset($tableExists[0]['exists']) || $tableExists[0]['exists'] == '0') {
-                return;
-            }
-            
-            if ($entity->getFixedAddress() && $remove == false) {
-                $query = "
-                DELETE FROM kea.hosts
-                WHERE dhcp_identifier = :mac
-                ;
-                INSERT INTO kea.hosts (dhcp_identifier,
-                dhcp_identifier_type,
-                dhcp4_subnet_id,
-                ipv4_address, 
-                hostname)
-                VALUES (:mac,
-                (SELECT type FROM kea.host_identifier_type WHERE name=:type),
-                :dhcp4_subnet_id,
-                INET_ATON(:ip),
-                :hostname)
-                ON DUPLICATE KEY UPDATE
-                dhcp_identifier_type=(SELECT type FROM kea.host_identifier_type WHERE name=:type),
-                dhcp4_subnet_id=:dhcp4_subnet_id, ipv4_address=INET_ATON(:ip),
-                hostname=:hostname
-                ;
-                ";
-            } else {
-                $query = "
-                DELETE FROM kea.hosts
-                WHERE dhcp_identifier = :mac;
-                ";
-            }
-            
-            $databaseConnection = $this->serviceContainer->get('database_connection');
-            $stmt = $databaseConnection->prepare($query);
-            $hostType = $entity->getHostType();
-            $shortname = $hostType->getShortname();
-            // calculo el campo dhcp_identifier_type de kea.hosts
-            if ($shortname == 'cablemodem' || $shortname == 'cm') {
-                $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
-            } else {
-                // mta|cpe
-                $hex = '';
-                $prefix = $shortname . '-';
-                for ($i = 0; $i < strlen($prefix); $i++) {
-                    $ord = ord($prefix[$i]);
-                    $hexCode = dechex($ord);
-                    $hex .= substr('0'.$hexCode, -2);
+        try {
+            $entity = $args->getEntity();
+            if ($entity instanceof Host) {
+                $query = "SELECT COUNT(*) AS `exists`
+                FROM information_schema.tables 
+                WHERE table_schema = 'kea' 
+                AND table_name = 'hosts';";
+                
+                $databaseConnection = $this->serviceContainer->get('database_connection');
+                $stmt = $databaseConnection->prepare($query);
+                $stmt->execute();
+                $tableExists = $stmt->fetchAll();
+                if (!isset($tableExists[0]['exists']) || $tableExists[0]['exists'] == '0') {
+                    return;
                 }
-                $stmt->bindValue("mac", hex2bin($hex . str_replace(':', '', $entity->getHost()->getMac())));
-            }
-            if ($entity->getFixedAddress() && $remove == false) {
-                if ($shortname == 'cablemodem' || $shortname == 'cm') {
-                    $stmt->bindValue("type", 'hw-address');
+                
+                if ($entity->getFixedAddress() && $remove == false) {
+                    $query = "
+                    DELETE FROM kea.hosts
+                    WHERE dhcp_identifier = :mac
+                    ;
+                    INSERT INTO kea.hosts (dhcp_identifier,
+                    dhcp_identifier_type,
+                    dhcp4_subnet_id,
+                    ipv4_address, 
+                    hostname)
+                    VALUES (:mac,
+                    (SELECT type FROM kea.host_identifier_type WHERE name=:type),
+                    :dhcp4_subnet_id,
+                    INET_ATON(:ip),
+                    :hostname)
+                    ON DUPLICATE KEY UPDATE
+                    dhcp_identifier_type=(SELECT type FROM kea.host_identifier_type WHERE name=:type),
+                    dhcp4_subnet_id=:dhcp4_subnet_id, ipv4_address=INET_ATON(:ip),
+                    hostname=:hostname
+                    ;
+                    ";
                 } else {
-                    $stmt->bindValue("type", 'flex-id');
+                    $query = "
+                    DELETE FROM kea.hosts
+                    WHERE dhcp_identifier = :mac;
+                    ";
                 }
-                $stmt->bindValue("hostname", $entity->getHostName());
-                $stmt->bindValue("ip", $entity->getFixedAddress());
                 
-                // calculo el valor de la subnet según la config de KEA
-                $subnet = $this->serviceContainer->get('dhcp.host_service')->getSubnet($entity->getFixedAddress());
-                $stmt->bindValue("dhcp4_subnet_id", $this->serviceContainer->get('kea.config')->getSubnetInConfig($subnet));
+                $databaseConnection = $this->serviceContainer->get('database_connection');
+                $stmt = $databaseConnection->prepare($query);
+                $hostType = $entity->getHostType();
+                $shortname = $hostType->getShortname();
+                // calculo el campo dhcp_identifier_type de kea.hosts
+                if ($shortname == 'cablemodem' || $shortname == 'cm') {
+                    $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
+                } else {
+                    // mta|cpe
+                    $hex = '';
+                    $prefix = $shortname . '-';
+                    for ($i = 0; $i < strlen($prefix); $i++) {
+                        $ord = ord($prefix[$i]);
+                        $hexCode = dechex($ord);
+                        $hex .= substr('0'.$hexCode, -2);
+                    }
+                    $stmt->bindValue("mac", hex2bin($hex . str_replace(':', '', $entity->getHost()->getMac())));
+                }
+                if ($entity->getFixedAddress() && $remove == false) {
+                    if ($shortname == 'cablemodem' || $shortname == 'cm') {
+                        $stmt->bindValue("type", 'hw-address');
+                    } else {
+                        $stmt->bindValue("type", 'flex-id');
+                    }
+                    $stmt->bindValue("hostname", $entity->getHostName());
+                    $stmt->bindValue("ip", $entity->getFixedAddress());
+                    
+                    // calculo el valor de la subnet según la config de KEA
+                    $subnet = $this->serviceContainer->get('dhcp.host_service')->getSubnet($entity->getFixedAddress());
+                    $stmt->bindValue("dhcp4_subnet_id", $this->serviceContainer->get('kea.config')->getSubnetInConfig($subnet));
+                }
+                $stmt->execute();
             }
-            $stmt->execute();
-        }
+        } catch (\Exception $ex) {
+            $flashbag = $this->serviceContainer->get('session')->getFlashBag();
+            $flashbag->add("error", $ex->getMessage());
+        } catch (\Throwable $ex) {
+            $flashbag = $this->serviceContainer->get('session')->getFlashBag();
+            $flashbag->add("error", $ex->getMessage());
+        }    
     }
 
 }

+ 5 - 3
src/HostBundle/Resources/views/CRUD/edit.html.twig

@@ -8,13 +8,13 @@
 
 $(document).ready(function() {
 
-    showHostField();
+    showHostField(false);
 
     $("select[id$='hostType']").on('change', showHostField);
     
 });
 
-function showHostField()
+function showHostField(value)
 {
     var $hostTypeField = $("select[id$='hostType'] :selected");
     var $hostField = $("div.form-group[id$='host']");
@@ -25,7 +25,9 @@ function showHostField()
         $hostField.show();
     }
     
-    updateFixedIPs();
+    if (value !== false) {
+        updateFixedIPs();
+    }
 
     return false;
 }