Bladeren bron

FD3-442 comando para crear Host DHCP. Migración de actions de cablemodem_workflow

Guillermo Espinoza 7 jaren geleden
bovenliggende
commit
4241768740

+ 18 - 1
app/DoctrineMigrations/Version20180124153518.yml

@@ -1,4 +1,4 @@
-replace:
+INSERTORUPDATE:
     workflow:
         - id: 1
           name: "cablemodem_workflow"
@@ -13,3 +13,20 @@ replace:
           enable: 1
           support: "a:1:{i:0;s:34:\"CablemodemBundle\\Entity\\Cablemodem\";}"
           owner: "1"
+    action:
+        - id: "1"
+          name: "Create DHCP Host"
+          workflow_name: "cablemodem_workflow"
+          object_class: "CablemodemBundle\\Entity\\Cablemodem"
+          event: "a:1:{i:0;s:5:\"enter\";}"
+          event_reference: "active"
+          template: "/opt/cablemodem/bin/console dhcp:host:crud {{ object.mac }}"
+          tenancy_id: "1"
+        - id: "2"
+          name: "Remove DHCP Host"
+          workflow_name: "cablemodem_workflow"
+          object_class: "CablemodemBundle\\Entity\\Cablemodem"
+          event: "a:1:{i:0;s:5:\"enter\";}"
+          event_reference: "disable"
+          template: "/opt/cablemodem/bin/console dhcp:host:crud {{ object.mac }} -d"
+          tenancy_id: "1"

+ 7 - 0
app/config/bundles/ik/webservice-bundle/parameters.yml.dist

@@ -19,3 +19,10 @@ parameters:
     remote_device_log_url: '%url_base%/api/devicelogs.json'
     remote_get_map_url: '%url_mapas%/api/maps.json'
     remote_get_object_type_url: '%url_mapas%/api/objecttypes.json'
+
+    # DHCP APP API URL
+    dhcp_docker: 'http://dhcp:8000'
+    dhcp_host_post_url: '%dhcp_docker%/api/hosts.json'
+    dhcp_host_put_url: '%dhcp_docker%/api/hosts/{id}.json'
+    dhcp_host_del_url: '%dhcp_docker%/api/hosts/{id}.json'
+    dhcp_host_type_get_url: '%dhcp_docker%/api/hosttypes.json'

+ 7 - 0
app/config/bundles/ik/webservice-bundle/parameters.yml.docker

@@ -19,3 +19,10 @@ parameters:
     remote_device_log_url: '%url_base%/api/devicelogs.json'
     remote_get_map_url: '%url_mapas%/api/maps.json'
     remote_get_object_type_url: '%url_mapas%/api/objecttypes.json'
+
+    # DHCP APP API URL
+    dhcp_docker: 'http://dhcp:8000'
+    dhcp_host_post_url: '%dhcp_docker%/api/hosts.json'
+    dhcp_host_put_url: '%dhcp_docker%/api/hosts/{id}.json'
+    dhcp_host_del_url: '%dhcp_docker%/api/hosts/{id}.json'
+    dhcp_host_type_get_url: '%dhcp_docker%/api/hosttypes.json'

+ 137 - 0
src/CablemodemBundle/Command/DHCPHostCRUDCommand.php

