The following code uses Stingray's RESTful API to enable or disabled a specific Virtual Server. The code is written in Ruby. This program 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.
startstopvs.rb
require 'rest_client' require 'base64' require 'json' vs = "test vs" # Because there is a space in the virtual serve name it must be escaped url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/vservers/' + URI.escape(vs) auth = 'Basic ' + Base64.encode64('admin:admin') begin # Get the config data for the virtual server response = RestClient.get(url, {:authorization => auth}) # Decode the json response. The result will be a hash vsConfig = JSON.parse(response.body) if vsConfig['properties']['basic']['enabled'] # the virtual server is enabled, disable it. We only need to send the data that we # are changing so create a new hash with just this data. newVSConfig = {'properties' => {'basic' => {'enabled' => false}}} puts "#{vs} is enabled. Disable it." else # the virtual server is disabled, enable it newVSConfig = {'properties' => {'basic' => {'enabled' => true}}} puts "#{vs} is disabled. Enable it." end # PUT the new data response = RestClient.put(url, JSON.generate(newVSConfig), {:content_type => :json, :authorization => auth}) rescue => e puts "Error: 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:
$ startstopvs.rb
test vs is enabled. Disable it.
Notes
This program it is sending only the 'enabled' value to the server by creating a new hash 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