From b2ed710cda99f78fc78a8595ca240d50f1d92699 Mon Sep 17 00:00:00 2001 From: aomurbekov Date: Thu, 4 Sep 2014 13:01:44 +0600 Subject: [PATCH 1/2] Add feature of getting keystone settings from iron.json Add keystone support --- IronCore.class.php | 60 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/IronCore.class.php b/IronCore.class.php index 9dd3705..18c1dd0 100644 --- a/IronCore.class.php +++ b/IronCore.class.php @@ -46,6 +46,15 @@ class IronCore protected $curl = null; protected $last_status; + protected $server; + protected $tenant; + protected $username; + protected $password; + protected $keystone; + protected $use_keystone; + protected $keystone_token; + protected $keystone_token_expires; + protected $urlFetchContext; protected $urlFetchData; protected $urlFetchUrl; @@ -150,9 +159,16 @@ protected function getConfigData($config) $this->loadFromHash($this->default_values); - if (empty($this->token) || empty($this->project_id)) { + if (empty($this->project_id)) { throw new InvalidArgumentException("token or project_id not found in any of the available sources"); } + + if (!empty($this->keystone)){ + $required_keys = array('username', 'password', 'tenant', 'server'); + if (count(array_intersect_key(array_flip($required_keys), $this->keystone)) === count($required_keys)) { + $this->use_keystone = True; + } + } } @@ -168,6 +184,7 @@ protected function loadFromHash($options) $this->setVarIfValue('port', $options); $this->setVarIfValue('api_version', $options); $this->setVarIfValue('encryption_key', $options); + $this->setVarIfValue('keystone', $options); } protected function loadFromEnv($prefix) @@ -219,17 +236,13 @@ protected function loadConfigFile($file) $this->loadFromHash($data); } - protected function apiCall($type, $url, $params = array(), $data = null) + protected function request($type, $url, $params = array(), $data = null) { - $url = "{$this->url}$url"; $this->debug("API $type", $url); if ($this->curl == null && $this->curlEnabled()) { $this->curl = curl_init(); } - if (!isset($params['oauth'])) { - $params['oauth'] = $this->token; - } if ($this->curlEnabled()) { switch ($type) { case self::DELETE: @@ -305,6 +318,15 @@ protected function apiCall($type, $url, $params = array(), $data = null) return $this->callWithRetries(); } + protected function apiCall($type, $url, $params = array(), $data = null) + { + $url = "{$this->url}$url"; + if (!isset($params['oauth'])) { + $params['oauth'] = $this->token; + } + return $this -> request($type, $url, $params, $data); + } + protected function callWithRetries() { for ($retry = 0; $retry < $this->max_retries; $retry++) { @@ -421,6 +443,32 @@ protected function setCommonHeaders() 'Keep-Alive' => '300' ); } + + protected function getToken() + { + $current_time = new DateTime("now", new DateTimeZone('UTC')); + if (is_null($this->keystone_token) || $current_time > $this->keystone_token_expires) { + + $req = array( + 'auth' => array( + 'tenantName' => $this->keystone['tenant'], + 'passwordCredentials' => array( + 'username' => $this->keystone['username'], + 'password' => $this->keystone['password'] + ) + ) + ); + $this -> setCommonHeaders(); + $url = $this->keystone['server'].'tokens'; + $response = json_decode($this->request(self::POST, $url, $req), true); + $token = $response['access']['token']; + $timespan = abs(strtotime($token['expires']) - strtotime($token['issued_at'])); + $this->keystone_token_expires = $current_time->add(new DateInterval('PT'.$timespan.'S')); + $this->keystone_token = $token['id']; + } + + return $this->keystone_token; + } } /** From 0d582e2422eb969efbeb4ae46d477711b3a5986d Mon Sep 17 00:00:00 2001 From: aomurbekov Date: Fri, 5 Sep 2014 10:34:33 +0600 Subject: [PATCH 2/2] Remove keystone unused fields --- IronCore.class.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/IronCore.class.php b/IronCore.class.php index 18c1dd0..9c110c0 100644 --- a/IronCore.class.php +++ b/IronCore.class.php @@ -46,10 +46,6 @@ class IronCore protected $curl = null; protected $last_status; - protected $server; - protected $tenant; - protected $username; - protected $password; protected $keystone; protected $use_keystone; protected $keystone_token;