getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin', 'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'adminpass' ]; $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->mac = $input->getArgument('mac'); $this->webservice = $this->getContainer()->get('webservice'); $this->credentials = [ 'username' => $input->getOption('api-username'), 'password' => $input->getOption('api-password'), ]; $em = $this->getContainer()->get('doctrine.orm.entity_manager'); if ($this->cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($this->mac)) { // Crea el Host $output->writeln($this->createHost()); // Crea o elimina si existe el Host CPE $cpeFixedIP = $this->cablemodem->getCpeFixedIP(); if ($cpeFixedIP) { $output->writeln($this->createHost('CPE')); } else { $output->writeln($this->deleteHost('CPE')); } // Crea o elimina si existe el Host MTA $mtaFixedIP = $this->cablemodem->getMtaFixedIP(); if ($mtaFixedIP) { $output->writeln($this->createHost('MTA')); } else { $output->writeln($this->deleteHost('MTA')); } } else { $output->writeln("Cablemodem mac {$this->mac} not found"); } } /** * @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, HttpRequestInterface::METHOD_GET, [], $this->credentials)) { $host = current(json_decode($hostJSON, true)); } // 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, HttpRequestInterface::METHOD_GET, [], $this->credentials)) { $host = current(json_decode($hostJSON, true)); } } // Creo, edito o elimino dependiendo los parámetros $method = HttpRequestInterface::METHOD_POST; if ($this->input->getOption('delete') == true) { $method = HttpRequestInterface::METHOD_DELETE; } elseif ($host) { $method = HttpRequestInterface::METHOD_PUT; } $data = [ 'mac' => $this->mac, 'hostType' => isset($host['hostType']) ? $host['hostType']['id'] : $this->getIdHostType($type), 'state' => 'active', 'fixedIP' => $this->cablemodem->getFixedIP(), ]; if ($type == 'MTA' || $type == 'CPE') { unset($data['mac']); $data['host'] = $hostId; $data['fixedIP'] = $type == 'MTA' ? $this->cablemodem->getMtaFixedIP() : $this->cablemodem->getCpeFixedIP(); } $dhcpOptions = $this->cablemodem->getDHCPOptions(); $data = array_merge($data, $dhcpOptions); return $ws->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $this->credentials); } /** * @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, HttpRequestInterface::METHOD_GET, [], $this->credentials)) { $host = current(json_decode($hostJSON, true)); } // 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, HttpRequestInterface::METHOD_GET, [], $this->credentials)) { $deleteHost = current(json_decode($hostJSON, true)); } } if (!$deleteHost) { return null; } $method = HttpRequestInterface::METHOD_DELETE; $data = [ 'hostType' => isset($deleteHost['hostType']) ? $deleteHost['hostType']['id'] : $this->getIdHostType($type), 'host' => $hostId, ]; return $ws->makeGetRequest($this->getUrlParameter($method, $deleteHost), $method, $data, $this->credentials); } /** * 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, HttpRequestInterface::METHOD_GET, [], $this->credentials); if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) { $hostType = current(json_decode($hostTypeJSON, true))['id']; } return $hostType; } }