The following code uses Stingray's RESTful API to enable or disabled a specific Virtual Server. The code is written in Python. This program checks to see if the Virtual Server "test vs" is enabled and if it is, it disables it and if it is disabled, it enables it. A GET is done to retrieve the configuration data for the Virtual Server and the "enabled" value in the "basic" properties section is checked. This is a boolean value, so if it is true it is set to false and if it is false it is set to true. The changed data is then sent to the server using a PUT.
startstopvs.py
#! /usr/bin/env python import requests import json import sys vs = "test vs" url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/vservers/' + vs; jsontype = {'content-type': 'application/json'} client = requests.Session() client.auth = ('admin', 'admin') client.verify = False # Get the config data for virtual server "test vs" try: response = client.get(url) except requests.exceptions.ConnectionError: print "Error: Unable to connect to " + url sys.exit(1) # Decode the json response. The result will be a dictionary vsConfig = json.loads(response.content) if response.status_code == 200: # Get the properties status = vsConfig['properties']['basic']['enabled']; if status: # virtual server is enabled, disable it. We only need to send the data that we are changing # so create a new dictionary with just this 'enabled'. newVSConfig = {'properties': {'basic': {'enabled': False}}} print vs + " is enabled. Disable it." else: # virtual server is disabled, enable it newVSConfig = {'properties': {'basic': {'enabled': True}}} print vs + " is disabled. Enable it." response = client.put(url, data = json.dumps(newVSConfig), headers = jsontype) vsConfig = json.loads(response.content) if response.status_code != 200: print "Error putting virtual server config: URL=%s Status=%d Id=%s: %s" %(url, response.status_code, vsConfig['error_id'], vsConfig['error_text']) else: print "Error getting virtual server config: URL=%s Status=%d Id=%s: %s" %(url, response.status_code, vsConfig['error_id'], vsConfig['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:
$ startstopvs.py
test vs is enabled. Disable it.
Notes
This program it is sending only the 'enabled' value to the server by creating a new dictionary with just this value in the 'basic' properties section. Alternatively, the entire Virtual Server configuration could have been returned to the server with just the enabled value changed. Sending just the data that has changed reduces the chances of overwriting another user's changes if multiple programs are concurrently accessing the RESTful API.
Read More