CablemodemUpdateCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace HostBundle\Command;
  3. use Buzz\Message\RequestInterface as HttpRequestInterface;
  4. use HostBundle\Entity\Host;
  5. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. class CablemodemUpdateCommand extends ContainerAwareCommand
  12. {
  13. /**
  14. * @see Command
  15. */
  16. protected function configure()
  17. {
  18. $params = [
  19. 'CMD_USERNAME' => getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin',
  20. 'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'admin'
  21. ];
  22. $host_cablemodem = getenv("HOST_CABLEMODEM");
  23. $this
  24. ->setName('cablemodem:update')
  25. ->setDescription('Update cablemodem record associated to a host')
  26. ->setDefinition(array(
  27. new InputOption('id', null, InputOption::VALUE_REQUIRED, 'Host id'),
  28. new InputOption(
  29. 'url-get',
  30. null,
  31. InputOption::VALUE_OPTIONAL,
  32. 'API URL GET cablemodems',
  33. "https://{$host_cablemodem}/api/cablemodems.json"
  34. ),
  35. new InputOption(
  36. 'url-put',
  37. null,
  38. InputOption::VALUE_OPTIONAL,
  39. 'API URL PUT cablemodems',
  40. "https://{$host_cablemodem}/api/cablemodems/{id}.json"
  41. ),
  42. new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', $params["CMD_USERNAME"]),
  43. new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', $params["CMD_PASSWORD"]),
  44. ))
  45. ->setHelp(<<<EOT
  46. The <info>cablemodem:update</info> command updates a Cablemodem associated to a DHCP Host
  47. EOT
  48. );
  49. }
  50. /**
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. $this->input = $input;
  57. $this->io = new SymfonyStyle($input, $output);
  58. $this->id = $input->getOption('id');
  59. if (!$this->id) {
  60. $output->writeln("<error>Parameter id is required</error>");
  61. return;
  62. }
  63. $this->credentials = [
  64. 'username' => $input->getOption('api-username'),
  65. 'password' => $input->getOption('api-password'),
  66. ];
  67. $this->webservice = $this->getContainer()->get('webservice');
  68. $em = $this->getContainer()->get('doctrine.orm.entity_manager');
  69. if ($this->host = $em->getRepository(Host::class)->find($this->id)) {
  70. $mac = $this->host->getMac();
  71. if (is_null($mac)) {
  72. // El host no tiene mac, chequeo si tiene host asociado
  73. $mac = $this->host->getHost()->getMac();
  74. }
  75. // Busco el cablemodem asociado
  76. $this->io->section('Searching Cablemodem');
  77. $this->cablemodem = $this->findCablemodem($mac);
  78. $this->io->success('RESULT: ' . json_encode($this->cablemodem));
  79. if ($this->cablemodem) {
  80. $this->io->section('Updating Cablemodem');
  81. $this->cablemodem = $this->updateCablemodem($this->cablemodem, $this->host);
  82. $this->io->success('RESULT: ' . json_encode($this->cablemodem));
  83. } else {
  84. $output->writeln("<error>Cablemodem MAC {$mac} not found</error>");
  85. }
  86. } else {
  87. $output->writeln("<error>Host id {$this->id} not found</error>");
  88. }
  89. }
  90. /**
  91. * @param string $mac
  92. *
  93. * @return null|array
  94. */
  95. private function findCablemodem($mac)
  96. {
  97. $ws = $this->webservice;
  98. $urlGET = $this->input->getOption('url-get');
  99. // Consulto en Cablemodem
  100. $cablemodem = null;
  101. $url = $ws->buildUrl($urlGET, compact('mac'));
  102. $cablemodemJSON = $ws->makeGetRequest($url);
  103. if ($cablemodemJSON) {
  104. $cablemodem = current(json_decode($cablemodemJSON, true));
  105. }
  106. return $cablemodem;
  107. }
  108. /**
  109. * @param array $cablemodem
  110. * @param Host $host
  111. *
  112. * @return array
  113. */
  114. private function updateCablemodem($cablemodem, $host)
  115. {
  116. $ws = $this->webservice;
  117. $id = $cablemodem['id'];
  118. $urlPUT = str_replace('{id}', $id, $this->input->getOption('url-put'));
  119. $fixedAddress = $host->getFixedAddress();
  120. $data = [];
  121. switch ($host->getHostType()->getShortname()) {
  122. case 'cablemodem':
  123. $data['fixedIP'] = $fixedAddress;
  124. break;
  125. case 'cpe':
  126. $data['cpeFixedIP'] = $fixedAddress;
  127. break;
  128. case 'mta':
  129. $data['mtaFixedIP'] = $fixedAddress;
  130. break;
  131. default:
  132. $data['fixedIP'] = $fixedAddress;
  133. break;
  134. }
  135. $cablemodemJSON = $ws->makeGetRequest($urlPUT, HttpRequestInterface::METHOD_PUT, $data);
  136. if ($cablemodemJSON) {
  137. $cablemodem = current(json_decode($cablemodemJSON, true));
  138. }
  139. return $cablemodem;
  140. }
  141. }