Browse Source

importador de onus

Luciano Andrade 7 years ago
parent
commit
2dd9b3c9ad
5 changed files with 187 additions and 31 deletions
  1. 1 0
      tools/ONU.json
  2. 1 0
      tools/cmd.php
  3. 162 0
      tools/src/Command/ImportONUCommand.php
  4. 22 31
      tools/src/ImportClient.php
  5. 1 0
      tools/src/Release.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();

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

@@ -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'); 
+    }
+
+}

+ 22 - 31
tools/src/ImportClient.php

@@ -38,41 +38,32 @@ class ImportClient extends Command{
 		->addOption('pass', null, InputOption::VALUE_REQUIRED, 'The FD3 Password.', 'adminpass')
 		// ->addOption('source-name', null, InputOption::VALUE_REQUIRED, 'Rename the source to this name.', "upstream")
 	    ;
-	    }
-
-	    protected function execute(InputInterface $input, OutputInterface $output)
-	    {
-		$base_url = $input->getArgument("base_url");
-		$uri = $input->getOption("uri");
-		$user = $input->getOption('user');
-		$pass = $input->getOption('pass');
+    }
 
-		$file = $input->getArgument("data_file.json");
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+	$base_url = $input->getArgument("base_url");
+	$uri = $input->getOption("uri");
+	$user = $input->getOption('user');
+	$pass = $input->getOption('pass');
 
-		//$data = array(
-		//	"externalId" => "0002",
-		//	"name" => "name",
-		//	"company" => "company",
-		//	"comments" => "comments",
-		//	"email" => "email@domain.com",
-		//	"phoneNumber" => "+55-55-55-55",
-		//	);
-		$url = $base_url.$uri;
+	$file = $input->getArgument("data_file.json");
 
-		$parser = new \JsonCollectionParser\Parser();
-		$parser->parse($file, function($data) use ($url, $user, $pass){
-			$client = new GuzzClient();
-			try{
-			    $res = $client->request("POST", $url, [ 'auth' => [$user, $pass], 'body' => json_encode($data) ]);
-			    echo $res->getBody();
-			} catch (RequestException $e) {
-			    echo Psr7\str($e->getRequest());
-			    if ($e->hasResponse()) {
-				echo Psr7\str($e->getResponse());
-			    }
-			}
+	$url = $base_url.$uri;
 
+	$parser = new \JsonCollectionParser\Parser();
+	$parser->parse($file, function($data) use ($url, $user, $pass){
+		$client = new GuzzClient();
+		try{
+		    $res = $client->request("POST", $url, [ 'auth' => [$user, $pass], 'body' => json_encode($data) ]);
+		    echo $res->getBody();
+		} catch (RequestException $e) {
+		    echo Psr7\str($e->getRequest());
+		    if ($e->hasResponse()) {
+			echo Psr7\str($e->getResponse());
+		    }
+		}
 		}
-		);
+	);
     }
 }

+ 1 - 0
tools/src/Release.php

@@ -210,6 +210,7 @@ $oauth_client_secret = "5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k";
 		//->addEnviroment("HTTPS_METHOD", 	"nohttps")
 
 		->addVolumes("./mapas/", "/opt/mapas")
+		->addVolumes("./mapas/web/uploads", "/opt/mapas/web/uploads")
 	;