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
This commit is contained in:
Monty Taylor 2015-11-05 17:00:33 -05:00
parent 1b91e007fd
commit 2339243e66
2 changed files with 40 additions and 0 deletions

View File

@ -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')

View File

@ -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.