diff --git a/examples/action.py b/examples/action.py deleted file mode 100644 index e6db288e..00000000 --- a/examples/action.py +++ /dev/null @@ -1,75 +0,0 @@ -# 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. - -""" -Example Action Command - -Invoke an action method on a resource. - -A session will be provided to the action if needed. The action itself -must be identified by the 'action' key. Arguments to the action may be -provided through an 'action_args' dictionary. - -For examples: - python -m examples.action openstack/telemetry/v2/alarm.py \ - --data '{"alarm_id": "33109eea-24dd-45ff-93f7-82292d1dd38c", - "action": "change_state", - "action_args": {"next_state": "insufficient data"}' - - python -m examples.action openstack/compute/v2/server.py \ - --data '{"id": "a1369557-748f-429c-bd3e-fc385aacaec7", - "action": "reboot", - "action_args": {"reboot_type": "SOFT"}}' -""" - -import inspect -import sys - -from examples import common -from examples import session - - -def filter_args(method, params): - expected_args = inspect.getargspec(method).args - accepted_args = ([a for a in expected_args if a != 'self']) - filtered_args = {desired: params[desired] for desired in accepted_args} - return filtered_args - - -def invoke_method(target, method_name, params): - action = getattr(target, method_name) - filtered_args = filter_args(action, params) - reply = action(**filtered_args) - return reply - - -def run_action(options): - sess = session.make_session(options) - cls = common.find_resource_cls(options) - data = common.get_data_option(options) - - action = data.pop('action') - if 'action_args' in data: - args = data.pop('action_args') - else: - args = {} - args.update(session=sess) - - obj = cls.new(**data) - reply = invoke_method(obj, action, args) - print(str(reply)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_action)) diff --git a/examples/authenticate.py b/examples/authenticate.py deleted file mode 100644 index b674492c..00000000 --- a/examples/authenticate.py +++ /dev/null @@ -1,47 +0,0 @@ -# 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. - -""" -Authentication example - -To authenticate you must have the environment variables set or use the -command line options. This is a good example to start with because once -you know you can authenticate, you can perform other operations that -require authentication. Refer to the example common.py for the environment -variables or command line options to use. - -If you use the environment variables, authenticate with: - - python -m examples.authenticate - -""" - -import sys - -from examples import common -from examples import connection -from examples import transport - - -def make_authenticate(opts): - return connection.make_connection(opts).session.authenticator - - -def run_authenticate(opts): - auth = make_authenticate(opts) - xport = transport.make_transport(opts) - print(auth.authorize(xport)) - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_authenticate)) diff --git a/examples/cloud-init.sh b/examples/cloud-init.sh deleted file mode 100644 index c806e116..00000000 --- a/examples/cloud-init.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash -x -echo '*** start cloud-init ***' -wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - -echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list -apt-get update -apt-get install -y jenkins -echo 'JENKINS_ARGS="${JENKINS_ARGS} --argumentsRealm.passwd.jenkins=demo --argumentsRealm.roles.jenkins=admin"' >> /etc/default/jenkins -cat >/var/lib/jenkins/config.xml < - - - 1.0 - 2 - NORMAL - true - - - false - - \${ITEM_ROOTDIR}/workspace - \${ITEM_ROOTDIR}/builds - - - - - - - 0 - - - - All - false - false - - - - All - 0 - - - - -! -cat >/var/lib/jenkins/jenkins.security.QueueItemAuthenticatorConfiguration.xml < - - - -! -service jenkins restart -echo "*** stop cloud-init ***\n" - diff --git a/examples/common.py b/examples/common.py deleted file mode 100755 index e7a867a3..00000000 --- a/examples/common.py +++ /dev/null @@ -1,160 +0,0 @@ -#!/usr/bin/env python -# common.py - Common bits for SDK examples - -# 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. - -""" -SDK Examples Common - -This is a collection of common functions used by the example scripts. - -common.object_parser() provides the common set of command-line arguments -used in the library CLIs for setting up authentication. This should make -playing with the example scripts against a running OpenStack simpler. - -Typical environment variables to set and export for authentication include: - - OS_PROJECT_NAME=FooFighter - OS_PASSWORD=nirvana - OS_AUTH_URL=https://foofighters.com:35357/v3 - OS_USERNAME=davegrohl - OS_REGION_NAME=Seattle - -""" - -import argparse -import logging -import os -import sys -import traceback -import uuid - -from openstack import utils - -_logger = logging.getLogger('openstack.example') - - -def find_resource_cls(opts): - argument = opts.argument - if argument.find('/') > 0: - # called with file e.g.: openstack/network/v2/network.py - args = argument.split('/') - args[-1] = args[-1].replace('.py', '') - from_str = '.'.join(args) - class_str = args[-1].title() - class_str = class_str.replace('_', '') - else: - # called with path e.g.: openstack.network.v2.network.Network - args = argument.rpartition('.') - from_str = args[0] - class_str = args[2] - __import__(from_str) - mod = sys.modules[from_str] - return getattr(mod, class_str) - - -def get_data_option(opts): - if not opts.data: - return opts.data - try: - iddy = uuid.UUID(opts.data) - return {'id': iddy} - except ValueError: - data = opts.data - if data.startswith('openstack.'): - fullname = data.split('(')[0] - classname = fullname.split('.')[-1] - modulename = fullname.replace('.' + classname, '') - data = data.replace('openstack.', - '__import__("' + modulename + '").') - return eval(data) - - -def env(*vars, **kwargs): - """Search for the first defined of possibly many env vars - - Returns the first environment variable defined in vars, or - returns the default defined in kwargs. - - """ - for v in vars: - value = os.environ.get(v, None) - if value: - return value - return kwargs.get('default', '') - - -def option_parser(): - """Set up some of the common CLI options - - These are the basic options that match the library CLIs so - command-line/environment setups for those also work with these - demonstration programs. - - """ - - parser = argparse.ArgumentParser( - description='A demonstration framework') - # Global arguments - parser.add_argument( - '--os-cloud', - dest='cloud', - metavar='', - default=env('OS_CLOUD', default=None), - help=('Cloud configuration from ' + - 'https://pypi.python.org/pypi/os-client-config (Env: OS_CLOUD)') - ) - parser.add_argument( - '--data', - metavar='', - default={}, - help='Json data for command.', - ) - parser.add_argument( - '-v', '--verbose', - action='count', - dest='verbose_level', - default=1, - help='Increase verbosity of output. Can be repeated.', - ) - parser.add_argument( - - '--debug', - default=False, - action='store_true', - help='show tracebacks on errors', - ) - parser.add_argument( - 'argument', - default=None, - nargs='?', - help='Argument to use.', - ) - return parser - - -def setup(): - opts = option_parser().parse_args() - utils.enable_logging(opts.debug, stream=sys.stdout) - return opts - - -def main(opts, run): - try: - return run(opts) - except Exception as e: - if opts.debug: - _logger.error(traceback.format_exc(e)) - else: - _logger.error('Exception raised: ' + str(e)) - return 1 diff --git a/examples/connection.py b/examples/connection.py deleted file mode 100644 index e1fb6f8d..00000000 --- a/examples/connection.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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. - -""" -Example Connection Command - -Make sure you can authenticate before running this command. - -For example: - python -m examples.connection -""" - -import sys - -from examples import common -from openstack import connection - - -def make_connection(opts): - return connection.from_config(opts) - - -def run_connection(opts): - conn = make_connection(opts) - print("Connection: %s" % conn) - for resource in conn.compute.flavors(): - print(resource) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_connection)) diff --git a/examples/create.py b/examples/create.py deleted file mode 100644 index 43604ecd..00000000 --- a/examples/create.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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. - -""" -Example Create Command - -For example: - python -m examples.create openstack/network/v2/network.py \ - --data '{"name": "foo"}' -""" - -import sys - -from examples import common -from examples import session - - -def run_create(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - obj = cls.new(**common.get_data_option(opts)) - obj.create(sess) - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_create)) diff --git a/examples/delete.py b/examples/delete.py deleted file mode 100644 index b561c920..00000000 --- a/examples/delete.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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. - -""" -Example Delete Command - -For example: - python -m examples.delete openstack/network/v2/network.py \ - --data '{"id": "a1369557-748f-429c-bd3e-fc385aacaec7"}' -""" - -import sys - -from examples import common -from examples import session - - -def run_delete(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - data = common.get_data_option(opts) - obj = cls.new(**data) - obj.delete(sess) - print('Deleted: %s' % str(data)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_delete)) diff --git a/examples/find.py b/examples/find.py deleted file mode 100644 index ad5dc54c..00000000 --- a/examples/find.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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. - -""" -Example Find Command - -For example: - python -m examples.find openstack/network/v2/network.py \ - --data '{"id": "a1369557-748f-429c-bd3e-fc385aacaec7"}' -""" - -import sys - -from examples import common -from examples import session - - -def run_find(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - obj = cls.find(sess, opts.data) - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_find)) diff --git a/examples/get.py b/examples/get.py deleted file mode 100644 index 543349f8..00000000 --- a/examples/get.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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. - -""" -Example Get Command - -For example: - python -m examples.get openstack/network/v2/network.py \ - --data '{"id": "a1369557-748f-429c-bd3e-fc385aacaec7"}' -""" - -import sys - -from examples import common -from examples import session - - -def run_get(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - data = common.get_data_option(opts) - obj = cls.new(**data) - obj.get(sess) - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_get)) diff --git a/examples/head.py b/examples/head.py deleted file mode 100644 index 87e80de3..00000000 --- a/examples/head.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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. - -""" -Example Head Command - -For example: - python -m examples.head openstack/image/v1/image.py \ - --data 9d7d22d0-7d43-481f-a7eb-d93ea2791409 -""" - -import sys - -from examples import common -from examples import session - - -def run_head(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - data = common.get_data_option(opts) - obj = cls.new(**data) - obj.head(sess) - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_head)) diff --git a/examples/jenkins/create.py b/examples/jenkins/create.py deleted file mode 100644 index d1e9f233..00000000 --- a/examples/jenkins/create.py +++ /dev/null @@ -1,85 +0,0 @@ -# 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. - -""" -Example Create a jenkins server - -Create all the pieces parts to get a jenkins server up and running. - -To run: - python examples/jenkins/create.py -""" - -import base64 -import sys - -from examples import common -from examples import connection -from examples.keypair import create as keypair -from examples.network import create as network - - -def create_jenkins(conn, name, opts): - flavor = opts.data.pop('flavor', '103') - image = opts.data.pop('image', 'bec3cab5-4722-40b9-a78a-3489218e22fe') - - ports = [9022, 443, 80, 8080, 422, 22] - net = network.create(conn, name, opts, ports_to_open=ports) - keypair.create(conn, name) - - server = conn.compute.find_server(name) - if server is None: - f = open('examples/cloud-init.sh', 'r') - cmd = f.read() - f.close() - b64str = base64.b64encode(cmd) - args = { - "name": name, - "flavorRef": flavor, - "imageRef": image, - "key_name": name, - "networks": [{"uuid": net.id}], - "user_data": b64str, - } - server = conn.compute.create_server(**args) - else: - server = conn.get(server) - print(str(server)) - print('Waiting for the server to come up....') - conn.compute.wait_for_server(server) - print('Server is up.') - - if len(server.get_floating_ips()) <= 0: - extnet = conn.network.find_network("Ext-Net") - ip = conn.network.find_available_ip() - if ip is None: - ip = conn.network.create_ip(floating_network_id=extnet.id) - port = next(conn.network.list_ports(device_id=server.id, fields='id')) - conn.network.add_ip_to_port(port, ip) - print(str(port)) - ip = conn.get(ip) - print("ssh -i jenkins ubuntu@%s" % ip.floating_ip_address) - print("http://%s:8080" % ip.floating_ip_address) - print("login jenkins/demo") - - return - - -def run_jenkins(opts): - conn = connection.make_connection(opts) - name = opts.data.pop('name', 'jenkins') - return(create_jenkins(conn, name, opts)) - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_jenkins)) diff --git a/examples/jenkins/delete.py b/examples/jenkins/delete.py deleted file mode 100644 index bb93c404..00000000 --- a/examples/jenkins/delete.py +++ /dev/null @@ -1,55 +0,0 @@ -# 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. - -""" -Example Delete a jenkins server - -Delete all the pieces parts to your jenkins server. - -To run: - python examples/jenkins/delete.py -""" - -import sys - -from examples import common -from examples import connection -from examples.keypair import delete as keypair -from examples.network import delete as network - - -def delete_jenkins(conn, name, opts): - server = conn.compute.find_server(name) - if server is not None: - server = conn.get(server) - print(str(server)) - ips = server.get_floating_ips() - for ip in ips: - print(str(ip)) - ip = conn.network.find_ip(ip) - conn.network.remove_ip_from_port(ip) - conn.network.delete_ip(ip) - conn.compute.delete_server(server) - - keypair.delete(conn, name) - network.delete(conn, name) - - -def run_jenkins(opts): - conn = connection.make_connection(opts) - name = opts.data.pop('name', 'jenkins') - return(delete_jenkins(conn, name, opts)) - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_jenkins)) diff --git a/examples/keypair/__init__.py b/examples/keypair/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/keypair/create.py b/examples/keypair/create.py deleted file mode 100644 index e44b8801..00000000 --- a/examples/keypair/create.py +++ /dev/null @@ -1,68 +0,0 @@ -# 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. - -""" -Keypair example - -Create a working keypair. - -To run: - python examples/keypair/create.py -""" - -import os -import sys - -from examples import common -from examples import connection - - -def create(conn, name): - - kp = conn.compute.find_keypair(name) - if kp is None: - dirname = os.path.expanduser("~/.ssh") - try: - os.mkdir(dirname, 0o700) - except OSError: - pass - filename = os.path.join(dirname, name) - filenamepub = filename + '.pub' - args = {'name': name} - pubkey = None - try: - with open(filenamepub, 'r') as f: - pubkey = f.read() - args['public_key'] = pubkey - except IOError: - pass - kp = conn.compute.create_keypair(**args) - if pubkey is None: - with open(filename, 'w') as f: - f.write("%s" % kp.private_key) - with open(filenamepub, 'w') as f: - f.write("%s" % kp.public_key) - os.chmod(filename, 0o640) - os.chmod(filenamepub, 0o644) - print(str(kp)) - return kp - - -def run_keypair(opts): - name = opts.data.pop('name', 'pare') - conn = connection.make_connection(opts) - return(create(conn, name)) - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_keypair)) diff --git a/examples/keypair/delete.py b/examples/keypair/delete.py deleted file mode 100644 index fe291781..00000000 --- a/examples/keypair/delete.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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. - -""" -Keypair example - -Destroy a keypair. - -To run: - python examples/keypair/delete.py -""" - -import sys - -from examples import common -from examples import connection - - -def delete(conn, name): - kp = conn.compute.find_keypair(name) - if kp is not None: - print(str(kp)) - conn.compute.delete_keypair(kp) - - -def run_keypair(opts): - name = opts.data.pop('name', 'pare') - conn = connection.make_connection(opts) - return(delete(conn, name)) - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_keypair)) diff --git a/examples/list.py b/examples/list.py deleted file mode 100644 index 5b916d9e..00000000 --- a/examples/list.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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. - -import sys - -from examples import common -from examples import session - - -def run_list(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - path_args = None - if opts.data: - path_args = common.get_data_option(opts) - for obj in cls.list(sess, path_args=path_args, paginated=True): - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_list)) diff --git a/examples/object_store.py b/examples/object_store.py deleted file mode 100644 index 5404c28e..00000000 --- a/examples/object_store.py +++ /dev/null @@ -1,104 +0,0 @@ -# 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. - -from __future__ import print_function - -import glob -import os -import sys - -from examples import common -from openstack import connection - -CONTAINER_HEADER = ("Name{0}| Bytes Used{1}| " - "Num Objects".format(13 * " ", 1 * " ")) -CONTAINER_FORMAT = ("{0.name: <16} | {0.bytes: <10} | {0.count}") -OBJECT_HEADER = ("Name{0}| Bytes {1}| " - "Content-Type".format(27 * " ", 2 * " ")) -OBJECT_FORMAT = ("{0.name: <30} | {0.bytes: <7} | {0.content_type}") - - -def list_containers(conn): - print(CONTAINER_HEADER) - print("=" * len(CONTAINER_HEADER)) - for container in conn.object_store.containers(): - print(CONTAINER_FORMAT.format(container)) - - -def list_objects(conn, container): - print(OBJECT_HEADER) - print("=" * len(OBJECT_HEADER)) - for obj in conn.object_store.objects(container.decode("utf8")): - print(OBJECT_FORMAT.format(obj)) - - -def upload_directory(conn, directory, pattern): - """Upload a directory to object storage. - - Given an OpenStack connection, a directory, and a file glob pattern, - upload all files matching the pattern from that directory into a - container named after the directory containing the files. - """ - container_name = os.path.basename(os.path.realpath(directory)) - - container = conn.object_store.create_container( - container_name.decode("utf8")) - - for root, dirs, files in os.walk(directory): - for file in glob.iglob(os.path.join(root, pattern)): - with open(file, "rb") as f: - ob = conn.object_store.create_object(data=f.read(), - obj=file.decode("utf8"), - container=container) - print("Uploaded {0.name}".format(ob)) - - -def main(): - # Add on to the common parser with a few options of our own. - parser = common.option_parser() - - parser.add_argument("--list-containers", dest="list_containers", - action="store_true") - parser.add_argument("--list-objects", dest="container") - parser.add_argument("--upload-directory", dest="directory") - parser.add_argument("--pattern", dest="pattern") - - opts = parser.parse_args() - - args = { - 'auth_plugin': opts.auth_plugin, - 'auth_url': opts.auth_url, - 'project_name': opts.project_name, - 'domain_name': opts.domain_name, - 'project_domain_name': opts.project_domain_name, - 'user_domain_name': opts.user_domain_name, - 'username': opts.username, - 'password': opts.password, - 'verify': opts.verify, - 'token': opts.token, - } - conn = connection.Connection(**args) - - if opts.list_containers: - return list_containers(conn) - elif opts.container: - return list_objects(conn, opts.container) - elif opts.directory and opts.pattern: - return upload_directory(conn, opts.directory.decode("utf8"), - opts.pattern) - else: - print(parser.print_help()) - - return -1 - -if __name__ == "__main__": - sys.exit(main()) diff --git a/examples/session.py b/examples/session.py deleted file mode 100644 index fe55a7cf..00000000 --- a/examples/session.py +++ /dev/null @@ -1,47 +0,0 @@ -# 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. - -""" -Example Session Command - -Make sure you can authenticate before running this command. This command -is currently hard coded to use the Identity service. - -For example: - python -m examples.session /tenants -""" - -import sys - -from examples import common -from examples import connection -from openstack.identity import identity_service - - -def make_session(opts): - return connection.make_connection(opts).session - - -def run_session(opts): - argument = opts.argument - if argument is None: - raise Exception("A path argument must be specified") - sess = make_session(opts) - filtration = identity_service.IdentityService() - print("Session: %s" % sess) - print(sess.get(argument, service=filtration).text) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_session)) diff --git a/examples/transport.py b/examples/transport.py deleted file mode 100755 index 5cd96ddc..00000000 --- a/examples/transport.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -# transport.py - Example transport 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. - -""" -Transport Example - -For example: - python -m examples.transport \ - https://region-a.geo-1.identity.hpcloudsvc.com:35357/ -""" - -import sys - -from examples import common -from openstack import transport - - -def make_transport(opts): - return transport.Transport(verify=opts.verify) - - -def run_transport(opts): - """Create a transport given some options.""" - argument = opts.argument - trans = make_transport(opts) - print("transport: %s" % trans) - print(trans.get(argument).text) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_transport)) diff --git a/examples/update.py b/examples/update.py deleted file mode 100644 index 12ed6c63..00000000 --- a/examples/update.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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. - -""" -Example Update Command - -For example: - python -m examples.update openstack/network/v2/network.py \ - --data '{"name": "foo"}' -""" - -import sys - -from examples import common -from examples import session - - -def run_update(opts): - sess = session.make_session(opts) - cls = common.find_resource_cls(opts) - obj = cls.new(**common.get_data_option(opts)) - obj.update(sess) - print(str(obj)) - return - - -if __name__ == "__main__": - opts = common.setup() - sys.exit(common.main(opts, run_update))