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.
addpool.py
#! /usr/bin/env python import requests import json import sys poolName = 'pytest' pool = { 'properties' : { 'basic' : { 'nodes' : [ '192.168.168.135:80'] } } } url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools/' + poolName jsontype = {'content-type': 'application/json'} client = requests.Session() client.auth = ('admin', 'admin') client.verify = False try: #First see if the pool already exists response = client.get(url) except requests.exceptions.ConnectionError: print "Error: Unable to connect to " + url sys.exit(1) 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) else: 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']) else: if response.status_code == 200: print "Pool %s already exists" %(poolName) else: 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:
$ addpool.py
Pool pytest added
Notes
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