|
@@ -0,0 +1,162 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace FD3\Command;
|
|
|
+
|
|
|
+use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputArgument;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
+use Symfony\Component\BrowserKit\Cookie;
|
|
|
+use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
|
|
|
+use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser;
|
|
|
+use GuzzleHttp\Client as GuzzClient;
|
|
|
+use GuzzleHttp\Exception\ServerException as RequestException;
|
|
|
+use GuzzleHttp\Psr7;
|
|
|
+use GuzzleHttp\Cookie\CookieJar;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class ImportONUCommand extends Command
|
|
|
+{
|
|
|
+
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this
|
|
|
+ ->setName('import:ftth:onu')
|
|
|
+ ->setDescription('Import ONU entities from json file')
|
|
|
+ ->setHelp('ONU.json shuld be a valid json file, probably looking like the following example:
|
|
|
+[
|
|
|
+ {"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3},
|
|
|
+ {"clientId":1,"ponSerialNumber":"2","transitionState":"success","tenancyId":3}
|
|
|
+]
|
|
|
+
|
|
|
+')
|
|
|
+ ->addArgument('base_url', InputArgument::REQUIRED, 'The base url of the FD3 Client.')
|
|
|
+ ->addArgument('data_file.json', InputArgument::OPTIONAL, 'The data source from where to get data.', "ONU.json")
|
|
|
+ ->addOption('uri', null, InputOption::VALUE_REQUIRED, 'uri after the base_url for the complete URL', 'api/onus.json')
|
|
|
+ ->addOption('user', null, InputOption::VALUE_REQUIRED, 'The FD3 Username.', 'admin')
|
|
|
+ ->addOption('pass', null, InputOption::VALUE_REQUIRED, 'The FD3 Password.', 'adminpass')
|
|
|
+ //->addOption('oauth_token', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', 'http://base.galvez.flowdat.com/oauth/v2/token')
|
|
|
+ //->addOption('client_id', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', '2_2giq1sosvyxwsgwswcw4s44w0o0go0wc0oo4ck4kgo008w80k0')
|
|
|
+ //->addOption('client_secret', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client Secret.', 'kuu06cynskgkc0swokkkg4gg8k84w0ks00cw00wk4ow480okg')
|
|
|
+ ;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
+ {
|
|
|
+ $base_url = $input->getArgument("base_url");
|
|
|
+ $uri = $input->getOption("uri");
|
|
|
+ $username = $input->getOption('user');
|
|
|
+ $password = $input->getOption('pass');
|
|
|
+
|
|
|
+ $file = $input->getArgument("data_file.json");
|
|
|
+
|
|
|
+ $url = $base_url . $uri;
|
|
|
+ $client = new GuzzClient();
|
|
|
+ $parser = new \JsonCollectionParser\Parser();
|
|
|
+ //$cookieJar = $this->getCookieJar($username, $token);
|
|
|
+
|
|
|
+ $parser->parse($file, function($data) use ($client, $url, $username, $password) {
|
|
|
+ try {
|
|
|
+ $res = $client->request("POST", $url.'?filters[disableTenancy]', [
|
|
|
+ // 'headers' => ['Authorization' => "Bearer {$token['access_token']}"]
|
|
|
+ 'auth'=> [$username, $password],
|
|
|
+ 'body' => json_encode($data)
|
|
|
+ ]);
|
|
|
+ echo $res->getBody();
|
|
|
+ } catch (RequestException $e) {
|
|
|
+ echo Psr7\str($e->getRequest());
|
|
|
+ if ($e->hasResponse()) {
|
|
|
+ echo Psr7\str($e->getResponse());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getAccessToken($username, $password, $client_id, $client_secret, $url)
|
|
|
+ {
|
|
|
+ $body = ['grant_type' => 'password',
|
|
|
+ 'username' => $username,
|
|
|
+ 'password' => $password
|
|
|
+ ];
|
|
|
+
|
|
|
+ $client = new GuzzClient();
|
|
|
+ $res = $client->request("POST", $url, [
|
|
|
+ 'auth' => [$client_id, $client_secret],
|
|
|
+ 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
|
|
|
+ 'body' => http_build_query($body),
|
|
|
+ ]);
|
|
|
+ $body = $res->getBody();
|
|
|
+
|
|
|
+ return json_decode($body, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retorna un auth token OAuth
|
|
|
+ *
|
|
|
+ * @param string $username
|
|
|
+ * @param string $password
|
|
|
+ * @param string $client_id
|
|
|
+ * @param string $client_secret
|
|
|
+ * @param string $url
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function getRefreshToken($token, $client_id, $client_secret, $url)
|
|
|
+ {
|
|
|
+ $body = ['grant_type' => 'refresh_token',
|
|
|
+ 'refresh_token' => $token['refresh_token']
|
|
|
+ ];
|
|
|
+
|
|
|
+ $client = new GuzzClient();
|
|
|
+ $res = $client->request("POST", $url, [
|
|
|
+ 'auth' => [$client_id, $client_secret],
|
|
|
+ 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
|
|
|
+ 'body' => http_build_query($body),
|
|
|
+ ]);
|
|
|
+ $body = $res->getBody();
|
|
|
+
|
|
|
+ return json_decode($body, true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Guardo en session al OAuthUser
|
|
|
+ * Retorno una cookie con la info de session
|
|
|
+ *
|
|
|
+ * @param string $username
|
|
|
+ * @param array $token
|
|
|
+ *
|
|
|
+ * @return CookieJar
|
|
|
+ */
|
|
|
+ private function getCookieJar($username, $token)
|
|
|
+ {
|
|
|
+ $session = new Session();
|
|
|
+ $session->setName('ftth_session');
|
|
|
+ $session->start();
|
|
|
+
|
|
|
+ // the firewall context (defaults to the firewall name)
|
|
|
+ $firewall = 'secured_area';
|
|
|
+
|
|
|
+ $OAuthToken = new OAuthToken($token['access_token'], array('ROLE_ADMIN', 'ROLE_USER'));
|
|
|
+ $OAuthToken->setRawToken($token);
|
|
|
+ $user = new OAuthUser($username);
|
|
|
+ $OAuthToken->setUser($user);
|
|
|
+
|
|
|
+ $session->set('_security_'.$firewall, $OAuthToken->serialize());
|
|
|
+ $session->save();
|
|
|
+
|
|
|
+ $cookie = new Cookie($session->getName(), $session->getId());
|
|
|
+
|
|
|
+ return CookieJar::fromArray([
|
|
|
+ $cookie->getName() => $cookie->getValue(),
|
|
|
+ ], 'ftth.galvez.flowdat.com');
|
|
|
+ }
|
|
|
+
|
|
|
+}
|