diff --git a/doc/source/code/connection.py b/doc/source/code/connection.py deleted file mode 100644 index f57c1a945..000000000 --- a/doc/source/code/connection.py +++ /dev/null @@ -1,6 +0,0 @@ -from openstack import connection -conn = connection.Connection(auth_url="http://openstack:5000/v3", - project_name="big_project", - username="SDK_user", - password="Super5ecretPassw0rd") - diff --git a/doc/source/contributors/clouds.yaml b/doc/source/contributors/clouds.yaml index 6f7f31951..86897d75a 100644 --- a/doc/source/contributors/clouds.yaml +++ b/doc/source/contributors/clouds.yaml @@ -1,21 +1,4 @@ clouds: - fone: - region_name: boneville - default_domain: 'Valley' - identity_api_version: 3 - auth: - auth_url: http://172.20.1.112:5000/v3/ - project_name: cowrace - username: fonebone - password: thorn - phoney: - region_name: barrelhaven - identity_api_version: 2 - auth: - auth_url: http://172.20.1.112:5000/v2.0/ - project_name: dragonslayer - username: phoneybone - password: gold test_cloud: region_name: RegionOne auth: @@ -23,3 +6,17 @@ clouds: username: demo password: secrete project_name: demo + hp: + cloud: hp + auth: + username: joe + password: joes-password + project_name: joe-tenant1 + region_name: region-b.geo-1 + rackspace: + cloud: rackspace + auth: + username: joe + password: joes-password + project_name: 123123 + region_name: IAD diff --git a/doc/source/users/examples b/doc/source/users/examples new file mode 120000 index 000000000..d4cb9b9c8 --- /dev/null +++ b/doc/source/users/examples @@ -0,0 +1 @@ +../../../examples/ \ No newline at end of file diff --git a/doc/source/users/examples.rst b/doc/source/users/examples.rst deleted file mode 100644 index f248f9ee0..000000000 --- a/doc/source/users/examples.rst +++ /dev/null @@ -1,50 +0,0 @@ -OpenStack SDK Examples -====================== - -An effort has been made to provide some useful examples to try out the -SDK. These examples reside in the examples directory of the project. -The examples use an authentication configuration that is very similar -to the configuration used for most OpenStack clients. - -Configuration -------------- - -Most of the examples use a common argument parser to collect -authentication information to run requests. The authentication may come -from the OS prefixed environment variables similar to the ones used in many -client libraries or you may use -`os-client-config `_. - -To use the OS prefixed environment variables, set up something similar to -the following.:: - - export OS_REGION_NAME=Manhattan - export OS_PROJECT_NAME=beasties - export OS_IDENTITY_API_VERSION=2.0 - export OS_PASSWORD=horovitz - export OS_AUTH_URL=https://127.0.0.1:5000/v2.0/ - export OS_USERNAME=adrock - -To use os-client-config, create a clouds.yaml file, which typically resides in -~/.config/openstack/clouds.yaml to store authentication and preference -information. If you are using os-client-config, you can use the OS_CLOUD -environment variable or use the --os-cloud command line option to specify -the cloud you want to use.:: - - export OS_CLOUD=miked - -Running -------- - -Once you have a configuration, most examples can be run by calling them -directly. For example, the list example can be run by just passing it the -name of the resource you want to list:: - - python examples/list.py openstack/compute/v2/flavor.py - -The example code uses some logic to parse the file name and generate a -resource class name. That may not work if the resource class name does -not match the file name. If that is the case, you will need to specify -the resource module name:: - - python examples/list.py openstack.network.v2.floating_ip.FloatingIP diff --git a/doc/source/users/guides/block_store.rst b/doc/source/users/guides/block_store.rst index e35a3be89..bb67a4eed 100644 --- a/doc/source/users/guides/block_store.rst +++ b/doc/source/users/guides/block_store.rst @@ -1,9 +1,9 @@ Using OpenStack Block Store =========================== -Before working with ``block_store``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Block Store service, you'll need to create a +connection to your OpenStack cloud by following the :doc:`connect` user +guide. This will provide you with the ``conn`` variable used in the examples +below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/cdn.rst b/doc/source/users/guides/cdn.rst index c195ce245..55fead3a4 100644 --- a/doc/source/users/guides/cdn.rst +++ b/doc/source/users/guides/cdn.rst @@ -1,9 +1,9 @@ Using OpenStack CDN =================== -Before working with ``cdn``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Content Distribution Network (CDN) service, you'll +need to create a connection to your OpenStack cloud by following the +:doc:`connect` user guide. This will provide you with the ``conn`` variable +used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/compute.rst b/doc/source/users/guides/compute.rst index 91a62c39b..94fd53351 100644 --- a/doc/source/users/guides/compute.rst +++ b/doc/source/users/guides/compute.rst @@ -1,9 +1,72 @@ Using OpenStack Compute ======================= -Before working with ``compute``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. +Before working with the Compute service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. -.. literalinclude:: /code/connection.py +.. contents:: Table of Contents + :local: -.. TODO(thowe): Implement this guide +The primary resource of the Compute service is the server. + +List Servers +------------ + +A **server** is a virtual machine that provides access to a compute instance +being run by your cloud provider. + +.. literalinclude:: ../examples/compute/list.py + :pyobject: list_servers + +Full example: `compute resource list`_ + +List Images +----------- + +An **image** is the operating system you want to use for your server. + +.. literalinclude:: ../examples/compute/list.py + :pyobject: list_images + +Full example: `compute resource list`_ + +List Flavors +------------ + +A **flavor** is the resource configuration for a server. Each flavor is a +unique combination of disk, memory, vCPUs, and network bandwidth. + +.. literalinclude:: ../examples/compute/list.py + :pyobject: list_flavors + +Full example: `compute resource list`_ + +List Networks +------------- + +A **network** provides connectivity to servers. + +.. literalinclude:: ../examples/network/list.py + :pyobject: list_networks + +Full example: `network resource list`_ + +Create Server +------------- + +At minimum, a server requires a name, an image, a flavor, and a network on +creation. Once you've discovered these attributes by listing them as above, +you can create a server. + +Servers take time to boot so we call ``wait_for_server`` to wait +for it to become active. + +.. literalinclude:: ../examples/compute/create.py + :pyobject: create_server + +Full example: `compute resource create`_ + +.. _compute resource list: http://git.openstack.org/cgit/stackforge/python-openstacksdk/tree/examples/compute/list.py +.. _network resource list: http://git.openstack.org/cgit/stackforge/python-openstacksdk/tree/examples/network/list.py +.. _compute resource create: http://git.openstack.org/cgit/stackforge/python-openstacksdk/tree/examples/compute/create.py diff --git a/doc/source/users/guides/connect.rst b/doc/source/users/guides/connect.rst new file mode 100644 index 000000000..afa4df973 --- /dev/null +++ b/doc/source/users/guides/connect.rst @@ -0,0 +1,45 @@ +Connect +======= + +In order to work with an OpenStack cloud you first need to create a +:class:`~openstack.connection.Connection` to it using your credentials. A +:class:`~openstack.connection.Connection` can be +created in 3 ways, using the class itself, a file, or environment variables. +If this is your first time using the SDK, we recommend simply using the +class itself as illustrated below. + +.. note:: To get your credentials + `Download the OpenStack RC file `_. + +Create Connection +----------------- + +To create a connection you need a :class:`~openstack.profile.Profile` and a +:class:`~openstack.connection.Connection`. + +.. literalinclude:: ../examples/connect.py + :language: python + :lines: 32-44 + +The :class:`~openstack.profile.Profile` sets your preferences for each +service. You will pass it the region of the OpenStack cloud that this +connection will use. + +The :class:`~openstack.connection.Connection` is a context for a connection +to an OpenStack cloud. You will primarily use it to set the +:class:`~openstack.profile.Profile` and authentication information with the +``auth_args`` parameter. You can also set the ``user_agent`` to something that +describes your application (e.g. ``my-web-app/1.3.4``). + +Full example at `connect.py `_ + +.. note:: To enable logging, see the :doc:`logging` user guide. + +Next +---- +Now that you can create a connection, continue with the :ref:`user_guides` +to work with an OpenStack service. + +As an alternative to creating a :class:`~openstack.connection.Connection` +using the class itself, you can connect using a file or environment +variables. See the :doc:`connect_from_config` user guide. diff --git a/doc/source/users/guides/connect_from_config.rst b/doc/source/users/guides/connect_from_config.rst new file mode 100644 index 000000000..d7efb314f --- /dev/null +++ b/doc/source/users/guides/connect_from_config.rst @@ -0,0 +1,71 @@ +Connect From Config +=================== + +In order to work with an OpenStack cloud you first need to create a +:class:`~openstack.connection.Connection` to it using your credentials. A +:class:`~openstack.connection.Connection` can be +created in 3 ways, using the class itself (see :doc:`connect`), a file, or +environment variables as illustrated below. The SDK uses +`os-client-config `_ +to handle the configuration. + +.. note:: To get your credentials + `Download the OpenStack RC file `_. + +Create Connection From A File +----------------------------- + +Default Location +**************** + +To create a connection from a file you need a YAML file to contain the +configuration and the :py:func:`~openstack.connection.from_config` +function. + +.. literalinclude:: ../../contributors/clouds.yaml + :language: yaml + +To look for a configuration file called ``clouds.yaml`` in the default +locations: + +* Current Directory +* ~/.config/openstack +* /etc/openstack + +call :py:func:`~openstack.connection.from_config` with an object that has +the name of the cloud configuration to use. + +.. literalinclude:: ../examples/connect.py + :language: python + :lines: 23-30 + +.. note:: To enable logging, set ``debug=True`` in the ``Opts`` object. + +User Defined Location +********************* + +To look for a configuration file in a user defined location set the +environment variable ``OS_CLIENT_CONFIG_FILE`` to the +absolute path of a file.:: + + export OS_CLIENT_CONFIG_FILE=/path/to/my/config/my-clouds.yaml + +and call :py:func:`~openstack.connection.from_config` with an object that has +the name of the cloud configuration to use. + +.. literalinclude:: ../examples/connect.py + :language: python + :lines: 23-30 + +.. note:: To enable logging, set ``debug=True`` in the ``Opts`` object. + +.. Create Connection From Environment Variables + -------------------------------------------- + + TODO(etoews): Document when https://bugs.launchpad.net/os-client-config/+bug/1489617 + is fixed. + +Next +---- +Now that you can create a connection, continue with the :ref:`user_guides` +for an OpenStack service. diff --git a/doc/source/users/guides/database.rst b/doc/source/users/guides/database.rst index cb325caef..44256b34a 100644 --- a/doc/source/users/guides/database.rst +++ b/doc/source/users/guides/database.rst @@ -1,9 +1,8 @@ Using OpenStack Database ======================== -Before working with ``database``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Database service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/identity.rst b/doc/source/users/guides/identity.rst index 541604724..47bda51ed 100644 --- a/doc/source/users/guides/identity.rst +++ b/doc/source/users/guides/identity.rst @@ -1,9 +1,8 @@ Using OpenStack Identity ======================== -Before working with ``identity``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Identity service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/image.rst b/doc/source/users/guides/image.rst index 4ffebc30c..e9b3b4066 100644 --- a/doc/source/users/guides/image.rst +++ b/doc/source/users/guides/image.rst @@ -1,9 +1,8 @@ Using OpenStack Image ===================== -Before working with ``image``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Image service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/key_management.rst b/doc/source/users/guides/key_management.rst index cd371e8cc..de9ea1912 100644 --- a/doc/source/users/guides/key_management.rst +++ b/doc/source/users/guides/key_management.rst @@ -1,9 +1,9 @@ -Using OpenStack KeyManagement -============================= +Using OpenStack Key Management +============================== -Before working with ``key_management``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Key Management service, you'll need to create a +connection to your OpenStack cloud by following the :doc:`connect` user +guide. This will provide you with the ``conn`` variable used in the examples +below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/metric.rst b/doc/source/users/guides/metric.rst index 59032af58..e977d7149 100644 --- a/doc/source/users/guides/metric.rst +++ b/doc/source/users/guides/metric.rst @@ -1,9 +1,8 @@ Using OpenStack Metric ====================== -Before working with ``metric``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Metric service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/network.rst b/doc/source/users/guides/network.rst index 497bb7f76..0ec9e2c2a 100644 --- a/doc/source/users/guides/network.rst +++ b/doc/source/users/guides/network.rst @@ -1,9 +1,8 @@ Using OpenStack Network ======================= -Before working with ``network``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Network service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/object_store.rst b/doc/source/users/guides/object_store.rst index 17b455baf..e777a525e 100644 --- a/doc/source/users/guides/object_store.rst +++ b/doc/source/users/guides/object_store.rst @@ -1,12 +1,15 @@ Using OpenStack Object Store ============================ -The Object Store API operates on two things: containers and objects. +Before working with the Object Store service, you'll need to create a +connection to your OpenStack cloud by following the :doc:`connect` user +guide. This will provide you with the ``conn`` variable used in the examples +below. -Before working with ``object_store``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. +.. contents:: Table of Contents + :local: -.. literalinclude:: /code/connection.py +The primary resources of the Object Store service are containers and objects. Working with Containers ----------------------- diff --git a/doc/source/users/guides/orchestration.rst b/doc/source/users/guides/orchestration.rst index 379139cae..64adc4fdd 100644 --- a/doc/source/users/guides/orchestration.rst +++ b/doc/source/users/guides/orchestration.rst @@ -1,9 +1,9 @@ Using OpenStack Orchestration ============================= -Before working with ``orchestration``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Orchestration service, you'll need to create a +connection to your OpenStack cloud by following the :doc:`connect` user +guide. This will provide you with the ``conn`` variable used in the examples +below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/telemetry.rst b/doc/source/users/guides/telemetry.rst index 3429b9d41..8aff342af 100644 --- a/doc/source/users/guides/telemetry.rst +++ b/doc/source/users/guides/telemetry.rst @@ -1,9 +1,8 @@ Using OpenStack Telemetry ========================= -Before working with ``telemetry``, you'll need to obtain a -:class:`~openstack.connection.Connection` object like so. - -.. literalinclude:: /code/connection.py +Before working with the Telemetry service, you'll need to create a connection +to your OpenStack cloud by following the :doc:`connect` user guide. This will +provide you with the ``conn`` variable used in the examples below. .. TODO(thowe): Implement this guide diff --git a/doc/source/users/guides/usage.rst b/doc/source/users/guides/usage.rst deleted file mode 100644 index e79d48a71..000000000 --- a/doc/source/users/guides/usage.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. TODO(briancurtin): turn this into a full guide on the Connection class -.. TODO(briancurtin): cover user_agent setting - -===== -Usage -===== - -To use openstacksdk in a project:: - - from openstack import connection - from openstack import profile - - # First, specify your profile - prof = profile.Profile() - prof.set_region('network', 'zion') - - # Second, create a connection - conn = connection.Connection(profile=prof, - auth_url='http://172.20.1.108:5000/v3', - project_name='hacker', - username='neo', - password='bluepill') - - # Finally, access your desired services - network = conn.network.find_network("matrix") - if network is None: - network = conn.network.create_network(name="matrix") diff --git a/doc/source/users/index.rst b/doc/source/users/index.rst index 9f3d42118..05ba7a73a 100644 --- a/doc/source/users/index.rst +++ b/doc/source/users/index.rst @@ -13,6 +13,8 @@ The OpenStack SDK is available on $ pip install openstacksdk +.. _user_guides: + User Guides ----------- @@ -23,7 +25,8 @@ approach, this is where you'll want to begin. .. toctree:: :maxdepth: 1 - Connecting to an OpenStack Cloud + Connect to an OpenStack Cloud + Connect to an OpenStack Cloud Using a Config File Logging Block Store CDN @@ -38,13 +41,6 @@ approach, this is where you'll want to begin. Orchestration Telemetry -It may also be useful to try out some working code examples: - -.. toctree:: - :maxdepth: 1 - - examples - API Documentation ----------------- diff --git a/examples/compute/__init__.py b/examples/compute/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/compute/create.py b/examples/compute/create.py new file mode 100644 index 000000000..6460a235b --- /dev/null +++ b/examples/compute/create.py @@ -0,0 +1,28 @@ +# 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. + +""" +Create resources with the Compute service. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + + +def create_server(conn, name, image, flavor, network): + print("Create Server:") + + server = conn.compute.create_server(name=name, image=image, + flavor=flavor, + networks=[{"uuid": network.id}]) + conn.compute.wait_for_server(server) + + print(server) diff --git a/examples/compute/find.py b/examples/compute/find.py new file mode 100644 index 000000000..faf39a3bc --- /dev/null +++ b/examples/compute/find.py @@ -0,0 +1,37 @@ +# 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. + +""" +Find a resource from the Compute service. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + + +def find_image(conn): + print("Find Image:") + + image = conn.compute.find_image("fedora-20.x86_64") + + print(image) + + return image + + +def find_flavor(conn): + print("Find Flavor:") + + flavor = conn.compute.find_flavor("m1.small") + + print(flavor) + + return flavor diff --git a/examples/compute/list.py b/examples/compute/list.py new file mode 100644 index 000000000..28a3c048d --- /dev/null +++ b/examples/compute/list.py @@ -0,0 +1,38 @@ +# 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. + +""" +List resources from the Compute service. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + + +def list_servers(conn): + print("List Servers:") + + for server in conn.compute.servers(): + print(server) + + +def list_images(conn): + print("List Images:") + + for image in conn.compute.images(): + print(image) + + +def list_flavors(conn): + print("List Flavors:") + + for flavor in conn.compute.flavors(): + print(flavor) diff --git a/examples/connect.py b/examples/connect.py new file mode 100644 index 000000000..0bf398f74 --- /dev/null +++ b/examples/connect.py @@ -0,0 +1,44 @@ +# 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. + +""" +Connect to an OpenStack cloud. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + +from openstack import connection +from openstack import profile + + +class Opts(object): + def __init__(self, cloud='hp', debug=False): + self.cloud = cloud + self.debug = debug + + +def create_connection_from_config(): + return connection.from_config(Opts(cloud='test_cloud')) + + +def create_connection(auth_url, region, project_name, username, password): + prof = profile.Profile() + prof.set_region(profile.Profile.ALL, region) + + return connection.Connection( + profile=prof, + user_agent='examples', + auth_url=auth_url, + project_name=project_name, + username=username, + password=password + ) diff --git a/examples/network/find.py b/examples/network/find.py new file mode 100644 index 000000000..21844f326 --- /dev/null +++ b/examples/network/find.py @@ -0,0 +1,27 @@ +# 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. + +""" +Find a resource from the Network service. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + + +def find_network(conn): + print("Find Network:") + + network = conn.network.find_network("public") + + print(network) + + return network diff --git a/examples/network/list.py b/examples/network/list.py new file mode 100644 index 000000000..d32e6df9e --- /dev/null +++ b/examples/network/list.py @@ -0,0 +1,24 @@ +# 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. + +""" +List resources from the Network service. + +For a full guide see TODO(etoews):link to docs on developer.openstack.org +""" + + +def list_networks(conn): + print("List Networks:") + + for network in conn.network.networks(): + print(network) diff --git a/openstack/tests/examples/__init__.py b/openstack/tests/examples/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openstack/tests/examples/test_compute.py b/openstack/tests/examples/test_compute.py new file mode 100644 index 000000000..4dd53fd93 --- /dev/null +++ b/openstack/tests/examples/test_compute.py @@ -0,0 +1,44 @@ +# 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 unittest + +from examples.compute import create +from examples.compute import find +from examples.compute import list +from examples import connect +from examples.network import find as network_find +from examples.network import list as network_list + + +class TestCompute(unittest.TestCase): + """Test the compute examples + + The purpose of these tests is to ensure the examples run without erring + out. + """ + + @classmethod + def setUpClass(cls): + cls.conn = connect.create_connection_from_config() + + def test_compute(self): + list.list_servers(self.conn) + list.list_images(self.conn) + list.list_flavors(self.conn) + network_list.list_networks(self.conn) + + image = find.find_image(self.conn) + flavor = find.find_flavor(self.conn) + network = network_find.find_network(self.conn) + + create.create_server(self.conn, 'example', image, flavor, network) diff --git a/openstack/transport.py b/openstack/transport.py index c9045617f..6d6aa42eb 100644 --- a/openstack/transport.py +++ b/openstack/transport.py @@ -129,7 +129,7 @@ from openstack import exceptions #: Default value for the HTTP User-Agent header. The default includes the #: version information of the SDK as well as ``requests``, Python, #: and the operating system. -USER_AGENT = "python-openstacksdk/%s %s" % ( +USER_AGENT = "openstacksdk/%s %s" % ( openstack.__version__, requests.utils.default_user_agent()) _logger = logging.getLogger(__name__) diff --git a/tox.ini b/tox.ini index a8665181a..775570797 100644 --- a/tox.ini +++ b/tox.ini @@ -11,6 +11,10 @@ setenv = deps = -r{toxinidir}/test-requirements.txt commands = ostestr {posargs} +[testenv:examples] +setenv = OS_TEST_PATH=./openstack/tests/examples +passenv = OS_* + [functionalbase] setenv = OS_TEST_PATH=./openstack/tests/functional passenv = OS_*