Merge "Fetch token from Keystone if nothing is provided"

This commit is contained in:
Jenkins 2017-05-29 18:51:27 +00:00 committed by Gerrit Code Review
commit 2b18befd15
4 changed files with 55 additions and 18 deletions

View File

@ -22,6 +22,16 @@ from almanachclient import exceptions
class KeystoneClient(object): class KeystoneClient(object):
def __init__(self, auth_url, username, password, service, region_name, def __init__(self, auth_url, username, password, service, region_name,
domain_name='default', user_domain_id='default'): domain_name='default', user_domain_id='default'):
"""KeystoneClient
:arg str auth_url: Keystone URL (v3 endpoint)
:arg str username: Username
:arg str password: Password
:arg str service: Service name (example: almanach)
:arg str region_name: Region name
:arg str domain_name: Domain name
:arg str user_domain_id: User domain ID
"""
self.auth_url = auth_url self.auth_url = auth_url
self.username = username self.username = username
self.password = password self.password = password
@ -30,6 +40,14 @@ class KeystoneClient(object):
self.domain_name = domain_name self.domain_name = domain_name
self.user_domain_id = user_domain_id self.user_domain_id = user_domain_id
def get_token(self):
"""Get Keystone token
:rtype: str
"""
sess = self._get_session()
return sess.get_token()
def get_endpoint_url(self, visibility='admin'): def get_endpoint_url(self, visibility='admin'):
"""Get Almanach API URL from Keystone catalog """Get Almanach API URL from Keystone catalog
@ -37,7 +55,7 @@ class KeystoneClient(object):
:return: Almanach Endpoint URL :return: Almanach Endpoint URL
:rtype: str :rtype: str
""" """
keystone = self._get_keystone_client() keystone = keystone_client.Client(session=self._get_session())
endpoints = keystone.endpoints.list(service=self.service, region=self.region_name) endpoints = keystone.endpoints.list(service=self.service, region=self.region_name)
for endpoint in endpoints: for endpoint in endpoints:
@ -46,12 +64,11 @@ class KeystoneClient(object):
raise exceptions.EndpointNotFound('Endpoint URL Not Found') raise exceptions.EndpointNotFound('Endpoint URL Not Found')
def _get_keystone_client(self): def _get_session(self):
auth = v3.Password(auth_url=self.auth_url, auth = v3.Password(auth_url=self.auth_url,
username=self.username, username=self.username,
password=self.password, password=self.password,
domain_name=self.domain_name, domain_name=self.domain_name,
user_domain_id=self.user_domain_id) user_domain_id=self.user_domain_id)
sess = session.Session(auth=auth) return session.Session(auth=auth)
return keystone_client.Client(session=sess)

View File

@ -111,19 +111,20 @@ class AlmanachApp(app.App):
return parser return parser
def get_client(self): def get_client(self):
return Client(self.get_almanach_url(), token=self.options.almanach_token) return Client(self._get_almanach_url(), self._get_almanach_token())
def get_almanach_url(self): def _get_almanach_token(self):
if self.options.almanach_url: return self.options.almanach_token or self._get_keystone_client().get_token()
return self.options.almanach_url
keystone = KeystoneClient(auth_url=self.options.os_auth_url, def _get_almanach_url(self):
username=self.options.os_username, return self.options.almanach_url or self._get_keystone_client().get_endpoint_url()
password=self.options.os_password,
service=self.options.almanach_service,
region_name=self.options.os_region_name)
return keystone.get_endpoint_url() def _get_keystone_client(self):
return KeystoneClient(auth_url=self.options.os_auth_url,
username=self.options.os_username,
password=self.options.os_password,
service=self.options.almanach_service,
region_name=self.options.os_region_name)
def main(argv=sys.argv[1:]): def main(argv=sys.argv[1:]):

View File

@ -30,6 +30,14 @@ class TestKeystoneClient(base.TestCase):
self.region_name = 'some region' self.region_name = 'some region'
self.client = KeystoneClient(self.auth_url, self.username, self.password, self.service, self.region_name) self.client = KeystoneClient(self.auth_url, self.username, self.password, self.service, self.region_name)
@mock.patch('keystoneauth1.session.Session')
def test_get_token(self, session):
sess = mock.Mock()
sess.get_token.return_value = 'some token'
session.return_value = sess
self.assertEqual('some token', self.client.get_token())
sess.get_token.assert_called_with()
@mock.patch('keystoneclient.v3.client.Client') @mock.patch('keystoneclient.v3.client.Client')
def test_get_endpoint_url(self, keystone): def test_get_endpoint_url(self, keystone):
endpoint_manager = mock.Mock() endpoint_manager = mock.Mock()

View File

@ -5,12 +5,23 @@ Environment variables
--------------------- ---------------------
* :code:`OS_AUTH_URL`: Keystone URL (v3 endpoint) * :code:`OS_AUTH_URL`: Keystone URL (v3 endpoint)
* :code:`OS_AUTH_URL`: OpenStack region name * :code:`OS_REGION_NAME`: OpenStack region name
* :code:`OS_USERNAME`: OpenStack username * :code:`OS_USERNAME`: OpenStack username
* :code:`OS_PASSWORD`: OpenStack password * :code:`OS_PASSWORD`: OpenStack password
* :code:`ALMANACH_SERVICE`: Almanach catalog service name * :code:`ALMANACH_SERVICE`: Almanach Keystone catalog service name
* :code:`ALMANACH_TOKEN`: Almanach API key * :code:`ALMANACH_TOKEN`: Almanach API token, if empty a token will be fetched from Keystone
* :code:`ALMANACH_URL`: Almanach API base URL, override Keystone catalog lookup if specified * :code:`ALMANACH_URL`: Almanach API base URL, if empty the endpoint will be fetched from Keystone catalog
Command Line Arguments
----------------------
* :code:`--os-auth-url`: Keystone URL (v3 endpoint)
* :code:`--os-region-name`: OpenStack region name
* :code:`--os-username`: OpenStack username
* :code:`--os-password`: OpenStack password
* :code:`--almanach-service`: Almanach Keystone catalog service name
* :code:`--almanach-token`: Almanach API token, if empty a token will be fetched from Keystone
* :code:`--almanach-url`: Almanach API base URL, if empty the endpoint will be fetched from Keystone catalog
Get server version Get server version
------------------ ------------------