
This is essentially a repeat of content from the recently updated README. We simply duplicate it rather than using complicated includes because $effort. Change-Id: If41a6b2d43d00e5bd78f6b2bd5bf33fa4a5d38d5 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
2.9 KiB
Getting started
openstacksdk aims to talk to any OpenStack cloud. To do this, it
requires a configuration file. openstacksdk favours
clouds.yaml
files, but can also use environment variables.
The clouds.yaml
file should be provided by your cloud
provider or deployment tooling. An example:
clouds:
mordred:
region_name: Dallas
auth:
username: 'mordred'
password: XXXXXXX
project_name: 'demo'
auth_url: 'https://identity.example.com'
More information on configuring openstacksdk can be found in /user/config/configuration
.
Given sufficient configuration, you can use openstacksdk to interact
with your cloud. openstacksdk consists of three layers. Most users will
make use of the proxy layer. Using the above
clouds.yaml
, consider listing servers:
import openstack
# Initialize and turn on debug logging
=True)
openstack.enable_logging(debug
# Initialize connection
= openstack.connect(cloud='mordred')
conn
# List the servers
for server in conn.compute.servers():
print(server.to_dict())
openstacksdk also contains a higher-level cloud layer based on logical operations:
import openstack
# Initialize and turn on debug logging
=True)
openstack.enable_logging(debug
# Initialize connection
= openstack.connect(cloud='mordred')
conn
# List the servers
for server in conn.list_servers():
print(server.to_dict())
The benefit of this layer is mostly seen in more complicated operations that take multiple steps and where the steps vary across providers. For example:
import openstack
# Initialize and turn on debug logging
=True)
openstack.enable_logging(debug
# Initialize connection
= openstack.connect(cloud='mordred')
conn
# Upload an image to the cloud
= conn.create_image(
image 'ubuntu-trusty', filename='ubuntu-trusty.qcow2', wait=True)
# Find a flavor with at least 512M of RAM
= conn.get_flavor_by_ram(512)
flavor
# Boot a server, wait for it to boot, and then do whatever is needed
# to get a public IP address for it.
conn.create_server('my-server', image=image, flavor=flavor, wait=True, auto_ip=True)
Finally, there is the low-level resource layer. This provides support for the basic CRUD operations supported by REST APIs and is the base building block for the other layers. You typically will not need to use this directly:
import openstack
import openstack.config.loader
import openstack.compute.v2.server
# Initialize and turn on debug logging
=True)
openstack.enable_logging(debug
# Initialize connection
= openstack.connect(cloud='mordred')
conn
# List the servers
for server in openstack.compute.v2.server.Server.list(session=conn.compute):
print(server.to_dict())