The following code uses Stingray's RESTful API to enable or disabled a specific Virtual Server. The code is written in TrafficScript. This rule 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. Subroutines in stmrestclient are used to do the actual RESTful API calls. stmrestclient is attached to the article Tech Tip: Using the RESTful Control API with TrafficScript - Overview.
stmrest_startstopvs
################################################################################ # stmrest_startstopvs # # This rule toggles the enabled status of virtual server "test vs". # # To run this rule add it as a request rule to an HTTP Virtual Server and in a # browser enter the path /rest/startstopvs. # # It uses the subroutines in stmrestclient ################################################################################ import stmrestclient; if (http.getPath() != "/rest/startstopvs") break; $vs = "test vs"; $resource = "vservers/" . string.escape($vs); $accept = "json"; $html = "<br><b>Toggle the status of Virtual Server " . $vs . "</b><br><br>"; $response = stmrestclient.stmRestGet($resource, $accept); if ($response["rc"] == 1) { $vsConfig = $response["data"]; if ($vsConfig["properties"]["basic"]["enabled"]) { $vsConfig["properties"]["basic"]["enabled"] = false; } else { $vsConfig["properties"]["basic"]["enabled"] = true; } $response = stmrestclient.stmRestPut($resource, $accept, $vsConfig); if ($response["rc"] == 1) { if ($vsConfig["properties"]["basic"]["enabled"]) { $html = $html . $vs . " is Disabled. Enable it"; } else { $html = $html . $vs . " is Enabled. Disable it."; } } else { $html = $html . "There was an error updating the virtul server configuration for " . $vs . ": " . $response["info"]; } } else { if ($response["status"] == 404) { $html = $html . "Virtual Server " . $vs . " not found"; } else { $html = $html . "There was an error getting the virtul server configuration for " . $vs . ": " . $response["info"]; } } http.sendResponse("200 OK", "text/html", $html, "");
Running the example
This rule should be added as a request rule to a Virtual Server and run with the URL:
http://<hostname>/rest/startstopvs
test vs is enabled. Disable it.
Notes
This rule it is sending only the "enabled" value to the server by creating a new hash 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