vADC Docs

Tech Tip: Using the RESTful Control API with TrafficScript - addpool

by ricknelson on ‎10-15-2013 03:49 PM (872 Views)

The following code uses Stingray's RESTful API to add a pool.   The code is written in TrafficScript. This rule creates a new pool, "tstest", 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 rule creates a properties hash with just one node.  All other values will get default values when Stingray creates the pool.  Subroutines in stmrestclient are used to do the actual RESTful API calls.  stmrestclient is attached to the article Tech Tip: Using the RESTful Control API with TrafficScript - Overview.

stmrest_addpool



################################################################################


# stmrest_addpool


#


# This rule adds the pool "tspool" with the node "192.168.168.135:80"


#


# To run this rule add it as a request rule to an HTTP Virtual Server and in a


# browser enter the path /rest/addpool.


#


# It uses a set of subroutines in stmrestclient


################################################################################


import stmrestclient;



if (http.getPath() != "/rest/addpool") break;



$pool = "tspool";


$node = "192.168.168.135";


$resource = "pools/" . string.escape($pool);


$accept = "json";



$html = "<br><b>Add Pool " . $pool . "</b><br><br>";



# Check to make sure that the Pool doesn't already exist


$response = stmrestclient.stmRestGet($resource, $accept);


if ($response["rc"] == 1) {


   $html = $html . "Pool " . $pool . " already exists";


} else {


   $poolConfig =["properties" => ["basic" => ["nodes" => [$node . ":80"]]]];


   $response = stmrestclient.stmRestPut($resource, $accept, $poolConfig);


   if ($response["rc"] == 1) {


      $html = $html . "Pool " . $pool . " created";


   } else {


      $html = $html . "There was an error creating pool " . $pool . ": "  . $response['info'];


   }


}



http.sendResponse("200 OK", "text/html", $html, "");








Running the example


This rule should be added as a request rule to a Virtual Server and run with the URL:

http://<hostname>/rest/addpool

Pool tstest 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