cancel
Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to list all the pools defined on a cluster. The code is written in Python. This example has more extensive comments then the following examples and most of these are applicable to all the examples. This program does a single GET request for the list of pools and then loops through that list, each element of which is a dictionary, and then outputs the pool name.

 

listpools.py

 

#! /usr/bin/env python
import requests # The REST Client
import json # For serializing and deserializing JSON
import sys

print "Pools:\n"

url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools';

# Setup the session
client = requests.Session()

# Set the Userid and Password.  These need to match a UserId and Password for a Stingray user

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

# Don't require that the Stingray's certiciate be from a certiticate authority because
# Stingray certificates are self-signed.

client.verify = False

try:
    # Do the HTTP GET to get the lists of pools
    response = client.get(url)
except requests.exceptions.ConnectionError:
    # We weren't able to connect to the Stingray.  The most likely reasons for this are that the
    # hostname of the Stingray instance is incorrect, or this client doesn't have network access
    # to the Stingray instance or to port 9070 on the Stingray instance, or the RESTful API could
    # be disabled on the Stingray instance, or the Stingray instance isn't using the default port.

    print "Error: Unable to connect to " + url
    sys.exit(1)

data = json.loads(response.content) # Deserialize the JSON response into a dictionary

if response.status_code == 200: # We always expect a 200
    # Since we are getting a list of pools we expect the first element to be 'children'
    if data.has_key('children'):
        # The value for the key 'children' will be a list containing a dictionary for each pool
        # with key, 'name', set to the name of the pool and key, 'href', set to the URL of the pool
        pools = data['children']
        for i, pool in enumerate(pools):
            print pool['name'] # This is the name of the pool
    else:
        print 'Error: No children found'
else:
    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:

 

$ listpools.py

Pools:

 

Pool1

Pool2

 

Read More

 

 

Version history
Revision #:
2 of 2
Last update:
‎01-06-2021 04:39:PM
Updated by:
 
Labels (1)
Contributors