cancel
Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to delete a pool.  The code is written in TrafficScript.  This rule deletes the "tstest" pool created by the stmrest_addpool example.  To delete a resource you do a HTTP DELETE on the URI for the resource.  If the delete is successful a 204 HTTP status code will be returned.  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_deletepool

################################################################################
# stmrest_deletepool
#
# This rule deletes the pool "tspool".
#
# To run this rule add it as a request rule to an HTTP Virtual Server and in a
# browser enter the path /rest/deletepool.
#
# It uses the subroutines in stmrestclient
################################################################################
import stmrestclient;

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

$pool = "tspool";
$resource = "pools/" . string.escape($pool);
$accept = "json";
$html = "<br><b>Delete Pool " . $pool . "</b><br><br>";

# Check to make sure that the Pool exists
$response = stmrestclient.stmRestGet($resource, $accept);

if ($response["rc"] == 1) {
   $response = stmrestclient.stmRestDelete($resource);

   if ($response["rc"] == 1) {
      $html = $html . "Pool " . $pool . " deleted";
   } else {
      $html = $html . "There was an error deleting pool " . $pool . ": " . $response['info'];
   }
} else {
   if ($response['status'] == 404) {
      $html = $html . "Pool " . $pool . " not found";
   } else {
      $html = $html . "There was an error getting the configuration for 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/deletepool

Pool tstest deleted

 

Read More

 

Version history
Revision #:
2 of 2
Last update:
‎01-13-2021 04:39:PM
Updated by:
 
Labels (1)
Contributors