diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-11-12 13:40:15 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-11-12 13:40:15 +0000 |
commit | 38f959b1b592fb1d5ab8f2387f506ac851ae8b4f (patch) | |
tree | 37e343fa35b519be45c35e60e2453421804ef862 | |
parent | dfe21d75b8a1dab2d5b8b8910886635801a58f8c (diff) | |
parent | 2339243e66b676325c84693e7f8199d476199203 (diff) | |
download | os-client-config-38f959b1b592fb1d5ab8f2387f506ac851ae8b4f.tar.gz |
Merge "Add method to get a mounted session from config"
-rw-r--r-- | os_client_config/__init__.py | 17 | ||||
-rw-r--r-- | os_client_config/cloud_config.py | 23 |
2 files changed, 40 insertions, 0 deletions
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 710bb5e..3f46c6e 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 import requestsexceptions @@ -151,6 +152,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. |