As an example let's take a look at Python Rackspace pyrax module. It was built and made available in a form of Python SDK for Rackspace cloud. Under the hood it is a wrapper and integrator of various python libraries that exists to interact with Openstack systems(cloud servers, files, networks, databases ...). As an example it uses python-novaclient library communicate and interact with Nova (Openstack Compute).
From developer point of view you can write a Python program to interact with Rackspace cloud using any of them. Both should work fine.
Working with any of these libraries you are going to face these challenges very soon:
- where is the latest API documentation
- how well are function documented
- for a function what arguments it accepts and what it returns
Problem 1
How to start interactive Python session with loaded pyrax module for testing
We are going to use bpython that has a nice feature allowing it to show the available functions and variables a Python objects implements (for these who are interested how this works you can read about introspection here and there)
- Create a file with credentials
- Create an auxiliary file for bpython
- Open the bpython interpreter
- Test how it works
root@server:~# cat > rackspace_cloud_credentials [rackspace_cloud] username = user api_key = key
# https://github.com/rtomaszewski/api-challenge/blob/master/bpython-pyrax.py
cat > bpython-pyrax.py
import os
import sys
import json
import re
import time
import datetime, time
from pprint import pprint
from pprint import pformat
import pyrax
creds_file = os.path.expanduser("~/rackspace_cloud_credentials")
pyrax.set_credential_file(creds_file, "LON")
cs = pyrax.cloudservers
help_str="""
the pyrax module has been loaded, try typing one of the commands below to see if it works.
#example 1: whow all cloud servers under you cloud account
l=cs.servers.list()
print(l)
# example 2: show list of images
pprint(cs.images.list())
"""
print(help_str)
root@server:~# bpython -i bpython-pyrax.py the pyrax module has been loaded, try typing one of the commands below to see if it works. #example 1: whow all cloud servers under you cloud account l=cs.servers.list() print(l) # example 2: show list of images pprint(cs.images.list()) >>>
Problem 2
How to start interactive Python session with loaded python-novaclient for testing
- Create bash file with variables
- Create an auxiliary file for bpython
- Open the bpython interpreter
- Test how it works
cat > nova.vars.sh export OS_AUTH_SYSTEM="rackspace_uk" export OS_AUTH_URL="https://lon.identity.api.rackspacecloud.com/v2.0/" export OS_NO_CACHE="1" export OS_PASSWORD="pass" export OS_REGION_NAME="LON" export OS_TENANT_NAME="user" export OS_USERNAME="user" export NOVA_RAX_AUTH=1 export OS_PROJECT_ID="user"
# https://github.com/rtomaszewski/api-challenge/blob/master/bpython-novaclient.py
cat > bpython-novaclient.py
import os
import sys
import json
import re
import time
import datetime, time
from pprint import pprint
from pprint import pformat
from novaclient.v1_1 import client
os.environ["OS_USERNAME"]
os.environ["OS_PASSWORD"]
os.environ["OS_NO_CACHE"]
os.environ["OS_TENANT_NAME"]
os.environ["OS_AUTH_URL"]
os.environ["OS_REGION_NAME"]
os.environ["OS_AUTH_SYSTEM"]
os.environ["NOVA_RAX_AUTH"]
os.environ["OS_PROJECT_ID"]
from novaclient import auth_plugin as _cs_auth_plugin
_cs_auth_plugin.discover_auth_systems()
auth_plugin = _cs_auth_plugin.load_plugin("rackspace_uk")
cs = client.Client(os.environ["OS_USERNAME"], os.environ["OS_PASSWORD"], os.environ["OS_TENANT_NAME"], auth_url=os.environ["OS_AUTH_URL"], auth_system="rackspace", region_name="LON", service_type="compute", auth_plugin=auth_plugin)
help_str="""
the novaclient module has been loaded and INITIATED, try typing one of the commands below to see if it works.
#example 1: whow all cloud servers under you cloud account
l=cs.servers.list()
print(l)
# example 2: show list of images
pprint(cs.images.list())
"""
print(help_str)
root@manage2:~# bpython -i bpython-novaclient.py the novaclient module has been loaded and INITIATED, try typing one of the commands below to see if it works. #example 1: whow all cloud servers under you cloud account l=cs.servers.list() print(l) # example 2: show list of images pprint(cs.images.list()) >>>
- http://bpython-interpreter.org/screenshots/
- http://stackoverflow.com/questions/546337/how-do-i-perform-introspection-on-an-object-in-python-2-x
- http://docs.rackspace.com/servers/api/v2/cs-gettingstarted/content/section_gs_install_nova.html
- https://github.com/openstack/python-novaclient
- https://github.com/rackspace/pyrax/blob/master/samples/cloudservers/create_server.py


No comments:
Post a Comment