Nutanix: How to Create a New Storage Container with PHP

Posted in Datacenter, Scripting4 years ago • Written by Rick GouinNo Comments

Nutanix_logoI recently had a need to create a new container in an existing Nutanix storage pool from a web interface.  I did it using PHP.  I figured since I wrote the code, which is very similar to the cloning script I posted here, I may as well post this as well.

The script itself is pretty self explanatory.  Put your variables in the top, and the script posts the required data to the Nutanix REST API to create the new container.

One thing to note is that the API call to create a container requires you to include the UUID of the storage pool that you want the container created in. You can get this from the GUI, or programmatically via the REST API. In this example, I’ve just hard coded a Storage Pool UUID to show how it all works.

//Fill in your username, password, and hostname below
$username = "username";
$password = "password";
$url = "mynutanixhost.local";
//For example purposes, lets just put a Storage Pool uuid here of the pool to put the container into.
$pool_uuid = "b52c918c-4ec7-4d23-8091-8622869b270b";
//The Nutanix API will expect the specifications for the containter as a JSON list.  
//Since I'm just sending two parameters, I'll build it by hand below. Otherwise, I would have used JSON_ENCODE.  
//The only required parameters for a new container is a name for the new container, and the storage pool to put it in. 
//You can see below I'm just using "New_Container" in my example for the new container name.  You could put a variable or any name there.      
$data_string = "{\"name\": \"New Container\",\"storagePoolUuid\": \"".$pool_uuid."\"}"; //This is the hand made JSON, all on 1 line
//Lets build a URL and login
$apiURL = "https://".$url.":9440/PrismGateway/services/rest/v1/containers/";
$login = $username.":".$password;
//Start cURL
$ch = curl_init($apiURL);
//Setup our cURL options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                                                                     
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
$result = curl_exec($ch);
//I'll just output the results for example purposes
echo "Results here:".$result;
TAGS: , ,

Leave A Response