Make Changes to Your Nutanix Cluster using PHP and cURL

Posted in Datacenter, Scripting, Tech Soup4 years ago • Written by Rick Gouin1 Comment

In my last article, I showed a simple way to read data from a Nutanix cluster using PHP. This method was simple because it didn’t have to send any data to the cluster, just retrieve it. We simply fetched a JSON file to show the data we wanted to show. That would be a perfect solution for making a pretty, end user consumable dashboard to show select pieces of system information.

What if you wanted to take action against your Nutanix cluster, however?  For example, what if you wanted to clone a VM, or create a new one, straight from your little home grown status page?

Well, that would require some changes to how we built the first query.  We’ll have to build a cURL session, provide some data, and then kick it off.  In the example below, I’ll use PHP to clone a VM.


//Fill in your username, password, and hostname below
$username = "username";
$password = "password";
$url = "Your.Host.Here";
//For example purposes, lets just put a VM uuid here of the VM we want to clone.
//If this was a real script, I would grab the uuid by making a different API call.
//Such as the one in the article linked above.
$vm_uuid = "642c44e2-c551-4ca7-a56b-7ddcb645d6c7";
//The Nutanix API will expect the specifications for the clone as a JSON list.  
//Since I'm just sending one parameter, I'll build it by hand below. Otherwise, I would have used JSON_ENCODE.  
//The only required parameter for a cloning task is a name for the new VM. 
//You can see below I'm just using "VMNAME_2" in my example.  You could put a variable or any name there.      
$data_string = "{\"specList\": [{\"name\": \"VMNAME_2\"}]}";
//Lets build a URL and login
$apiURL = "https://".$url.":9440/PrismGateway/services/rest/v1/vms/".$vm_uuid."/clone";
$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;

This is not a complete, particularly useful script, and it isn’t meant to be. Its meant to give you an idea of how to send data to the Nutanix REST API using PHP. This could easily be adapted to use cURL directly as well. This same method will work for many of the “POST” API calls, though most of them will require you to include more parameters than this one. If you have any comments or questions on how you might use this, feel free to comment below.

TAGS: , ,

1 Comment so far. Feel free to join this conversation.

Leave A Response