DHCPHostCRUDCommand.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace CablemodemBundle\Command;
  3. use Buzz\Message\RequestInterface as HttpRequestInterface;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. class DHCPHostCRUDCommand extends ContainerAwareCommand
  10. {
  11. /**
  12. * @see Command
  13. */
  14. protected function configure()
  15. {
  16. $params = [
  17. 'CMD_USERNAME' => getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin',
  18. 'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'adminpass'
  19. ];
  20. $dhcpUrl = getenv("HOST_DHCP");
  21. $this
  22. ->setName('dhcp:host:crud')
  23. ->setDescription('DHCP Host CRUD')
  24. ->setDefinition(array(
  25. new InputArgument('mac', InputArgument::REQUIRED, "Cablemodem mac address"),
  26. new InputOption('delete', 'd'),
  27. new InputOption('url-get', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts', "https://{$dhcpUrl}/api/hosts.json"),
  28. new InputOption('url-post', null, InputOption::VALUE_OPTIONAL, 'API URL POST hosts', "https://{$dhcpUrl}/api/hosts.json"),
  29. new InputOption('url-put', null, InputOption::VALUE_OPTIONAL, 'API URL PUT hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
  30. new InputOption('url-delete', null, InputOption::VALUE_OPTIONAL, 'API URL DELETE hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
  31. new InputOption('url-get-hosttype', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts types', "https://{$dhcpUrl}/api/hosttypes.json"),
  32. new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', $params["CMD_USERNAME"]),
  33. new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', $params["CMD_PASSWORD"]),
  34. ))
  35. ->setHelp(<<<EOT
  36. The <info>dhcp:host:crud</info> command create or delete a DHCP Host from Cablemodem HostType and mac parameter
  37. EOT
  38. );
  39. }
  40. /**
  41. * @param InputInterface $input
  42. * @param OutputInterface $output
  43. */
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $this->input = $input;
  47. $mac = $input->getArgument('mac');
  48. $delete = $input->getOption('delete');
  49. $credentials = [
  50. 'username' => $input->getOption('api-username'),
  51. 'password' => $input->getOption('api-password'),
  52. ];
  53. $em = $this->getContainer()->get('doctrine.orm.entity_manager');
  54. $cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($mac);
  55. if ($cablemodem) {
  56. $webservice = $this->getContainer()->get('webservice');
  57. // consulto si hay un DHCP Host para la mac y traigo el HostType
  58. $host = null;
  59. $url = $webservice->buildUrl($input->getOption('url-get'), [
  60. 'mac' => $mac,
  61. ]);
  62. $hostJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
  63. if ($hostJSON != '') {
  64. $host = current(json_decode($hostJSON, true));
  65. }
  66. $hostType = null;
  67. if (isset($host['hostType'])) {
  68. $hostType = $host['hostType']['id'];
  69. } else {
  70. $url = $webservice->buildUrl($this->getUrlParameterHostType(), [
  71. 'name' => 'Cablemodem',
  72. ]);
  73. $hostTypeJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
  74. if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) {
  75. $hostType = current(json_decode($hostTypeJSON, true))['id'];
  76. }
  77. }
  78. $method = HttpRequestInterface::METHOD_POST;
  79. if ($delete == true) {
  80. $method = HttpRequestInterface::METHOD_DELETE;
  81. } elseif ($host) {
  82. $method = HttpRequestInterface::METHOD_PUT;
  83. }
  84. $data = [
  85. 'mac' => $mac,
  86. 'hostType' => $hostType,
  87. 'state' => 'active',
  88. 'fixedIP' => $cablemodem->getFixedIP(),
  89. ];
  90. $dhcpOptions = $cablemodem->getDHCPOptions();
  91. $data = array_merge($data, $dhcpOptions);
  92. $result = $webservice->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $credentials);
  93. $output->writeln($result);
  94. } else {
  95. $output->writeln("<error>Cablemodem mac {$mac} not found</error>");
  96. }
  97. }
  98. /**
  99. * @param string $method
  100. * @param array $host
  101. *
  102. * @return string
  103. */
  104. private function getUrlParameter($method = HttpRequestInterface::METHOD_POST, $host = [])
  105. {
  106. $container = $this->getContainer();
  107. $parameter = 'dhcp_host_post_url';
  108. $url = $this->input->getOption('url-post');
  109. $id = '';
  110. if (isset($host['id'])) {
  111. $id = $host['id'];
  112. }
  113. if ($method == HttpRequestInterface::METHOD_PUT && $id) {
  114. $parameter = 'dhcp_host_put_url';
  115. $url = $this->input->getOption('url-put');
  116. } elseif ($method == HttpRequestInterface::METHOD_DELETE && $id) {
  117. $parameter = 'dhcp_host_del_url';
  118. $url = $this->input->getOption('url-delete');
  119. }
  120. return $container->hasParameter($parameter) ? str_replace('{id}', $id, $container->getParameter($parameter)) : $url;
  121. }
  122. /**
  123. * @return string
  124. */
  125. private function getUrlParameterHostType()
  126. {
  127. $container = $this->getContainer();
  128. $parameter = 'dhcp_host_type_get_url';
  129. return $container->hasParameter($parameter) ?
  130. $container->getParameter($parameter) :
  131. $this->input->getOption('url-get-hosttype');
  132. }
  133. }