Bladeren bron

Merged in FD3-534 (pull request #17)

API REST para configuración de KEA

Approved-by: Guillermo Espinoza <guillermo@interlink.com.ar>
Maximiliano Schvindt 7 jaren geleden
bovenliggende
commit
4f7b2ad8c0

+ 5 - 0
app/config/routing.yml

@@ -17,6 +17,11 @@ KeaBundle:
     resource: "@KeaBundle/Resources/config/routing/admin.xml"
     prefix: /
 
+kea_config_api:
+    resource: "@KeaBundle/Controller/REST/ConfigRESTController.php"
+    type:   rest
+    prefix:   /api
+
 i_pv4_sub_net_api:
     resource: "@IPv4Bundle/Controller/REST/SubNetRESTController.php"
     type:   rest

+ 12 - 3
src/KeaBundle/Command/KeaConfigCommand.php

@@ -17,8 +17,9 @@ class KeaConfigCommand extends ContainerAwareCommand
             ->setDescription('Kea DHCP Config')
             ->setHelp('Create Kea DHCP Config')
             ->addOption('class', null, InputOption::VALUE_OPTIONAL, 'Kea Class Config in KeaBundle\Services. e.g. BaseKea', 'BaseKea')
-            ->addOption('id', null, InputOption::VALUE_OPTIONAL, 'DHCP Entity ID. e.g.: 1')
-            ->addOption('library', null, InputOption::VALUE_OPTIONAL, 'Hook library path', '/home/iksop/kea-hook-cm/kea-hook-flowdat.so')
+            ->addOption('id', null, InputOption::VALUE_OPTIONAL, 'DHCP Entity ID. e.g.: 1', 1)
+            ->addOption('library', null, InputOption::VALUE_OPTIONAL, 'Hook library path', '/kea-cm-hook/kea-hook-flowdat.so')
+            ->addOption('set', null, InputOption::VALUE_REQUIRED, 'Send Config DHCP4 to KEA', 0)
             ;
     }
 
@@ -28,13 +29,21 @@ class KeaConfigCommand extends ContainerAwareCommand
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
+        $setConfig = (int) $input->getOption('set');
+
         $this->output = $output;
         $class = $input->getOption('class');
         $id = $input->getOption('id');
         $library = $input->getOption('library');
         $keaConfigService = $this->getContainer()->get('kea.config');
 
-        $output->writeln($keaConfigService->getConfig($id, $class, $library));
+        if($setConfig) {
+            $config = $keaConfigService->getConfig($id, $class, $library);
+            $dhcp = $this->getContainer()->get("doctrine.orm.entity_manager")->getRepository("DHCPBundle\Entity\DHCP")->findOneById($id);
+            $output->writeln($keaConfigService->setConfig($dhcp, $config));
+        } else {
+            $output->writeln($keaConfigService->getConfig($id, $class, $library));
+        }
     }
 
 }

+ 81 - 0
src/KeaBundle/Controller/REST/ConfigRESTController.php

@@ -0,0 +1,81 @@
+<?php
+
+namespace KeaBundle\Controller\REST;
+
+use KeaBundle\Form\ConfigType;
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+use WebserviceBundle\Controller\RESTController;
+
+use FOS\RestBundle\Controller\Annotations\Patch;
+use FOS\RestBundle\Controller\Annotations\Get;
+use FOS\RestBundle\Controller\Annotations\View;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * Config controller.
+ * @RouteResource("Config")
+ */
+class ConfigRESTController extends RESTController
+{
+    /**
+     * @return string Retorna el nombre de la Entity de trabajo.
+     */
+    public function getRepository()
+    {
+        return 'KeaBundle:Config';
+    }
+
+    /**
+     * @return string Retorna el tipo de la clase.
+     */
+    public function getFormEntityType()
+    {
+        return get_class(new ConfigType());
+    }
+
+    /**
+     * GET Route annotation.
+     * @Get("/config/dhcp/{id}/action/{action}")
+     *
+     * @param Request $request
+     * @param $id
+     * @param $action
+     *
+     * @return Response
+     */
+    public function applyAction(Request $request, Int $id, String $action)
+    {
+        $dhcp = $this->get("doctrine.orm.entity_manager")->getRepository("DHCPBundle\Entity\DHCP")->findOneById($id);
+        $translator = $this->get('translator');
+        $keaConfigService = $this->get('kea.config');
+
+        if($action == "help") {
+            return array("help" => array(
+                                'id' => $translator->trans("api.dhcp_id", array(), "KeaBundle"),
+                                'action' => array(
+                                    'get' => $translator->trans("api.get_config_help", array(), "KeaBundle"),
+                                    'generate' => $translator->trans("api.generate_config_help", array(), "KeaBundle"),
+                                    'set' => $translator->trans("api.set_config_help", array(), "KeaBundle")
+                                    )));
+                                
+        }
+
+        if(is_null($dhcp)) {
+            return array("error" => $translator->trans("api.dhcp_not_found", array(), "KeaBundle"));
+        }
+        
+        if($action == "get") {
+            $config = $keaConfigService->getRemoteConfig($dhcp);
+        } elseif($action == "generate") {
+            $config = $keaConfigService->getConfig($id);
+        } elseif($action == "set") {
+            $config = $keaConfigService->getConfig($id);
+            $config = $keaConfigService->setConfig($dhcp, $config);
+        } else {
+            return array("error" => $translator->trans("api.config_action_not_found", array(), "KeaBundle"));
+        }
+        
+        return json_decode($config,true);
+    }
+}

+ 38 - 0
src/KeaBundle/Form/ConfigType.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace KeaBundle\Form;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class ConfigType extends AbstractType
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder->add('description')->add('dhcp')->add('template')->add('created');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function configureOptions(OptionsResolver $resolver)
+    {
+        $resolver->setDefaults(array(
+            'data_class' => 'KeaBundle\Entity\Config'
+        ));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getBlockPrefix()
+    {
+        return 'keabundle_config';
+    }
+
+
+}

+ 8 - 1
src/KeaBundle/Resources/translations/KeaBundle.es.yml

@@ -45,4 +45,11 @@ set_config: Cargar configuración
 set_config_help: Envía la configuración visible en el editor al KEA del DHCP seleccionado(recomendable guardar la configuración actual antes de cargar una nueva configuración).
 save_config: Guardar configuración
 save_config_help: Guarda la configuración visible en el sistema.
-kea_get_config: Configurar KEA
+kea_get_config: Configurar KEA
+api:
+    dhcp_not_found: Elemento DHCP no encontrado
+    config_action_not_found: "Acción incorrecta, opciones: get|generate|set|help"
+    dhcp_id: ID del DHCP sobre el cual se desea obtener o cargar la configuración de KEA.
+    get_config_help: Obtiene la configuración actual del KEA perteneciente al DHCP.
+    generate_config_help: Genera la configuración para el KEA del DHCP partiendo de los datos cargados en el sistema de Flowdat(host, dhcp, subred y pool).
+    set_config_help: Genera configuración KEA y la envía hacia el KEA del DHCP seleccionado.