From 3e5cec75c1fcb6543de8d779243fba1d05dd940d Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 22 Feb 2016 07:39:02 -0800 Subject: Add post about using os-client-config --- src/posts/simple-openstack-clients.hbs | 130 +++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/posts/simple-openstack-clients.hbs diff --git a/src/posts/simple-openstack-clients.hbs b/src/posts/simple-openstack-clients.hbs new file mode 100644 index 0000000..6d7b6e4 --- /dev/null +++ b/src/posts/simple-openstack-clients.hbs @@ -0,0 +1,130 @@ + + + + Simple OpenStack access with os-client-config + + + +

Simple things should be simple, and nowhere is that more true than in + constructing objects to interface with clouds. What you wnat to do is just + interact with the cloud - what you often are required to do is generate + sequences of object constructions because there are a bunch of things you + might have wanted to do.

+ +

In the spirit of making the simple things simple, we recently added a + couple of simple helper factory functions to + + os-client-config: One to create Client objects, and one to create + mounted Session objects. Both functions fully support the breadth of + config options afforded by os-client-config, including environment + variables, config files and command line options.

+ +

Examples are probably best

+ +

Client Library Client Objects

+ +

Make a nova client object that uses env vars for auth info:

+ +

+client = os_client_config.make_client('compute')
+    
+ +

Make a glance client object for the cloud named 'mtvexx' in my + clouds.yaml file:

+ +

+client = os_client_config.make_client('image', cloud='mtvxx')
+    
+ +

Make a neutron client by passing in all of the values directly:

+ +

+client = os_client_config.make_client(
+    'network',
+    auth_url=OS_AUTH_URL, username=OS_USERNAME,
+    password=OS_PASSWORD, project_name=OS_PROJECT_NAME,
+    region_name=OS_REGION_NAME)
+    
+ +

Make a barbican client from env vars with all the standard arguments + passed on the comand line:

+ +

+import argparse
+client = os_client_config.make_client(
+    'key-manager', options=argparse.ArgumentParser())
+    
+ +

That likely covers most of the common end-user use cases with using + OpenStack Client Libraries. If you need more flexibility, you can always + create an os-client-config CloudConfig object and call + get_legacy_client, but I'm a big believer in one step instead of three + if you can get away with it:

+ +

+config = os_client_config.OpenStackConfig()
+cloud_config = config.get_one_cloud(cloud='vexxhost')
+client = cloud_config.get_legacy_client('compute')
+    
+ +

Mounted Keystone Session

+ +

What if what you want to do is make some direct REST calls to an + OpenStack service, but you want to be able to do env vars or argparse or + clouds.yaml files to configure your authentication?

+ +

Well - you're in luck!

+ +

Make a Session object for nova that uses env vars for auth info:

+ +

+client = os_client_config.session_client('compute')
+    
+ +

That will get you a keystoneauth Session object that has been "mounted" on the compute service. So you can do this:

+ +

+response = session.get('/servers')
+server_list = response.json()['servers']
+    
+ +

The same argument sequence used for make_client works for + session_client. + +

Make a glance Session object for the cloud named 'mtvexx' in my + clouds.yaml file:

+ +

+session = os_client_config.session_client('image', cloud='mtvxx')
+    
+ +

Make a neutron Session object by passing in all of the values + directly:

+ +

+session = os_client_config.session_client(
+    'network',
+    auth_url=OS_AUTH_URL, username=OS_USERNAME,
+    password=OS_PASSWORD, project_name=OS_PROJECT_NAME,
+    region_name=OS_REGION_NAME)
+    
+ +

Make a barbican Session object from env vars with all the standard + arguments passed on the comand line:

+ +

+import argparse
+session = os_client_config.session_client(
+    'key-manager', options=argparse.ArgumentParser())
+    
+ +

Do it all without syntactic help:

+ +

+config = os_client_config.OpenStackConfig()
+cloud_config = config.get_one_cloud(cloud='vexxhost')
+session = cloud_config.get_session_client('compute')
+    
+ + + -- cgit v1.2.3