123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?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;
- use Symfony\Component\Console\Style\SymfonyStyle;
- class DHCPHostCRUDCommand extends ContainerAwareCommand
- {
- /**
- * @see Command
- */
- protected function configure()
- {
- $params = [
- 'CMD_USERNAME' => getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin',
- 'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'admin'
- ];
- $dhcpUrl = getenv("HOST_DHCP");
- $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', "https://{$dhcpUrl}/api/hosts.json"),
- new InputOption('url-post', null, InputOption::VALUE_OPTIONAL, 'API URL POST hosts', "https://{$dhcpUrl}/api/hosts.json"),
- new InputOption('url-put', null, InputOption::VALUE_OPTIONAL, 'API URL PUT hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
- new InputOption('url-delete', null, InputOption::VALUE_OPTIONAL, 'API URL DELETE hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
- new InputOption('url-get-hosttype', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts types', "https://{$dhcpUrl}/api/hosttypes.json"),
- new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', $params["CMD_USERNAME"]),
- new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', $params["CMD_PASSWORD"]),
- ))
- ->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;
- $this->io = new SymfonyStyle($input, $output);
-
- $this->mac = $input->getArgument('mac');
- $this->webservice = $this->getContainer()->get('webservice');
- $this->credentials = [
- 'username' => $input->getOption('api-username'),
- 'password' => $input->getOption('api-password'),
- ];
- // deshabilito filtros, entre ellos soft_delete
- $em = $this->getContainer()->get('doctrine.orm.entity_manager');
- $filters = $em->getFilters();
- $enabledFilters = $filters->getEnabledFilters();
- foreach ($enabledFilters as $filter_name => $filter) {
- $filters->disable($filter_name);
- }
-
- $this->cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($this->mac);
- if ($this->cablemodem) {
- // Crea el Host
- if ($this->input->getOption('delete') != true) {
- $this->io->section('Creating or updating DHCP Host');
- } else {
- $this->io->section('Deleting DHCP Host if exists');
- }
- $this->io->success('RESULT: ' . $this->createHost());
-
- // Crea o elimina si existe el Host CPE
- $cpeFixedIP = $this->cablemodem->getCpeFixedIP();
- if ($cpeFixedIP) {
- $this->io->section('Creating or updating DHCP CPE Host');
- $this->io->success('RESULT: ' . $this->createHost('cpe'));
- } else {
- $this->io->section('Deleting DHCP CPE Host if exists');
- $this->io->success('RESULT: ' . $this->deleteHost('cpe'));
- }
-
- // Crea o elimina si existe el Host MTA
- $mtaFixedIP = $this->cablemodem->getMtaFixedIP();
- if ($mtaFixedIP) {
- $this->io->section('Creating or updating DHCP MTA Host');
- $this->io->success('RESULT: ' . $this->createHost('mta'));
- } else {
- $this->io->section('Deleting DHCP MTA Host if exists');
- $this->io->success('RESULT: ' . $this->deleteHost('mta'));
- }
- } else {
- $output->writeln("<error>Cablemodem mac {$this->mac} not found</error>");
- }
-
- foreach ($enabledFilters as $filter_name => $filter) {
- $filters->enable($filter_name);
- }
- }
-
- /**
- * @param string $type HostType name Cablemodem | MTA | CPE
- *
- * @return string
- */
- private function createHost($type = 'cablemodem')
- {
- $ws = $this->webservice;
- $urlGET = $this->input->getOption('url-get');
-
- // Consulto en DHCP por Host
- $host = null;
- $url = $ws->buildUrl($urlGET, [
- 'mac' => $this->mac,
- ]);
- if ($hostJSON = $ws->makeGetRequest($url)) {
- $hostDecode = json_decode($hostJSON, true);
- if (json_last_error() == JSON_ERROR_NONE && is_array($hostDecode)) {
- $host = current($hostDecode);
- }
- }
-
- // Consulto por Host con HostType MTA o CPE relacionado
- if ($host && ($type == 'mta' || $type == 'cpe')) {
- $hostId = $host['id'];
- $url = $ws->buildUrl($urlGET, [
- 'host' => $hostId,
- 'hostType' => $this->getIdHostType($type),
- ]);
- if ($hostJSON = $ws->makeGetRequest($url)) {
- $hostDecode = json_decode($hostJSON, true);
- if (json_last_error() == JSON_ERROR_NONE && is_array($hostDecode)) {
- $host = current($hostDecode);
- }
- }
- if (!isset($hostId)) {
- $this->io->error("No existe el hostType {$type} en DHCP");
-
- return;
- }
- }
-
- // Creo, edito o elimino dependiendo los parámetros
- $method = HttpRequestInterface::METHOD_POST;
- if ($this->input->getOption('delete') == true) {
- $method = HttpRequestInterface::METHOD_DELETE;
- } elseif ($host) {
- $this->io->section('Updating');
- $method = HttpRequestInterface::METHOD_PUT;
- }
-
- $fixedIP = $this->cablemodem->getFixedIP();
- $mtaFixedIP = $this->cablemodem->getMtaFixedIP();
- $cpeFixedIP = $this->cablemodem->getCpeFixedIP();
-
- $options = isset($host['options']) ? $host['options'] : [];
-
- $data = [
- 'mac' => $this->mac,
- 'hostType' => $this->getIdHostType($type),
- 'state' => 'active',
- 'fixed_address' => $fixedIP ?: null,
- 'fixedIP' => $fixedIP ? true : false,
- ];
- if (isset($hostId) && ($type == 'mta' || $type == 'cpe')) {
- unset($data['mac']);
- $data['host'] = $hostId;
-
- if ($type == 'mta') {
- $data['fixed_address'] = $mtaFixedIP ?: null;
- $data['fixedIP'] = $mtaFixedIP ? true : false;
- }
- if ($type == 'cpe') {
- $data['fixed_address'] = $cpeFixedIP ?: null;
- $data['fixedIP'] = $cpeFixedIP ? true : false;
- }
- }
- $dhcpOptions = $this->cablemodem->getDHCPOptions();
- $data = array_merge($data, $dhcpOptions);
-
- $this->io->text('DATA: ' . json_encode($data));
-
- return $ws->makeGetRequest($this->getUrlParameter($method, $host), $method, $data);
- }
-
- /**
- * @param string $type HostType name Cablemodem | MTA | CPE
- *
- * @return string
- */
- private function deleteHost($type = 'cablemodem')
- {
- $ws = $this->webservice;
- $urlGET = $this->input->getOption('url-get');
-
- // Consulto en DHCP por Host
- $host = null;
- $url = $ws->buildUrl($urlGET, [
- 'mac' => $this->mac,
- ]);
- if ($hostJSON = $ws->makeGetRequest($url)) {
- $hostDecode = json_decode($hostJSON, true);
- if (json_last_error() == JSON_ERROR_NONE && is_array($hostDecode)) {
- $host = current($hostDecode);
- }
- }
-
- // Consulto por Host con HostType MTA o CPE relacionado
- $deleteHost = null;
- if ($host && ($type == 'mta' || $type == 'cpe')) {
- $hostId = $host['id'];
- $url = $ws->buildUrl($urlGET, [
- 'host' => $hostId,
- 'hostType' => $this->getIdHostType($type),
- ]);
- if ($hostJSON = $ws->makeGetRequest($url)) {
- $hostDecode = json_decode($hostJSON, true);
- if (json_last_error() == JSON_ERROR_NONE && is_array($hostDecode)) {
- $deleteHost = current($hostDecode);
- }
- }
- }
-
- if (!$deleteHost) {
- return null;
- }
-
- $method = HttpRequestInterface::METHOD_DELETE;
- $data = [
- 'hostType' => isset($deleteHost['hostType']) ? $deleteHost['hostType']['id'] : $this->getIdHostType($type),
- 'host' => $hostId,
- ];
-
- $this->io->text('DATA: ' . json_encode($data));
- return $ws->makeGetRequest($this->getUrlParameter($method, $deleteHost), $method, $data);
- }
- /**
- * Retorna una url de DHCP seteada como parámetro según el método http (GET|PUT|DELETE)
- *
- * @param string $method GET | PUT | DELETE
- * @param array $host 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;
- }
-
- /**
- * @param string $name HostType name Cablemodem | MTA | CPE
- *
- * @return int
- */
- private function getIdHostType($name = 'cablemodem')
- {
- $hostType = null;
- $container = $this->getContainer();
- $parameter = 'dhcp_host_type_get_url';
- $url = $container->hasParameter($parameter) ?
- $container->getParameter($parameter) :
- $this->input->getOption('url-get-hosttype');
-
- $url = $this->webservice->buildUrl($url, [
- 'shortname' => $name,
- ]);
- $hostTypeJSON = $this->webservice->makeGetRequest($url);
- if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) {
- $hostDecode = json_decode($hostTypeJSON, true);
- if (json_last_error() == JSON_ERROR_NONE && is_array($hostDecode)) {
- $hostType = current($hostDecode)['id'];
- }
- }
-
- return $hostType;
- }
- }
|