summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-11-17 06:01:32 +0000
committerGerrit Code Review <review@openstack.org>2014-11-17 06:01:32 +0000
commita9670b4cf0d2f8e98e62c25e6379311ef66ea01b (patch)
tree124de3a76614092186c1f66e3d302abcbf1c1fe3
parentb4ca5cc9c174494a392ff70f5631ac51e0103e30 (diff)
parent01a5ff6d3234457fd0f8268be13fca487a1793c2 (diff)
downloadpython-openstackclient-a9670b4cf0d2f8e98e62c25e6379311ef66ea01b.tar.gz
Merge "Add more session/api examples"
-rwxr-xr-xexamples/object_api.py106
-rwxr-xr-xexamples/osc-lib.py102
-rw-r--r--functional/tests/test_examples.py6
3 files changed, 214 insertions, 0 deletions
diff --git a/examples/object_api.py b/examples/object_api.py
new file mode 100755
index 00000000..5c6bd9f0
--- /dev/null
+++ b/examples/object_api.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+# object_api.py - Example object-store API usage
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""
+Object Store API Examples
+
+This script shows the basic use of the low-level Object Store API
+
+"""
+
+import argparse
+import logging
+import sys
+
+import common
+
+
+from openstackclient.api import object_store_v1 as object_store
+from openstackclient.identity import client as identity_client
+
+
+LOG = logging.getLogger('')
+
+
+def run(opts):
+ """Run the examples"""
+
+ # Set up certificate verification and CA bundle
+ # NOTE(dtroyer): This converts from the usual OpenStack way to the single
+ # requests argument and is an app-specific thing because
+ # we want to be like OpenStackClient.
+ if opts.os_cacert:
+ verify = opts.os_cacert
+ else:
+ verify = not opts.insecure
+
+ # get a session
+ # common.make_session() does all the ugly work of mapping
+ # CLI options/env vars to the required plugin arguments.
+ # The returned session will have a configured auth object
+ # based on the selected plugin's available options.
+ # So to do...oh, just go to api.auth.py and look at what it does.
+ session = common.make_session(opts, verify=verify)
+
+ # Extract an endpoint
+ auth_ref = session.auth.get_auth_ref(session)
+
+ if opts.os_url:
+ endpoint = opts.os_url
+ else:
+ endpoint = auth_ref.service_catalog.url_for(
+ service_type='object-store',
+ endpoint_type='public',
+ )
+
+ # At this point we have a working session with a configured authentication
+ # plugin. From here on it is the app making the decisions. Need to
+ # talk to two clouds? Go back and make another session but with opts
+ # set to different credentials. Or use a config file and load it
+ # directly into the plugin. This example doesn't show that (yet).
+ # Want to work ahead? Look into the plugin load_from_*() methods
+ # (start in keystoneclient/auth/base.py).
+
+ # This example is for the Object Store API so make one
+ obj_api = object_store.APIv1(
+ session=session,
+ service_type='object-store',
+ endpoint=endpoint,
+ )
+
+ # Do useful things with it
+
+ c_list = obj_api.container_list()
+ print("Name\tCount\tBytes")
+ for c in c_list:
+ print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))
+
+ if len(c_list) > 0:
+ # See what is in the first container
+ o_list = obj_api.object_list(c_list[0]['name'])
+ print("\nObject")
+ for o in o_list:
+ print("%s" % o)
+
+
+if __name__ == "__main__":
+ opts = common.base_parser(
+ identity_client.build_option_parser(
+ argparse.ArgumentParser(description='Object API Example')
+ )
+ ).parse_args()
+
+ common.configure_logging(opts)
+ sys.exit(common.main(opts, run))
diff --git a/examples/osc-lib.py b/examples/osc-lib.py
new file mode 100755
index 00000000..69fc5d98
--- /dev/null
+++ b/examples/osc-lib.py
@@ -0,0 +1,102 @@
+#!/usr/bin/env python
+# osc-lib.py - Example using OSC as a library
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""
+OpenStackClient Library Examples
+
+This script shows the basic use of the OpenStackClient ClientManager
+as a library.
+
+"""
+
+import argparse
+import logging
+import sys
+
+import common
+
+from openstackclient.common import clientmanager
+
+
+LOG = logging.getLogger('')
+
+
+def run(opts):
+ """Run the examples"""
+
+ # Loop through extensions to get API versions
+ # Currently API versions are statically selected. Once discovery
+ # is working this can go away...
+ api_version = {}
+ for mod in clientmanager.PLUGIN_MODULES:
+ version_opt = getattr(opts, mod.API_VERSION_OPTION, None)
+ if version_opt:
+ api = mod.API_NAME
+ api_version[api] = version_opt
+
+ # Set up certificate verification and CA bundle
+ # NOTE(dtroyer): This converts from the usual OpenStack way to the single
+ # requests argument and is an app-specific thing because
+ # we want to be like OpenStackClient.
+ if opts.os_cacert:
+ verify = opts.os_cacert
+ else:
+ verify = not opts.insecure
+
+ # Get a ClientManager
+ # Collect the auth and config options together and give them to
+ # ClientManager and it will wrangle all of the goons into place.
+ client_manager = clientmanager.ClientManager(
+ auth_options=opts,
+ verify=verify,
+ api_version=api_version,
+ )
+
+ # At this point we have a working client manager with a configured
+ # session and authentication plugin. From here on it is the app
+ # making the decisions. Need to talk to two clouds? Make another
+ # client manager with different opts. Or use a config file and load it
+ # directly into the plugin. This example doesn't show that (yet).
+
+ # Do useful things with it
+
+ # Look in the object store
+ c_list = client_manager.object_store.container_list()
+ print("Name\tCount\tBytes")
+ for c in c_list:
+ print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))
+
+ if len(c_list) > 0:
+ # See what is in the first container
+ o_list = client_manager.object_store.object_list(c_list[0]['name'])
+ print("\nObject")
+ for o in o_list:
+ print("%s" % o)
+
+ # Look at the compute flavors
+ flavor_list = client_manager.compute.flavors.list()
+ print("\nFlavors:")
+ for f in flavor_list:
+ print("%s" % f)
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description='ClientManager Example')
+ opts = common.base_parser(
+ clientmanager.build_plugin_option_parser(parser),
+ ).parse_args()
+
+ common.configure_logging(opts)
+ sys.exit(common.main(opts, run))
diff --git a/functional/tests/test_examples.py b/functional/tests/test_examples.py
index fdaa26b8..6e0e5867 100644
--- a/functional/tests/test_examples.py
+++ b/functional/tests/test_examples.py
@@ -20,3 +20,9 @@ class ExampleTests(test.TestCase):
# NOTE(stevemar): If an examples has a non-zero return
# code, then execute will raise an error by default.
test.execute('python', test.EXAMPLE_DIR + '/common.py --debug')
+
+ def test_object_api(self):
+ test.execute('python', test.EXAMPLE_DIR + '/object_api.py --debug')
+
+ def test_osc_lib(self):
+ test.execute('python', test.EXAMPLE_DIR + '/osc-lib.py --debug')