Showing results for 
Search instead for 
Did you mean: 

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

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.




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 = '' + URI.escape(vs)

auth = 'Basic ' + Base64.encode64('admin:admin')


    # 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."


        # the virtual server is disabled, enable it

        newVSConfig = {'properties' => {'basic' => {'enabled' => true}}}

        puts "#{vs} is disabled.  Enable it."


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



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.



 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


Version history
Revision #:
2 of 2
Last update:
‎01-13-2021 05:16:PM
Updated by:
Labels (1)