The following code uses Stingray's RESTful API to list all the pools defined for a cluster. The code is written in TrafficScript. This example has more extensive comments then the other examples and most of these are applicable to all the examples. This rule does a single GET request for the list of pools and then loops through that list, each element of which is a hash, and outputs the pool name to a web page. A subroutine in stmrestclient is used to do the actual RESTful API call. stmrestclient is attached to the article Tech Tip: Using the RESTful Control API with TrafficScript - Overview.
stmrest_listpools
################################################################################ # stmrest_listpools # # This rule gets the lists of pools and creates a webpage to display the list. # # To run this rule add it as a request rule to an HTTP Virtual Server and in a # browser enter the path /rest/listpools. # # It uses the subroutines in stmrestclient ################################################################################ import stmrestclient; # Only run the rule for the path /rest/listpools if (http.getPath() != "/rest/listpools") break; $resource = "pools"; # The configuration resource $accept = "json"; # The format of the response $html = "<b>Pools:</b><br><br>\n"; # Do the HTTP GET to get the lists of pools $response = stmrestclient.stmRestGet($resource, $accept); if ($response["rc"] == 1) { # Since we are getting a list of pools we expect the first element to be 'children' $pools = $response["data"]["children"]; # The value for the key 'children' will be a hash containing an array for each pool # with the key, 'name', set to the name of the pool and the key, 'href', set to the # URL of the pool foreach ($pool in $pools) { $html = $html . $pool["name"] . "<br>"; } } else { # Either the attempt to connect to the Stingray REST API failed, or the request had an # error. $response['info'] should provide some information. If The connection attempt # failed, the most likely reasons for this are that the port, username or password for # the Stingray REST API are incorrect, or the REST API is disabled. The above request # should not have an error, but the most common reason for a GET error is that the # incorrect resource is specified. $html = $html . "There was an error getting the pool list: " . $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/listpools
Pools:
Pool1
Pool2
Read More