The following code uses Stingray's RESTful API to add a pool. The code is written in Perl. This program creates a new pool, "pltest", first doing a GET to make sure the pool doesn't already exist, and if the pool doesn't exist, the pool is created by doing a PUT with just the minimum data needed to create a pool. In this case the program creates a properties hash with just one node. All other values will get default values when Stingray creates the pool.
addpool.pl
#!/usr/bin/perl use REST::Client; use MIME::Base64; use JSON; # Since Stingray is using a self-signed certificate we don't need to verify it $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; my $poolName = 'pltest'; my %pool = (properties => {basic => {nodes => [ '192.168.168.135:80']}}); my $url = "/api/tm/1.0/config/active/pools/$poolName"; # Set up the connection my $client = REST::Client->new(); $client->setHost("https://stingray.example.com:9070"); $client->addHeader("Authorization", "Basic " . encode_base64("admin:admin")); #First see if the pool already exists $client->GET($url); if ($client->responseCode == 404) { $client->addHeader("Content-Type", "application/json"); $client->PUT($url, encode_json(\%pool)); my $poolConfig = decode_json $client->responseContent(); if ($client->responseCode() == 201) { # When creating a new resource we expect to get a 201 print "Pool $poolName added"; } else { print "Error adding pool. status=" . $client->responseCode() . " Id=" . $vsConfig->{error_id} . ": " . $vsConfig->{error_text} . "\n"; } } else { if ($client->responseCode() == 200) { print "Pool $poolName already exists"; } 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:
$ addpool.pl
Pool pltest added
Notes
The only difference between doing a PUT to change a resource and a PUT to add a resource is the HTTP status code returned. When changing a resource 200 is the expected status code and when adding a resource, 201 is the expected status code.
Read More