瀏覽代碼

Avances importador

Guillermo Espinoza 7 年之前
父節點
當前提交
4a26a8be41
共有 5 個文件被更改,包括 2125 次插入175 次删除
  1. 1 0
      tools/ONU.json
  2. 1 0
      tools/cmd.php
  3. 5 1
      tools/composer.json
  4. 1983 174
      tools/composer.lock
  5. 135 0
      tools/src/Command/ImportONUCommand.php

+ 1 - 0
tools/ONU.json

@@ -0,0 +1 @@
+[{"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3}]

+ 1 - 0
tools/cmd.php

@@ -12,5 +12,6 @@ $app->add(new FD3\GetSource());
 $app->add(new FD3\MergeHostsFile());
 $app->add(new FD3\DockerInventory());
 $app->add(new FD3\ImportClient());
+$app->add(new FD3\Command\ImportONUCommand());
 
 $app->run();

+ 5 - 1
tools/composer.json

@@ -7,7 +7,11 @@
         "magicalex/write-ini-file": "^1.2",
         "rlanvin/php-ip": "1.*",
         "guzzlehttp/guzzle": "~6.0",
-        "maxakawizard/json-collection-parser": "^1.1"
+        "maxakawizard/json-collection-parser": "^1.1",
+        "symfony/http-foundation": "3.3.*",
+        "symfony/security": "*",
+        "symfony/browser-kit": "*",
+        "hwi/oauth-bundle": "^0.5.3"
     },
     "autoload": {
         "psr-4": {"FD3\\": "src/"}

文件差異過大導致無法顯示
+ 1983 - 174
tools/composer.lock


+ 135 - 0
tools/src/Command/ImportONUCommand.php

@@ -0,0 +1,135 @@
+<?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 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":"1","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('client_id', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', '1_3323sq6urn8kwccg8s4ok848ggwwgkw4c08wsc4cwkc08osocc')
+                ->addOption('client_secret', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client Secret.', '5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k')
+        ;
+    }
+
+    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");
+        
+        $client_id = $input->getOption('client_id');
+        $client_secret = $input->getOption('client_secret');
+        
+        $token = $this->getAccessToken($username, $password, $client_id, $client_secret);
+
+        $url = $base_url . $uri;
+
+        $parser = new \JsonCollectionParser\Parser();
+        $client = new GuzzClient();
+        $parser->parse($file, function($data) use ($client, $url, $token) {
+            try {
+                
+
+//                $cookie = $this->logIn($user, $token['access_token'], $token['refresh_token'], $token['expires_in']);
+//                $cookieJar = CookieJar::fromArray([
+//                    $cookie->getName() => $cookie->getValue()
+//                ], 'ftth.fd3.flowdat.com');
+//                
+                
+//                $res = $client->request("POST", 'base.fd3.flowdat.com/oauth/v2/auth/login_check', [
+//                    'auth' => [$user, $pass],
+//                'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
+//                    'body' => http_build_query([
+//                        '_username'=>$user,
+//                        '_password'=>$pass,
+//                        '_csrf_token'=>'XqptzqyIjxEvmrp8kPb6-1AuTNmxYwH5qfhVR6QNiKg'
+//                            ])
+//                    ]);
+//                    
+                $res = $client->request("POST", $url, [
+//                    'cookies' => $cookieJar,
+                    'headers' => ['Authorization' => "Bearer {$token['access_token']}"],
+                    '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 = 'http://base.fd3.flowdat.com/oauth/v2/token')
+    {
+        $body = ['grant_type' => 'password',
+            'client_id' => $client_id,
+            'client_secret' => $client_secret,
+            'username' => $username,
+            'password' => $password];
+        
+        $client = new GuzzClient();
+        $res = $client->request("POST", $url, [
+            'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
+            'body' => http_build_query($body),
+        ]);
+        
+        return json_decode($res->getBody(), true);
+    }
+    
+    private function logIn($username, $access_token, $refresh_token, $expires_in)
+    {
+        $session = new Session();
+        $session->setName('ftth_session');
+        $session->start();
+
+        // the firewall context (defaults to the firewall name)
+        $firewall = 'secured_area';
+
+        $token = new OAuthToken($access_token, array('ROLE_USER'));
+        $token->setAccessToken($access_token);
+        $token->setRefreshToken($refresh_token);
+        $token->setExpiresIn($expires_in);
+        $session->set('_security_'.$firewall, serialize($token));
+        $session->save();
+
+        return new Cookie($session->getName(), $session->getId());
+    }
+
+}