The XMS Python SDK Developer Guide

Introduction

This guide aims to give you the basis to develop your own Python applications based on our SDK.

The easiest possible usage of Python SDK:

# Importing Python SDK
import sdk
        # Creating a client for OpenAM authentication
iam = sdk.Iam("http://openam.xlcloud-dev.com:8082/openam","xlcloud-console","password")
        # Obtaining a token (authenticating as a 'xlcadmin' user)
token = iam.login("xlcadmin","admin123")
        # Creating a client for XMS
client = sdk.Client("http://localhost:8080/xlcloud-xms",token)
        # Retrieving JSON object represeting list of all accounts
accounts = client.accounts.listAccounts()
        # Processing the result - listing account names
print map(lambda x: x["name"],accounts["account"])

This piece of code prints list of all accounts.

Configuration

We'll assume that you have pulled our repository, and are now in possession of the SDK folder located in /xclcloud-ci/xms/cli/sdk.

You must first create a configuration file in which you'll put the following text:

Properties file sdk.conf

[XmsCli]
XMS_URL:http://<your-ip-address-or-domain-name-for-xlcloud>:8090/xlcloud-xms
OPENAM_URL:http://<your-ip-address-or-domain-name-for-openam>:8082/openam
OAUTH_CLIENT_USER:<username>
OAUTH_CLIENT_PASS:<password>

Install the XMS SDK

The package is available on PyPi: https://pypi.python.org/pypi/xms_sdk/

You can either download the .tar.gz package, extract it, and run:

Install the xms_sdk package manually

python setup.py install 

Or you can simply run:

Install the xms_sdk package with pip

pip install xms_sdk

You will also need two other things: httplib2, and PyGTK (this one is to install only if you plan on developing a GUI).

Developing with the XMS SDK

The application we'll be developing in this guide will use GTK to render a simple GUI interface. We are going to list existing accounts.

Since this guide will only focus on the minimum required, we'll assume that you have somehow acquired a valid token (for example, with a Text Entry).

Import the necessary packages

In the code below, the section "SDK identities and tools packages" must be included after the section "SDK packages relative to what you want to do".

The top of the file

1   #!/usr/bin/env python
2   # -*- coding: utf-8 -*-
3
4   # GTK packages
5   import pygtk
6   pygtk.require("2.0")
7   import gtk
8
9   # System packages
10  import os
11  import json
12  import ConfigParser
13
14  # SDK packages relative to what you want to do
15  import xms_sdk.accounts
16
17  # SDK identities and tools packages
18  import xms_sdk.iam
19  from xms_sdk import sdk 

Get the token and the configuration settings

In the code below, don't forget to put the name of your configuration file (for us, it's sdk.conf).

Function that will load the configuration properties

def load_configuration(args):
        configFile = os.path.join(os.path.dirname(file), 'sdk.conf')
        try:
            config = ConfigParser.ConfigParser()
            config.read(configFile)
            return dict(config.items("XmsCli"))
        except:
            raise Exception("Error during reading configuration from file: "+configFile)

Here we'll set the token in the code, but feel free to get it by other means.

Extract and set configuration settings

# Get the token
token = '6d368295-451b-4748-8c72-a514d596edef'

# Extract the configuration settings to connect to XMS
config = load_configuration(0)
myIam = sdk.Iam(config["openam_url"], config["oauth_client_user"], config["oauth_client_pass"])
myClient = sdk.Client(config["xms_url"], token)

Get the list of accounts

# Get the list of accounts
myAccounts = sdk.Accounts(config["xms_url"], token)
json_string_of_accounts = myAccounts.listAccounts()

Display the list of accounts

Simple display in the command line

If you just want to display the list of accounts in the console, use this line and the work is done:

# Pretty-print the JSON list of accounts
print json.dumps(json_string_of_accounts, sort_keys=True, indent=2);

If you want to display it in a table, continue reading.

Fancy display in a GTK window

We iterate a first time on this JSON to get the number of accounts.

Get the number of accounts

# Count the number of accounts
nb_accounts = 0
for key, value in json_string_of_accounts.iteritems():
    if key == "account":
        for i in value:
            nb_accounts += 1

Then, we'll set up a GTK table:

Setup the GTK table that will display the list of accounts

# Create the table of accounts
label_id = gtk.Label('<span size="12000" weight="bold">Account ID</span>')
label_id.set_use_markup(True)
label_name = gtk.Label('<span size="12000" weight="bold">Account Name</span>')
label_name.set_use_markup(True)
table_of_users = gtk.Table(nb_accounts+1,2,True)
table_of_users.attach(label_id,0,1,0,1)
table_of_users.attach(label_name,1,2,0,1)

We iterate one more time on the JSON to display the selected accounts properties (ID and name) in the GTK table we just created:

Get the accounts properties and display them in a table

# Display the accounts in the table
j = 1
for key, value in json_string_of_accounts.iteritems():
    if key == "account":
        for i in value:
            table_of_users.attach(gtk.Label(i["id"]),0,1,j,j+1)
            table_of_users.attach(gtk.Label(i["name"]),1,2,j,j+1)
            j+=1

Finally, we use the following code to display the table in a window:

Create a window to display the accounts table

# Create a window, set its title and width
myWindow = gtk.Window()
myWindow.set_title("Demo of the XMS Python SDK")
myWindow.set_geometry_hints(min_width=400, min_height=200)

# Create a handler for when the window is closed
myWindow.connect("destroy", on_mainWindow_destroy)

# Create a container and add the table in it
vBox = gtk.VBox()
vBox.pack_start(table_of_users, expand=False, fill=False, padding=10)

# Add the container to the main window
myWindow.add(self.vBox)

# Make all widgets visible
myWindow.show_all()

# This function handles the closing of the window
def on_mainWindow_destroy(self, widget):
    gtk.main_quit()

And you should get the following output:

Demo_window.png


This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 5.4.6 - Documentation - Legal Notice

Site maintained by