vADC Docs

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

by ricknelson on ‎02-06-2014 09:02 PM - edited on ‎05-29-2015 09:39 AM by pwallace_1 (598 Views)

The following code uses Stingray's RESTful API to add a pool.   The code is written in PHP and uses STMRESTClient. This program creates a new pool, "phptest", 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 object with just one node.  All other values will get default values when Stingray creates the pool.

 

addpool.php

 

<?php

# Add a pool

require_once 'stmrestclient.inc';

$poolName = 'phptest';

$node = '192.168.168.135:80';

try {

    # Set up the connection

    $client = new STMRestClient();

    /* Check to see if the pool already exist.  If it does not yet exist, then a

     * ResourceNotFoundException exception will be thrown. */

    $response = $client->get("pools/$poolName");

    print("Pool $poolName already exists");

} catch (STMResourceNotFoundException $e) {

    # The pool doesn't exist, so it can be created

    $poolConfig->properties->basic->nodes = array($node);

    #$poolConfig = array('properties' => array('basic' => array('nodes' => array($node))));

    try {

        $response = $client->put("pools/$poolName", $poolConfig);

        print("Pool $poolName added");

    } catch (Exception $e) {

        print ("Error creating Pool $poolName: " . $e->getMessage());

    }

} catch (STMConnectException $e) {

    print ("Error connecting to the REST API: " . $e->getMessage());

} catch (STMAuthException $e) {

    print ("Error logging into the REST API: " . $e->getMessage());

} catch (Exception $e) {

    print ('Error: ' . $e->getMessage());

}


print("</body></html>\n");

?>

 

Running the example

 

This code was tested with PHP 5.3.3 and uses the STMRestClient class that can be found here: Tech Tip: A Stingray Traffic Manager REST Client for PHP

 

To run this program, Make it available via a web server and the output should be:

 

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

 

Contributors