vADC Docs

Tech Tip: Using the RESTful Control API with Python - listpoolnodes

by ricknelson on ‎02-27-2013 10:36 AM (912 Views)

The following code uses Stingray's RESTful API to list all the pools defined for a cluster and for each pool it lists the nodes defined for that pool, including draining and disabled nodes. The code is written in Python. This example builds on the previous example.  This program does a GET request for the list of pool and then while looping through the list of pools, a GET is done for each pool to retrieve the configuration parameters for that pool.

#! /usr/bin/env python

import requests

import json

import sys

print "Pools:\n"

url = ''

jsontype = {'content-type': 'application/json'}

client = requests.Session()

client.auth = ('admin', 'admin')

client.verify = False


    # Do the HTTP GET to get the lists of pools.  We are only putting this client.get within a try

    # because if there is no error connecting on this one there shouldn't be an error connnecting

    # on later client.get so that would be an unexpected exception.

    response = client.get(url)

except requests.exceptions.ConnectionError:

    print "Error: Unable to connect to " + url


data = json.loads(response.content)

if response.status_code == 200:

    if data.has_key('children'):

        pools = data['children']

        for i, pool in enumerate(pools):

            poolName = pool['name']

            # Do the HTTP GET to get the properties of a pool

            response = client.get(url + "/" + poolName)

            poolConfig = json.loads(response.content)

            if response.status_code == 200:

                # Since we are getting the properties for a pool we expect the first element to be 'properties'

                if poolConfig.has_key('properties'):

                    # The value of the key 'properties' will be a dictionary containing property sections

                    # All the properties that this program cares about are in the 'basic' section

                    # nodes is the list of all active or draining nodes in this pool

                    # draining the list of all draining nodes in this pool

                    # disabled is the list of all disabled nodes in this pool

                    nodes = poolConfig['properties']['basic']['nodes']

                    draining = poolConfig['properties']['basic']['draining']

                    disabled = poolConfig['properties']['basic']['disabled']

                    print pool['name']

                    print "    Nodes: ",

                    for n, node in enumerate(nodes):

                        print node + " ",

                    print ""

                    if len(draining) > 0:

                        print "    Draining Nodes: ",

                        for n, node in enumerate(draining):

                            print node + " ",

                        print ""

                    if len(disabled) > 0:

                        print "    Disabled Nodes: ",

                        for n, node in enumerate(disabled):

                            print node + " ",

                        print ""


                    print "Error: No properties found for pool " + poolName

                print ""


                print "Error getting pool config: URL=%s Status=%d Id=%s: %s" %(url + "/" + poolName, response.status_code, poolConfig['error_id'], poolConfig['error_text'])


        print 'Error: No chidren found'


    print "Error getting pool list: URL=%s Status=%d Id=%s: %s" %(url, response.status_code, data['error_id'], data['error_text'])

Running the example

This code was tested with Python 2.7.3 and version 1.1.0 of the requests library.

Run the Python script as follows:









Read More