Explorar o código

FD3-660 Se muestran las fixed address según el hostType

Espinoza Guillermo %!s(int64=6) %!d(string=hai) anos
pai
achega
aff1d2c0ca

+ 37 - 0
src/HostBundle/Controller/HostController.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace HostBundle\Controller;
+
+use HostBundle\Entity\HostType;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+
+/**
+ * @RouteResource("Host")
+ */
+class HostController extends Controller
+{
+
+    /**
+     * @param Request $request
+     *
+     * @return JsonResponse
+     *
+     * @Route("/host/ipv4/ip_range", name="ajax_host_ipv4_range")
+     */
+    public function ajaxHostIPV4RangeAction(Request $request)
+    {
+        $em = $this->getDoctrine();
+        $hostType = $em->getRepository(HostType::class)->find($request->get('id'));
+        $ips = [];
+        if ($hostType) {
+            $ips = $this->get('dhcp.host_service')->getFreeFixedIP($hostType);
+        }
+
+        return new JsonResponse(compact('ips'));
+    }
+
+}

+ 1 - 1
src/HostBundle/Entity/Host.php

@@ -12,7 +12,7 @@ use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
 use WorkflowBundle\Entity\Traits\WorkflowTrait;
 
 /**
- * @ORM\Entity
+ * @ORM\Entity(repositoryClass="HostBundle\Repository\HostRepository")
  * @ORM\HasLifecycleCallbacks
  *
  * @UniqueEntity("mac")

+ 3 - 1
src/HostBundle/EventListener/AdminDHCPOption.php

@@ -48,7 +48,9 @@ class AdminDHCPOption
             foreach (DHCPOptions::getConstants() as $opt) {
                 
                 if ($opt == 'fixed_address') {
-                    $freeIP = $this->hostService->getFreeFixedIP();
+                    $hostType = method_exists($subject, 'getHostType') 
+                                ? $subject->getHostType() : null;
+                    $freeIP = $this->hostService->getFreeFixedIP($hostType);
                     $fixedAddress = $subject->getFixedAddress();
                     if ($fixedAddress) {
                         $freeIP[] = $fixedAddress;

+ 1 - 1
src/HostBundle/EventListener/AssignHostFixedAddressSubscriber.php

@@ -62,7 +62,7 @@ class AssignHostFixedAddressSubscriber implements EventSubscriber
             
             // Se debe asignar una IP fija ya que el host no tiene ninguna
             if ($entity->getFixedIP() == true && !$entity->getFixedAddress()) {
-                $ip = $this->serviceContainer->get('dhcp.host_service')->getFirstFreeFixedIP();
+                $ip = $this->serviceContainer->get('dhcp.host_service')->getFirstFreeFixedIP($entity->getHostType());
                 $entity->setFixedAddress($ip);
             }
                         

+ 29 - 0
src/HostBundle/Repository/HostRepository.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace HostBundle\Repository;
+
+use HostBundle\Entity\HostType;
+
+class HostRepository extends \Doctrine\ORM\EntityRepository
+{
+
+    /**
+     * @param HostType $hostType
+     *
+     * @return array
+     */
+    public function findAllFixedIPByHostType(HostType $hostType = null)
+    {
+        $qb = $this->createQueryBuilder('Host')
+            ->where('Host.fixedIP = :fixedIP')->setParameter('fixedIP', true);
+            
+        if ($hostType) {
+            $qb->join('Host.hostType', 'HostType')
+               ->andWhere('HostType.id = :id')->setParameter('id', $hostType->getId())
+            ;
+        }
+            
+        return $qb->getQuery()->getResult();
+    }
+
+}

+ 29 - 1
src/HostBundle/Resources/views/CRUD/edit.html.twig

@@ -11,7 +11,7 @@ $(document).ready(function() {
     showHostField();
 
     $("select[id$='hostType']").on('change', showHostField);
-
+    
 });
 
 function showHostField()
@@ -24,10 +24,38 @@ function showHostField()
     } else {
         $hostField.show();
     }
+    
+    updateFixedIPs();
 
     return false;
 }
 
+function updateFixedIPs()
+{
+    var $hostTypeField = $("select[id$='hostType'] :selected");
+    var $hostTypeId = $hostTypeField.val();
+    
+    if ($hostTypeId) {
+        $.ajax({
+            url: '{{ path ('ajax_host_ipv4_range') }}',
+            type: 'POST',
+            data: {
+                id: $hostTypeId,
+            },
+            success: function(data) {
+                $('select[id$="_fixed_address"]').html('<option value=""></option>');
+                if (data.ips) {
+                    $.each(data.ips, function (index, value) {
+                        $('select[id$="_fixed_address"]').append('<option value="' + value + '">' + value + '</option>');
+                    });
+                }
+                
+                return false;
+            },
+        });
+    }
+}
+
 </script>
 
 {% endblock javascripts %}

