The following code uses Stingray's RESTful API to list all the pools defined on a cluster. The code is written in Ruby. This example has more extensive comments then the following examples and most of these are applicable to all the examples. This program does a single GET request for the list of pools and then loops through that list, each element of which is a hash, and then outputs the pool name.
listpools.rb
require 'rest_client' require 'base64' require 'json' puts "Pools:\n\n" # Set the URL url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools' # Setup the basic authorization header with the encoded Userid and Password. # These need to match a UserId and Password for a Stingray user auth = 'Basic ' + Base64.encode64('admin:admin') begin # Do the HTTP GET to get the lists of pools response = RestClient.get(url, {:authorization => auth}) data = JSON.parse(response.body) # Deserialize the JSON response into a hash pools = data['children'] pools.each do |pool| puts pool['name'] end rescue => e # We weren't able to connect to the Stingray or there was a problem with the request. # The most likely reasons for this are: # - the hostname of the Stingray instance is incorrect # - this client doesn't have network access to the Stingray instance or port 9070 # - the RESTful API is disabled # - the RESTful API is using using a different port # - the URL is incorrect puts "Error getting pool list: URL=#{url} Error: #{e.message}" end
Running the example
This code was tested with Ruby 1.9.3 and version 1.6.7 of the rest-client module.
Run the Ruby script as follows:
$ listpools.rb
Pools:
Pool1
Pool2
Read More