vADC Docs

Tech Tip: Using the RESTful Control API with Ruby - listpools

by ricknelson on ‎03-26-2013 05:07 PM - edited on ‎07-14-2015 04:42 PM by PaulWallace (738 Views)

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.




require 'rest_client'

require 'base64'

require 'json'

puts "Pools:\n\n"

# Set the URL

url = ''

# 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')


    # 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']


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}"



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






Read More