+ 16 - 10
src/HostBundle/Services/HostService.php

@@ -5,6 +5,7 @@ namespace HostBundle\Services;
 use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\EntityRepository;
 use HostBundle\Entity\Host;
+use HostBundle\Entity\HostType;
 use IPv4Bundle\Services\PoolService;
 use KeaBundle\Entity\Lease4;
 
@@ -20,7 +21,7 @@ class HostService
      * @var EntityRepository
      */
     private $hostRepository;
-    
+        
     /**
      * @var EntityRepository
      */
@@ -45,11 +46,13 @@ class HostService
     }
     
     /**
+     * @param HostType $hostType
+     *
      * @return string
      */
-    public function getFirstFreeFixedIP()
+    public function getFirstFreeFixedIP(HostType $hostType = null)
     {
-        $diff = $this->getFreeFixedIP();
+        $diff = $this->getFreeFixedIP($hostType);
         if (count($diff)) {
             return current($diff);
         }
@@ -58,26 +61,29 @@ class HostService
     }
     
     /**
+     * @param HostType $hostType
+     * @param boolean $hostFixed
+     *
      * @return array
      */
-    public function getFreeFixedIP($hostFixed = true)
+    public function getFreeFixedIP(HostType $hostType = null, $hostFixed = true)
     {
-        $range = $this->poolService->getStaticPoolIPRange();
+        $range = $this->poolService->getStaticPoolIPRange($hostType);
         $leases = $this->lease4Repository->getAllAddress();
-        $hostsFixedIP = $hostFixed ? $this->getHostsFixedIp() : [];
+        $hostsFixedIP = $hostFixed ? $this->getHostsFixedIp($hostType) : [];
         
         return array_diff($range, $leases, $hostsFixedIP);
     }
     
     /**
+     * @param HostType $hostType
+     *
      * @return array
      */
-    public function getHostsFixedIp()
+    public function getHostsFixedIp(HostType $hostType = null)
     {
         $hostsFixedIP = [];
-        $hosts = $this->hostRepository->findBy([
-            'fixedIP' => true,
-        ]);
+        $hosts = $this->hostRepository->findAllFixedIPByHostType($hostType);
         foreach ($hosts as $host) {
             if ($host->getFixedAddress()) {
                 $hostsFixedIP[] = $host->getFixedAddress();

+ 1 - 1
src/IPv4Bundle/Entity/Pool.php

@@ -14,7 +14,7 @@ use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
 use WorkflowBundle\Entity\Traits\WorkflowTrait;
 
 /**
- * @ORM\Entity
+ * @ORM\Entity(repositoryClass="IPv4Bundle\Repository\PoolRepository")
  *
  * @UniqueEntity("name")
  *

+ 31 - 0
src/IPv4Bundle/Repository/PoolRepository.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace IPv4Bundle\Repository;
+
+use HostBundle\Entity\HostType;
+use Doctrine\ORM\Query\Expr;
+
+class PoolRepository extends \Doctrine\ORM\EntityRepository
+{
+
+    /**
+     * @param HostType $hostType
+     *
+     * @return array
+     */
+    public function findAllStaticByHostType(HostType $hostType = null)
+    {
+        $qb = $this->createQueryBuilder('Pool')
+            ->where('Pool.isStatic = :isStatic')->setParameter('isStatic', true);
+            
+        if ($hostType) {
+            $qb->join('Pool.subNet', 'Subnet')
+               ->join('Subnet.allowedHostType', 'HostType')
+               ->andWhere('HostType.id = :id')->setParameter('id', $hostType->getId())
+               ;
+        }
+        
+        return $qb->getQuery()->getResult();
+    }
+
+}

+ 7 - 4
src/IPv4Bundle/Services/PoolService.php

@@ -4,6 +4,7 @@ namespace IPv4Bundle\Services;
 
 use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\EntityRepository;
+use HostBundle\Entity\HostType;
 use IPv4Bundle\Entity\Pool;
 use KeaBundle\Entity\Lease4;
 
@@ -48,14 +49,15 @@ class PoolService
     }
     
     /**
+     * @param HostType $hostType
+     *
      * @return array
      */
-    public function getStaticPoolIPRange()
+    public function getStaticPoolIPRange(HostType $hostType = null)
     {
+        $count = 1;
         $range = [];
-        $pools = $this->poolRepository->findBy([
-            'isStatic' => true,
-        ]);
+        $pools = $this->poolRepository->findAllStaticByHostType($hostType);
         foreach ($pools as $pool) {
             $range[] = $pool->getFirstIp();
             $firstIp = ip2long($pool->getFirstIp());
@@ -66,6 +68,7 @@ class PoolService
                 $currentIP++;
             }
         }
+        sort($range);
         
         return $range;
     }