123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?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()
- {
- $params = [
- 'CMD_USERNAME' => 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(<<<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;
- $url = $webservice->buildUrl($input->getOption('url-get'), [
- 'mac' => $mac,
- ]);
- $hostJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
- if ($hostJSON != '') {
- $host = current(json_decode($hostJSON, true));
- }
- $hostType = null;
- if (isset($host['hostType'])) {
- $hostType = $host['hostType']['id'];
- } else {
- $url = $webservice->buildUrl($this->getUrlParameterHostType(), [
- 'name' => 'Cablemodem',
- ]);
- $hostTypeJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
- if ($hostTypeJSON != '' && !is_null($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;
- }
- $data = [
- 'mac' => $mac,
- 'hostType' => $hostType,
- 'state' => 'active',
- 'fixedIP' => $cablemodem->getFixedIP(),
- ];
- $dhcpOptions = $cablemodem->getDHCPOptions();
- $data = array_merge($data, $dhcpOptions);
- $result = $webservice->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $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');
- }
- }
|