|
| 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)) |
0 commit comments