@@ -0,0 +1,137 @@
+<?php
+
+namespace CablemodemBundle\Command;
+
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class DHCPHostCRUDCommand extends ContainerAwareCommand
+{
+
+    /**
+     * @see Command
+     */
+    protected function configure()
+    {
+        $this
+            ->setName('dhcp:host:crud')
+            ->setDescription('DHCP Host CRUD')
+            ->setDefinition(array(
+                new InputArgument('mac', InputArgument::REQUIRED, "Cablemodem mac address"),
+                new InputOption('delete', 'd'),
+                new InputOption('url-get', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts', 'http://dhcp:8000/api/hosts.json'),
+                new InputOption('url-post', null, InputOption::VALUE_OPTIONAL, 'API URL POST hosts', 'http://dhcp:8000/api/hosts.json'),
+                new InputOption('url-put', null, InputOption::VALUE_OPTIONAL, 'API URL PUT hosts', 'http://dhcp:8000/api/hosts/{id}.json'),
+                new InputOption('url-delete', null, InputOption::VALUE_OPTIONAL, 'API URL DELETE hosts', 'http://dhcp:8000/api/hosts/{id}.json'),
+                new InputOption('url-get-hosttype', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts types', 'http://dhcp:8000/api/hosttypes.json'),
+                new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', 'admin'),
+                new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', 'adminpass'),
+            ))
+            ->setHelp(<<<EOT
+The <info>dhcp:host:crud</info> command create or delete a DHCP Host from Cablemodem HostType and mac parameter
+EOT
+        );
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $this->input = $input;
+        $mac = $input->getArgument('mac');
+        $delete = $input->getOption('delete');
+        $credentials = [
+            'username' => $input->getOption('api-username'),
+            'password' => $input->getOption('api-password'),
+        ];
+
+        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
+        $cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($mac);
+        if ($cablemodem) {
+            $webservice = $this->getContainer()->get('webservice');
+            // consulto si hay un DHCP Host para la mac y traigo el HostType
+            $host = null;
+            $hostJSON = $webservice->makeGetRequest($input->getOption('url-get'), HttpRequestInterface::METHOD_GET, [
+                'mac' => $mac,
+            ], $credentials);
+            if ($hostJSON != '') {
+                $host = current(json_decode($hostJSON, true));
+            }
+
+            $hostType = null;
+            if (isset($host['hostType'])) {
+                $hostType = $host['hostType']['id'];
+            } else {
+                $hostTypeJSON = $webservice->makeGetRequest($this->getUrlParameterHostType(), HttpRequestInterface::METHOD_GET, [
+                    'name' => 'Cablemodem',
+                ], $credentials);
+                if ($hostTypeJSON != '') {
+                    $hostType = current(json_decode($hostTypeJSON, true))['id'];
+                }
+            }
+
+            $method = HttpRequestInterface::METHOD_POST;
+            if ($delete == true) {
+                $method = HttpRequestInterface::METHOD_DELETE;
+            } elseif ($host) {
+                $method = HttpRequestInterface::METHOD_PUT;
+            }
+
+            $result = $webservice->makeGetRequest($this->getUrlParameter($method, $host), $method, [
+                'mac' => $mac,
+                'options' => $mac,
+                'hostType' => $hostType,
+            ], $credentials);
+
+            $output->writeln($result);
+        } else {
+            $output->writeln("<error>Cablemodem mac {$mac} not found</error>");
+        }
+    }
+
+    /**
+     * @param string $method
+     * @param array $host
+     *
+     * @return string
+     */
+    private function getUrlParameter($method = HttpRequestInterface::METHOD_POST, $host = [])
+    {
+        $container = $this->getContainer();
+        $parameter = 'dhcp_host_post_url';
+        $url = $this->input->getOption('url-post');
+        $id = '';
+        if (isset($host['id'])) {
+            $id = $host['id'];
+        }
+        if ($method == HttpRequestInterface::METHOD_PUT && $id) {
+            $parameter = 'dhcp_host_put_url';
+            $url = $this->input->getOption('url-put');
+        } elseif ($method == HttpRequestInterface::METHOD_DELETE && $id) {
+            $parameter = 'dhcp_host_del_url';
+            $url = $this->input->getOption('url-delete');
+        }
+
+        return $container->hasParameter($parameter) ? str_replace('{id}', $id, $container->getParameter($parameter)) : $url;
+    }
+
+    /**
+     * @return string
+     */
+    private function getUrlParameterHostType()
+    {
+        $container = $this->getContainer();
+        $parameter = 'dhcp_host_type_get_url';
+
+        return $container->hasParameter($parameter) ?
+            $container->getParameter($parameter) :
+            $this->input->getOption('url-get-hosttype');
+    }
+
+}