The following code uses Stingray's RESTful API to add a pool. The code is written in Ruby. This program creates a new pool, "rbtest", first doing a GET to make sure the pool doesn't already exist, and if the pool doesn't exist, the pool is created by doing a PUT with just the minimum data need to create a pool. In this case the program creates a properties hash with just one node. All other values will get default values when Stingray creates the pool.
addpool.rb
require 'rest_client' require 'base64' require 'json' poolName = 'rbtest' poolConfig = {'properties' => {'basic' => {'nodes' => ['192.168.168.135:80']}}} url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools/' + poolName auth = 'Basic ' + Base64.encode64('admin:admin') begin # First see if the pool already exists. If it does exist, a 404 will returned # which will causue a resource.not_found exception response = RestClient.get(url, {:authorization => auth}) puts "Pool #{poolName} already exists" rescue => e # If a 404 is returned then e.response will be a json object, otherwise it may not be if defined?(e.response) error = JSON.parse(e.response) if error['error_id'] == 'resource.not_found' begin # PUT the new data response = RestClient.put(url, JSON.generate(poolConfig), {:content_type => :json, :authorization => auth}) if response.code == 201 # When creating a new resource we expect to get a 201 puts "Pool #{poolName} added" else puts "Bad status code #{response.code} when adding pool" end rescue => e puts "Error: URL=#{url} Error: #{e.message}" end else puts "Error: URL=#{url} Error: #{e.message}" end else puts "Error: URL=#{url} Error: #{e.message}" end 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:
$ addpool.py
Pool rbtest added
Notes
The only difference between doing a PUT to change a resource and a PUT to add a resource is the HTTP status code returned. When changing a resource 200 is the expected status code and when adding a resource, 201 is the expected status code.
Read More