diff --git a/almanachclient/keystone_client.py b/almanachclient/keystone_client.py index 85afe85..158ea21 100644 --- a/almanachclient/keystone_client.py +++ b/almanachclient/keystone_client.py @@ -22,6 +22,16 @@ from almanachclient import exceptions class KeystoneClient(object): def __init__(self, auth_url, username, password, service, region_name, 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.username = username self.password = password @@ -30,6 +40,14 @@ class KeystoneClient(object): self.domain_name = domain_name 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'): """Get Almanach API URL from Keystone catalog @@ -37,7 +55,7 @@ class KeystoneClient(object): :return: Almanach Endpoint URL :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) for endpoint in endpoints: @@ -46,12 +64,11 @@ class KeystoneClient(object): raise exceptions.EndpointNotFound('Endpoint URL Not Found') - def _get_keystone_client(self): + def _get_session(self): auth = v3.Password(auth_url=self.auth_url, username=self.username, password=self.password, domain_name=self.domain_name, user_domain_id=self.user_domain_id) - sess = session.Session(auth=auth) - return keystone_client.Client(session=sess) + return session.Session(auth=auth) diff --git a/almanachclient/shell.py b/almanachclient/shell.py index 5dc088d..a8a9799 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -111,19 +111,20 @@ class AlmanachApp(app.App): return parser 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): - if self.options.almanach_url: - return self.options.almanach_url + def _get_almanach_token(self): + return self.options.almanach_token or self._get_keystone_client().get_token() - keystone = 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 _get_almanach_url(self): + return self.options.almanach_url or self._get_keystone_client().get_endpoint_url() - 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:]): diff --git a/almanachclient/tests/test_keystone_client.py b/almanachclient/tests/test_keystone_client.py index c12daf5..6b1fd99 100644 --- a/almanachclient/tests/test_keystone_client.py +++ b/almanachclient/tests/test_keystone_client.py @@ -30,6 +30,14 @@ class TestKeystoneClient(base.TestCase): self.region_name = 'some region' 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') def test_get_endpoint_url(self, keystone): endpoint_manager = mock.Mock() diff --git a/doc/source/usage_cli.rst b/doc/source/usage_cli.rst index 32557f3..0fb5d40 100644 --- a/doc/source/usage_cli.rst +++ b/doc/source/usage_cli.rst @@ -5,12 +5,23 @@ Environment variables --------------------- * :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_PASSWORD`: OpenStack password -* :code:`ALMANACH_SERVICE`: Almanach catalog service name -* :code:`ALMANACH_TOKEN`: Almanach API key -* :code:`ALMANACH_URL`: Almanach API base URL, override Keystone catalog lookup if specified +* :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 + +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 ------------------