123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace HostBundle\Command;
- use Buzz\Message\RequestInterface as HttpRequestInterface;
- use HostBundle\Entity\Host;
- 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;
- use Symfony\Component\Console\Style\SymfonyStyle;
- class CablemodemUpdateCommand 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") : 'admin'
- ];
- $host_cablemodem = getenv("HOST_CABLEMODEM");
- $this
- ->setName('cablemodem:update')
- ->setDescription('Update cablemodem record associated to a host')
- ->setDefinition(array(
- new InputOption('id', null, InputOption::VALUE_REQUIRED, 'Host id'),
- new InputOption(
- 'url-get',
- null,
- InputOption::VALUE_OPTIONAL,
- 'API URL GET cablemodems',
- "https://{$host_cablemodem}/api/cablemodems.json"
- ),
- new InputOption(
- 'url-put',
- null,
- InputOption::VALUE_OPTIONAL,
- 'API URL PUT cablemodems',
- "https://{$host_cablemodem}/api/cablemodems/{id}.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>cablemodem:update</info> command updates a Cablemodem associated to a DHCP Host
- EOT
- );
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $this->input = $input;
- $this->io = new SymfonyStyle($input, $output);
-
- $this->id = $input->getOption('id');
-
- if (!$this->id) {
- $output->writeln("<error>Parameter id is required</error>");
-
- return;
- }
-
- $this->credentials = [
- 'username' => $input->getOption('api-username'),
- 'password' => $input->getOption('api-password'),
- ];
- $this->webservice = $this->getContainer()->get('webservice');
- $em = $this->getContainer()->get('doctrine.orm.entity_manager');
-
- if ($this->host = $em->getRepository(Host::class)->find($this->id)) {
- $mac = $this->host->getMac();
- if (is_null($mac)) {
- // El host no tiene mac, chequeo si tiene host asociado
- $mac = $this->host->getHost()->getMac();
- }
- // Busco el cablemodem asociado
- $this->io->section('Searching Cablemodem');
- $this->cablemodem = $this->findCablemodem($mac);
- $this->io->success('RESULT: ' . json_encode($this->cablemodem));
-
- if ($this->cablemodem) {
- $this->io->section('Updating Cablemodem');
- $this->cablemodem = $this->updateCablemodem($this->cablemodem, $this->host);
- $this->io->success('RESULT: ' . json_encode($this->cablemodem));
- } else {
- $output->writeln("<error>Cablemodem MAC {$mac} not found</error>");
- }
- } else {
- $output->writeln("<error>Host id {$this->id} not found</error>");
- }
- }
-
- /**
- * @param string $mac
- *
- * @return null|array
- */
- private function findCablemodem($mac)
- {
- $ws = $this->webservice;
- $urlGET = $this->input->getOption('url-get');
-
- // Consulto en Cablemodem
- $cablemodem = null;
- $url = $ws->buildUrl($urlGET, compact('mac'));
- $cablemodemJSON = $ws->makeGetRequest($url);
- if ($cablemodemJSON) {
- $cablemodem = current(json_decode($cablemodemJSON, true));
- }
-
- return $cablemodem;
- }
-
- /**
- * @param array $cablemodem
- * @param Host $host
- *
- * @return array
- */
- private function updateCablemodem($cablemodem, $host)
- {
- $ws = $this->webservice;
- $id = $cablemodem['id'];
- $urlPUT = str_replace('{id}', $id, $this->input->getOption('url-put'));
- $fixedAddress = $host->getFixedAddress();
- $data = [];
- switch ($host->getHostType()->getShortname()) {
- case 'cablemodem':
- $data['fixedIP'] = $fixedAddress;
- break;
- case 'cpe':
- $data['cpeFixedIP'] = $fixedAddress;
- break;
- case 'mta':
- $data['mtaFixedIP'] = $fixedAddress;
- break;
- default:
- $data['fixedIP'] = $fixedAddress;
- break;
- }
-
- $cablemodemJSON = $ws->makeGetRequest($urlPUT, HttpRequestInterface::METHOD_PUT, $data);
- if ($cablemodemJSON) {
- $cablemodem = current(json_decode($cablemodemJSON, true));
- }
-
- return $cablemodem;
- }
-
- }
|