vADC Docs

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

by ricknelson on ‎03-27-2013 11:21 AM - edited on ‎07-14-2015 03:48 PM by PaulWallace (643 Views)

The following code uses Stingray's RESTful API to delete a pool.  The code is written in Ruby.  This program deletes the "rbtest" pool created by the addpool.rb example.  To delete a resource you do a HTTP DELETE on the URI for the resource.  If the delete is successful a 204 HTTP status code will be returned.

 

deletepool.rb

 

require 'rest_client'

require 'base64'

require 'json'


poolName = 'rbtest'

url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools/' + poolName

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

begin

    # Try to delete the pool.  If it exists and is deleted we will get a 204. If it doesn't

    # exist we will get a 404, which will causue a resource.not_found exception

    response = RestClient.delete(url, {:authorization => auth})

    if response.code == 204

        puts "Pool #{poolName} deleted"

    else

        puts "Bad status code #{response.code} when deleting pool"

    end

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'

            puts "Pool #{poolName} not found"  

        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:

 

$ delelepool.rb

Pool rbtest deleted

 

Read More

 

Contributors