|
@@ -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');
|
|
|
+ }
|
|
|
+
|
|
|
+}
|