vADC Docs

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

by ricknelson on ‎03-28-2013 03:18 PM - edited on ‎07-14-2015 03:28 PM by PaulWallace (1,359 Views)

The following code uses Stingray's RESTful API to enable or disabled a specific Virtual Server.   The code is written in Perl.  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.pl

 

#!/usr/bin/perl


use REST::Client;

use MIME::Base64;

use JSON;

use URI::Escape;


# Since Stingray is using a self-signed certificate we don't need to verify it

$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;


my $vs = "test vs";

# Because there is a space in the virtual serve name it must be escaped

my $url = "/api/tm/1.0/config/active/vservers/" . uri_escape($vs);


# Set up the connection

my $client = REST::Client->new();

$client->setHost("https://stingray.example.com:9070");

$client->addHeader("Authorization", "Basic " . encode_base64("admin:admin"));


# Get configuration data for the virtual server

$client->GET($url);


# Decode the json response.  The result will be a hash

my $vsConfig = decode_json $client->responseContent();


if ($client->responseCode() == 200) {

    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 => JSON::false}});

        print "$vs is Enabled.  Disable it.\n";

    } else {

        # The virtual server is disabled, enable it.

        %newVSConfig = (properties => { basic => { enabled => JSON::true}});

        print "$vs is Diabled.  Enable it.\n";

    }

    $client->addHeader("Content-Type", "application/json");

    $client->PUT($url, encode_json(\%newVSConfig));

    $vsConfig = decode_json $client->responseContent();

    if ($client->responseCode() != 200) {

        print "Error putting virtual server config. status=" . $client->responseCode() . " Id=" . $vsConfig->{error_id} . ": " . $vsConfig->{error_text} . "\n";

    }

} else {

    print "Error getting pool config. status=" . $client->responseCode() . " Id=" . $vsConfig->{error_id} . ": " . $vsConfig->{error_text} . "\n";

}

 

Running the example

 

This code was tested with Perl 5.14.2 and version 249 of the REST::Client module.

 

Run the Perl script as follows:

 

$ startstopvs.pl

test vs is enabled. Disable it.

 

Notes

 

This program it is sending only the 'enabled' value to the server by creating a new hash with 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

 

Contributors