From 2339243e66b676325c84693e7f8199d476199203 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Thu, 5 Nov 2015 17:00:33 -0500 Subject: [PATCH] Add method to get a mounted session from config Getting a session is great, but sometimes you need a thing called an "adapter" which takes 5 parameters which are all already contained in the config that you used to get the session. Change-Id: Id4e418cd04ae81540d9898f7b2e959b974f355d2 --- os_client_config/__init__.py | 17 +++++++++++++++++ os_client_config/cloud_config.py | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/os_client_config/__init__.py b/os_client_config/__init__.py index d5fd36c..00e6ff5 100644 --- a/os_client_config/__init__.py +++ b/os_client_config/__init__.py @@ -13,3 +13,20 @@ # under the License. from os_client_config.config import OpenStackConfig # noqa + + +def simple_client(service_key, cloud=None, region_name=None): + """Simple wrapper function. It has almost no features. + + This will get you a raw requests Session Adapter that is mounted + on the given service from the keystone service catalog. If you leave + off cloud and region_name, it will assume that you've got env vars + set, but if you give them, it'll use clouds.yaml as you'd expect. + + This function is deliberately simple. It has no flexibility. If you + want flexibility, you can make a cloud config object and call + get_session_client on it. This function is to make it easy to poke + at OpenStack REST APIs with a properly configured keystone session. + """ + return OpenStackConfig().get_one_cloud( + cloud=cloud, region_name=region_name).get_session_client('compute') diff --git a/os_client_config/cloud_config.py b/os_client_config/cloud_config.py index 8e0b963..7cab1ac 100644 --- a/os_client_config/cloud_config.py +++ b/os_client_config/cloud_config.py @@ -14,6 +14,7 @@ import warnings +from keystoneauth1 import adapter from keystoneauth1 import plugin from keystoneauth1 import session @@ -160,6 +161,28 @@ class CloudConfig(object): timeout=self.config['api_timeout']) return self._keystone_session + def get_session_client(self, service_key): + """Return a prepped requests adapter for a given service. + + This is useful for making direct requests calls against a + 'mounted' endpoint. That is, if you do: + + client = get_session_client('compute') + + then you can do: + + client.get('/flavors') + + and it will work like you think. + """ + + return adapter.Adapter( + session=self.get_session(), + service_type=self.get_service_type(service_key), + service_name=self.get_service_name(service_key), + interface=self.get_interface(service_key), + region_name=self.region) + def get_session_endpoint(self, service_key): """Return the endpoint from config or the catalog.