vADC Docs

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

by ricknelson on ‎10-15-2013 10:05 AM (655 Views)

The following code uses Stingray's RESTful API to list all the pools defined for a cluster and for each pool it lists the nodes defined for that pool, including draining and disabled nodes. The code is written in TrafficScript. This example builds on the previous stmrest_listpools example.  This rule does a GET request for the list of pools and then while looping through the list of pools, a GET is done for each pool to retrieve the configuration parameters for that pool.  A subroutine in stmrestclient is used to do the actual RESTful API call.  stmrestclient is attached to the article Tech Tip: Using the RESTful Control API with TrafficScript - Overview.

stmrest_listpoolnodes



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


# stmrest_listpoolnodes


#


# This rule lists the names of all pools and also the nodes, draining nodes


# and disable nodes in each pool.


#


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


# browser enter the path /rest/listpoolnodes.


#


# It uses the subroutines in stmrestclient


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


import stmrestclient;



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



$resource = "pools";


$accept = "json";



$html = "<br><b>Pools:</b><br>";



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


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


   $pools = $response["data"]["children"];


   foreach ($pool in $pools) {


      $poolName = $pool["name"];


      $response = stmrestclient.stmRestGet($resource. "/" . string.escape($poolName), $accept);


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


         $poolConfig = $response["data"];


         $nodes = $poolConfig["properties"]["basic"]["nodes"];


         $draining = $poolConfig["properties"]["basic"]["draining"];


         $disabled = $poolConfig["properties"]["basic"]["disabled"];


         $html = $html . "<br>" . $poolName . ":<br>";


         $html = $html . "<br>    Nodes: ";


         foreach ($node in $nodes) {


            $html = $html . $node . " ";


         }


         $html = $html . "\n";


         if (array.length($draining) > 0) {


            $html = $html . "<br>    Draining Nodes: ";


            foreach ($node in $draining) {


               $html = $html . $node . " ";


            }


            $html = $html . "\n";


         }


         if (array.length($disabled) > 0) {


            $html = $html . "<br>    Disabled Nodes: ";


            foreach ($node in $disabled) {


               $html = $html . $node . " ";


            }


            $html = $html . "\n";


         }


         $html = $html . "<br>\n";


      } else {


         $html = $html . "There was an error getting the pool configuration for pool . " . $poolName . ": " . $response['info'];


      }


   }


} else {


   $html = $html . "<br>There was an error getting the pool list: " . $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/listpoolnodes

Pools:


Pool1

    Nodes:  192.168.1.100 192.168.1.101

    Draining:  192.168.1.101

    Disabled:  192.168.1.102


Pool2

    Nodes:  192.168.1.103 192.168.1.104

Read More