diff --git a/doc/source/users/guides/image.rst b/doc/source/users/guides/image.rst index e9b3b4066..322628933 100644 --- a/doc/source/users/guides/image.rst +++ b/doc/source/users/guides/image.rst @@ -5,4 +5,43 @@ 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 +The primary resource of the Image service is the image. + +List Images +----------- + +An **image** is a collection of files for a specific operating system +that you use to create or rebuild a server. OpenStack provides +`pre-built images `_. +You can also create custom images, or snapshots, from servers that you have +launched. Images come in different formats and are sometimes called virtual +machine images. + +.. literalinclude:: ../examples/image/list.py + :pyobject: list_images + +Full example: `image resource list`_ + +Create Image +------------ + +Create an image by uploading its data and setting its attributes. + +.. literalinclude:: ../examples/image/create.py + :pyobject: upload_image + +Full example: `image resource create`_ + +Delete Image +------------ + +Delete an image. + +.. literalinclude:: ../examples/image/delete.py + :pyobject: delete_image + +Full example: `image resource delete`_ + +.. _image resource create: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/image/create.py +.. _image resource delete: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/image/delete.py +.. _image resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/image/list.py diff --git a/examples/connect.py b/examples/connect.py index ab6a4a9b5..45fee854d 100644 --- a/examples/connect.py +++ b/examples/connect.py @@ -63,6 +63,8 @@ PRIVATE_KEYPAIR_FILE = _get_resource_value( 'private_keypair_file', '{ssh_dir}/id_rsa.{key}'.format( ssh_dir=SSH_DIR, key=KEYPAIR_NAME)) +EXAMPLE_IMAGE_NAME = 'openstacksdk-example-public-image' + def create_connection_from_config(): return connection.from_config(cloud_config=cloud, options=opts) diff --git a/examples/image/__init__.py b/examples/image/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/image/create.py b/examples/image/create.py new file mode 100644 index 000000000..45b4bc7d0 --- /dev/null +++ b/examples/image/create.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. + +from examples.connect import EXAMPLE_IMAGE_NAME + +""" +Create resources with the Image service. + +For a full guide see +http://developer.openstack.org/sdks/python/openstacksdk/users/guides/image.html +""" + + +def upload_image(conn): + print("Upload Image:") + + # Load fake image data for the example. + data = 'This is fake image data.' + + # Build the image attributes and upload the image. + image_attrs = { + 'name': EXAMPLE_IMAGE_NAME, + 'data': data, + 'disk_format': 'raw', + 'container_format': 'bare', + 'visibility': 'public', + } + conn.image.upload_image(**image_attrs) diff --git a/examples/image/delete.py b/examples/image/delete.py new file mode 100644 index 000000000..dc6560366 --- /dev/null +++ b/examples/image/delete.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. + +from examples.connect import EXAMPLE_IMAGE_NAME + +""" +Delete resources with the Image service. + +For a full guide see +http://developer.openstack.org/sdks/python/openstacksdk/users/guides/image.html +""" + + +def delete_image(conn): + print("Delete Image:") + + example_image = conn.image.find_image(EXAMPLE_IMAGE_NAME) + + conn.image.delete_image(example_image, ignore_missing=False) diff --git a/examples/image/list.py b/examples/image/list.py new file mode 100644 index 000000000..decb6e5d9 --- /dev/null +++ b/examples/image/list.py @@ -0,0 +1,25 @@ +# 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 Image service. + +For a full guide see +http://developer.openstack.org/sdks/python/openstacksdk/users/guides/image.html +""" + + +def list_images(conn): + print("List Images:") + + for image in conn.image.images(): + print(image) diff --git a/openstack/tests/examples/test_image.py b/openstack/tests/examples/test_image.py new file mode 100644 index 000000000..db027e9b4 --- /dev/null +++ b/openstack/tests/examples/test_image.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. + +import unittest + +from examples import connect +from examples.image import create as image_create +from examples.image import delete as image_delete +from examples.image import list as image_list + + +class TestImage(unittest.TestCase): + """Test the image 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_image(self): + image_list.list_images(self.conn) + + image_create.upload_image(self.conn) + + image_delete.delete_image(self.conn)