Skip to content
Jiří Stránský edited this page Jul 19, 2013 · 7 revisions

Usage of python-tuskarclient

Install

Currently we do not provide any other way to install than using source directly.

Add python-tuskarclient into requirements.txt file according to http://www.pip-installer.org/en/latest/requirements.html#git

-e git://github.com/tuskar/python-tuskarclient.git#egg=python-tuskarclient

How to use tuskarclient

The client is very thin. The get/list methods return very simple wrappers around the raw dict that is created from JSON. The create/update methods take a dict that is translated into JSON and sent to the server. Update method works in a HTTP PATCH manner, that means it is not necessary to provide the full resource representation. Delete method returns what the server returns for the delete request, which in Tuskar's case is None.

# Import the module.
from tuskarclient.v1 import client

# Instantiate the client. Provide URL with hostname and port of Tuskar API.
c = client.Client("http://f18-tuskar:6385")

# The client has attributes that are "manager" instances. They
# can perform CRUD operations on resources.
c.racks
c.resource_classes

# List racks
c.racks.list()

# Get a rack by id
c.racks.get(1)

# Access properties on the rack
r = c.racks.get(1)
r.id
r.name

# Get the raw dict that matches the raw JSON data
r.to_dict

# Create a rack
c.racks.create(**{
  "subnet": "192.168.1.0/255",
  "name": "created_rack",
  "capacities": [{
    "name": "total_cpu",
    "value": "64"
  }, {
    "name": "total_memory",
    "value": "1024"
  }],
  "nodes": [{
    "id": "123"
  }, {
    "id": "345"
  }],
  "slots": 1
})

# Update a rack with id 1
c.racks.update(1, **{
  "subnet": "192.168.1.1/255",
  "name": "updated_rack",
})

# Delete a rack
c.racks.delete(1)

Flavors API

Flavors are a little different than the rest - they are a subresource of Resource Class. That means all operations require an additional resource_class_id parameter. You can use keyword arguments if you are worried about accidentally switching the order of resource_class_id and flavor_id.

# List flavors for resource class with id 3
c.flavors.list(3)

# Get a flavor with id 1 which is on resource class with id 3
c.flavors.get(3, 1)
c.flavors.get(resource_class_id=3, flavor_id=1)

# Create a new flavor on a resource class with id 3
c.flavors.create(3, **{
    "name": "x-large",
    "capacities":  [ 
                      {  "name": "cpu", 
                         "value": "4",
                         "unit": "count"  },
                      {  "name": "memory",
                         "value": "8192",
                         "unit": "MiB"    },
                      {  "name": "storage",
                         "value": "1024",
                         "unit": "GiB"    }
                    ]
})

# Update a flavor with id 1 on a resource class with id 3
c.flavors.update(3, 1, **{
  "name": "not-so-large-actually"
})

# Delete a flavor with id 1 on a resource class with id 3
c.flavors.delete(3, 1)

Attributes of the resources

To see what attributes the resources have and what data the create/update methods take, look at the API docs. The client is thin and does direct translation from JSON to Python dict (for get/list) and from Python dict to JSON (for create/update).

API docs are here: https://github.com/tuskar/tuskar/wiki/cURL-commands

Supported resources

  • Full CRUD on Racks.
  • Full CRUD on Resource Classes.
  • Full CRUD on Flavors.