Skip to content

Commit 01a5ff6

Browse files
author
Dean Troyer
committed
Add more session/api examples
* examples/object_api.py - Example of using the Object_Store API * examples/osc-lib.py - Minimal client to use ClientManager as a library Also add matching functional tests Change-Id: I4243a21141a821420951d4b6352d41029cdcccbc
1 parent 126b2c5 commit 01a5ff6

3 files changed

Lines changed: 214 additions & 0 deletions

File tree

examples/object_api.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
# object_api.py - Example object-store API usage
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
"""
17+
Object Store API Examples
18+
19+
This script shows the basic use of the low-level Object Store API
20+
21+
"""
22+
23+
import argparse
24+
import logging
25+
import sys
26+
27+
import common
28+
29+
30+
from openstackclient.api import object_store_v1 as object_store
31+
from openstackclient.identity import client as identity_client
32+
33+
34+
LOG = logging.getLogger('')
35+
36+
37+
def run(opts):
38+
"""Run the examples"""
39+
40+
# Set up certificate verification and CA bundle
41+
# NOTE(dtroyer): This converts from the usual OpenStack way to the single
42+
# requests argument and is an app-specific thing because
43+
# we want to be like OpenStackClient.
44+
if opts.os_cacert:
45+
verify = opts.os_cacert
46+
else:
47+
verify = not opts.insecure
48+
49+
# get a session
50+
# common.make_session() does all the ugly work of mapping
51+
# CLI options/env vars to the required plugin arguments.
52+
# The returned session will have a configured auth object
53+
# based on the selected plugin's available options.
54+
# So to do...oh, just go to api.auth.py and look at what it does.
55+
session = common.make_session(opts, verify=verify)
56+
57+
# Extract an endpoint
58+
auth_ref = session.auth.get_auth_ref(session)
59+
60+
if opts.os_url:
61+
endpoint = opts.os_url
62+
else:
63+
endpoint = auth_ref.service_catalog.url_for(
64+
service_type='object-store',
65+
endpoint_type='public',
66+
)
67+
68+
# At this point we have a working session with a configured authentication
69+
# plugin. From here on it is the app making the decisions. Need to
70+
# talk to two clouds? Go back and make another session but with opts
71+
# set to different credentials. Or use a config file and load it
72+
# directly into the plugin. This example doesn't show that (yet).
73+
# Want to work ahead? Look into the plugin load_from_*() methods
74+
# (start in keystoneclient/auth/base.py).
75+
76+
# This example is for the Object Store API so make one
77+
obj_api = object_store.APIv1(
78+
session=session,
79+
service_type='object-store',
80+
endpoint=endpoint,
81+
)
82+
83+
# Do useful things with it
84+
85+
c_list = obj_api.container_list()
86+
print("Name\tCount\tBytes")
87+
for c in c_list:
88+
print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))
89+
90+
if len(c_list) > 0:
91+
# See what is in the first container
92+
o_list = obj_api.object_list(c_list[0]['name'])
93+
print("\nObject")
94+
for o in o_list:
95+
print("%s" % o)
96+
97+
98+
if __name__ == "__main__":
99+
opts = common.base_parser(
100+
identity_client.build_option_parser(
101+
argparse.ArgumentParser(description='Object API Example')
102+
)
103+
).parse_args()
104+
105+
common.configure_logging(opts)
106+
sys.exit(common.main(opts, run))

examples/osc-lib.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
# osc-lib.py - Example using OSC as a library
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
"""
17+
OpenStackClient Library Examples
18+
19+
This script shows the basic use of the OpenStackClient ClientManager
20+
as a library.
21+
22+
"""
23+
24+
import argparse
25+
import logging
26+
import sys
27+
28+
import common
29+
30+
from openstackclient.common import clientmanager
31+
32+
33+
LOG = logging.getLogger('')
34+
35+
36+
def run(opts):
37+
"""Run the examples"""
38+
39+
# Loop through extensions to get API versions
40+
# Currently API versions are statically selected. Once discovery
41+
# is working this can go away...
42+
api_version = {}
43+
for mod in clientmanager.PLUGIN_MODULES:
44+
version_opt = getattr(opts, mod.API_VERSION_OPTION, None)
45+
if version_opt:
46+
api = mod.API_NAME
47+
api_version[api] = version_opt
48+
49+
# Set up certificate verification and CA bundle
50+
# NOTE(dtroyer): This converts from the usual OpenStack way to the single
51+
# requests argument and is an app-specific thing because
52+
# we want to be like OpenStackClient.
53+
if opts.os_cacert:
54+
verify = opts.os_cacert
55+
else:
56+
verify = not opts.insecure
57+
58+
# Get a ClientManager
59+
# Collect the auth and config options together and give them to
60+
# ClientManager and it will wrangle all of the goons into place.
61+
client_manager = clientmanager.ClientManager(
62+
auth_options=opts,
63+
verify=verify,
64+
api_version=api_version,
65+
)
66+
67+
# At this point we have a working client manager with a configured
68+
# session and authentication plugin. From here on it is the app
69+
# making the decisions. Need to talk to two clouds? Make another
70+
# client manager with different opts. Or use a config file and load it
71+
# directly into the plugin. This example doesn't show that (yet).
72+
73+
# Do useful things with it
74+
75+
# Look in the object store
76+
c_list = client_manager.object_store.container_list()
77+
print("Name\tCount\tBytes")
78+
for c in c_list:
79+
print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))
80+
81+
if len(c_list) > 0:
82+
# See what is in the first container
83+
o_list = client_manager.object_store.object_list(c_list[0]['name'])
84+
print("\nObject")
85+
for o in o_list:
86+
print("%s" % o)
87+
88+
# Look at the compute flavors
89+
flavor_list = client_manager.compute.flavors.list()
90+
print("\nFlavors:")
91+
for f in flavor_list:
92+
print("%s" % f)
93+
94+
95+
if __name__ == "__main__":
96+
parser = argparse.ArgumentParser(description='ClientManager Example')
97+
opts = common.base_parser(
98+
clientmanager.build_plugin_option_parser(parser),
99+
).parse_args()
100+
101+
common.configure_logging(opts)
102+
sys.exit(common.main(opts, run))

functional/tests/test_examples.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ def test_common(self):
2020
# NOTE(stevemar): If an examples has a non-zero return
2121
# code, then execute will raise an error by default.
2222
test.execute('python', test.EXAMPLE_DIR + '/common.py --debug')
23+
24+
def test_object_api(self):
25+
test.execute('python', test.EXAMPLE_DIR + '/object_api.py --debug')
26+
27+
def test_osc_lib(self):
28+
test.execute('python', test.EXAMPLE_DIR + '/osc-lib.py --debug')

0 commit comments

Comments
 (0)