vADC Docs

Tech Tip: Using the RESTful Control API with Perl

by on ‎03-05-2013 02:41 AM - edited on ‎07-22-2015 02:45 PM by PaulWallace (8,961 Views)

This article explains how to use Stingray's RESTful Control API with Perl.  It's a little more work than with Tech Tip: Using the RESTful Control API with Python - Overview but once the basic environment is set up and the framework in place, you can rapidly create scripts in Perl to manage Stingray's configuration.

 

Getting Started

 

The code examples below depend on several Perl modules that may not be installed by default on your client system: REST::Client, MIME::Base64 and JSON.

 

  • On a Linux system, the best way to pull these in to the system perl is by using the system package manager (apt or rpm).
  • On a Mac (or a home-grown perl instance), you can install them using CPAN

 

Preparing a Mac to use CPAN

 

Install the package 'Command Line Tools for Xcode' either from within the Xcode or directly from https://developer.apple.com/downloads/.

 

Some of the CPAN build scripts indirectly seek out /usr/bin/gcc-4.2 and won't build if /usr/bin/gcc-4.2 is missing.  If gcc-4.2 is missing, the following should help:

 

$ ls -l /usr/bin/gcc-4.2

ls: /usr/bin/gcc-4.2: No such file or directory

$ sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

 

Installing the perl modules

 

It may take 20 minutes for CPAN to initialize itself, download, compile, test and install the necessary perl modules:

 

$ sudo perl –MCPAN –e shell

cpan> install Bundle::CPAN

cpan> install REST:: Client

cpan> install MIME::Base64

cpan> install JSON

 

Your first Perl REST client application

 

This application looks for a pool named 'Web Servers'.  It prints a list of the nodes in the pool, and then sets the first one to drain.

 

#!/usr/bin/perl


use REST::Client;

use MIME::Base64;

use JSON;


# Configurables

$poolname = "Web Servers";

$endpoint = "stingray:9070";

$userpass = "admin:admin";


# Older implementations of LWP check this to disable server verification

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;


# Set up the connection

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


# Newer implementations of LWP use this to disable server verification

# Try SSL_verify_mode => SSL_VERIFY_NONE.  0 is more compatible, but may be deprecated

$client->getUseragent()->ssl_opts( SSL_verify_mode => 0 );


$client->setHost( "https://$endpoint" );

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


# Perform a HTTP GET on this URI

$client->GET( "/api/tm/1.0/config/active/pools/$poolname" );

die $client->responseContent() if( $client->responseCode() >= 300 );


# Add the node to the list of draining nodes

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

print "Pool: $poolname:\n";

print "   Nodes:        " . join( ", ", @{$r->{properties}->{basic}->{nodes}} ) . "\n";

print "   Draining:     " . join( ", ", @{$r->{properties}->{basic}->{draining}} ) . "\n";


# If the first node is not already draining, add it to the draining list

$node = $r->{properties}->{basic}->{nodes}[0];

if( ! ($node ~~ @{$r->{properties}->{basic}->{draining}}) ) {

    print "      Planning to drain: $node\n";

    push @{$r->{properties}->{basic}->{draining}}, $node;

}


# Now put the updated configuration

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

$client->PUT( "/api/tm/1.0/config/active/pools/$poolname", encode_json( $r ) );

die $client->responseContent() if( $client->responseCode() >= 300 );


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

print "   Now draining: " . join( ", ", @{$r->{properties}->{basic}->{draining}} ) . "\n";

 

Running the script

 

$ perl ./pool.pl 

Pool: Web Servers:

   Nodes:        192.168.207.101:80, 192.168.207.103:80, 192.168.207.102:80

   Draining:     192.168.207.102:80

      Planning to drain: 192.168.207.101:80

   Now draining: 192.168.207.101:80, 192.168.207.102:80

 

Notes

 

This script was tested against two different installations of perl, with different versions of the LWP library.  It was necessary to disable SSL certificate checking using:

 

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

 

... with the older, and:

 

# Try SSL_verify_mode => SSL_VERIFY_NONE.  0 is more compatible, but may be deprecated

$client->getUseragent()->ssl_opts( SSL_verify_mode => 0 );

 

with the new.  The older implementation failed when using SSL_VERIFY_NONE.  YMMV.

Contributors