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(<<dhcp:host:crud 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("Cablemodem mac {$this->mac} not found"); } 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; } }