vADC Docs

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

by ricknelson on ‎03-28-2013 03:04 PM - edited on ‎07-30-2015 01:31 PM by Community Manager (3,161 Views)

The following code uses Stingray's RESTful API to list all the pools defined on a cluster. The code is written in Perl. This example has more extensive comments then the following examples and most of these are applicable to all the examples. This program does a single GET request for the list of pools and then loops through that list, each element of which is a hash, and then outputs the pool name.

 

listpools.pl

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl
 
 
use REST::Client;
 
use MIME::Base64;
 
use JSON;
 
 
print "Pools:\n";
 
 
# Since Stingray is using a self-signed certificate we don't need to verify it
 
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
 
 
# Set up the connection
 
my $client = REST::Client->new();
 
 
# Setup the basic authorization header with the encoded Userid and Password.
 
# These need to match a UserId and Password for a Stingray user
 
$client->addHeader("Authorization", "Basic " . encode_base64("admin:admin"));
 
 
# Do the HTTP GET to get the lists of pools
 
$client->GET("/api/tm/1.0/config/active/pools");
 
 
# Deserialize the JSON response into a hash
 
my $response = decode_json($client->responseContent());
 
if ($client->responseCode() == 200) {
 
    # Obtain a reference to the children array
 
    my $poolArrayRef = $response->{children};
 
    foreach my $pool (@$poolArrayRef) {
 
        print $pool->{name} . "\n";
 
    }
 
} else {
 
    # We weren't able to connect to the Stingray or there was a problem with the request.
 
    # The most likely reasons for this are:
 
    # - the hostname of the Stingray instance is incorrect
 
    # - this client doesn't have network access to the Stingray instance or port 9070
 
    # - the RESTful API is disabled
 
    # - the RESTful API is using using a different port
 
    # - the URL is incorrect
 
    print "Error: status=" . $client->responseCode() . " Id=" . $response->{error_id} . ": " . $response->{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:

 

$ listpoolnodes.pl
Pools:
 
Pool11
 
 
Pool2
   

 

Read More