Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to add a pool.   The code is written in Python. This program creates a new pool, "pytest", first doing a GET to make sure the pool doesn't already exist, and if the pool doesn't exist, the pool is created by doing a PUT with just the minimum data need to create a pool.  In this case the program creates a properties dictionary with just one node.  All other values will get default values when Stingray creates the pool.


#! /usr/bin/env python
import requests
import json
import sys

poolName = 'pytest'
pool = {
   'properties' : {
      'basic' : { 'nodes' : [ ''] }

url = '' + poolName

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

client = requests.Session()
client.auth = ('admin', 'admin')
client.verify = False

    #First see if the pool already exists
    response = client.get(url)
except requests.exceptions.ConnectionError:
    print "Error: Unable to connect to " + url

if (response.status_code == 404):
    response = client.put(url, data = json.dumps(pool), headers = jsontype)

    if response.status_code == 201: # When creating a new resource we expect to get a 201
        print 'Pool %s added' %(poolName)
        data = json.loads(response.content)
        print "Error adding pool %s: URL=%s Status=%d Id=%s: %s" %(poolName, url, response.status_code, data['error_id'], data['error_text'])

    if response.status_code == 200:
        print "Pool %s already exists" %(poolName)

        print "Error: Status=%d URL=%s" %(response.status_code, url) 

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:



Pool pytest added




The only difference between doing a PUT to change a resource and a PUT to add a resource is the HTTP status code returned.  When changing a resource 200 is the expected status code and when adding a resource, 201 is the expected status code.


Read More




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