Solved

Rest API calls


Userlevel 3
Badge +18
Dear Community,

I'm trying - for the first time - to make some API calls to get specific statistics ...

I think I might have a authentication issue. Can somebody tell me how I can get the response (in php).

Warning: file_get_contents(https://192.168.8.189/PrismGateway/services/rest/v1/cluster/): failed to open stream: Operation timed out in /Users/flhoest/MyProjects/NutanixReporter/nxGetInfo.php on line 5

I'm getting this ....

I know I have to pass login / password to the get function but how can I do this ?

Thanks a lot 🙂

10 replies

Userlevel 3
Badge +18
Ok, some progress. In fact it timed out because I forgot to add the TCP port : 9440.

Now this is different, I'm getting an empty response ...

I'm using the code suggested here : https://stackoverflow.com/questions/9802788/call-a-rest-api-in-php
Userlevel 4
Badge +19
@flhoest

Refer below scripts.

https://github.com/sandeepmp/nutanix/blob/master/Cluster-Report-NTNX.Ps1

https://github.com/nutanix/Connection-viaREST
Userlevel 3
Badge +18
Hi @sandeepmp ,

Thanks a lot. Unfortunately, I won't be able to use PowerShell ...
But I might be able to use the Python version ... let me try !

Thx
Userlevel 3
Badge +18
Ok, the Python script worked ... It created a protection domain.

I really would love to use the cURL method in PhP.
Userlevel 4
Badge +19
@flhoest

Then best way is to use the “Rest API explorer” from prism and make an API test for the required operation.

from the output , you will get the direct curl url.
Userlevel 3
Badge +18
Yes you are right, I'm trying that way.
Issue is : I need to pass authentication. So I'm continue testing.

But thanks your git link is very interesting 😉
Userlevel 4
Badge +19
@flhoest

Hope we were able to assist with your queries.
If so, can you mark this post as resolved?
Userlevel 3
Badge +18
Well, your version of the scripts definitely works fine but this is not solving my immediate issue. I need to learn python 😉
But if will mark it as resolved taking into consideration that I've managed to make your script working perfectly.
Userlevel 3
Badge +18
FYI,

I just managed to make REST API calls using php.

Here is a small demo who query the status of a task UUID in a AHV cluster :

$clusterIP="192.168.8.189";
$API_CALL="api/nutanix/v2.0/tasks/79fcd7ab-b6c8-49bd-ba54-e6706255e01e?include_subtasks_info=true";
$username="username";
$password="password";

$curl = curl_init();

curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$clusterIP.":9440/".$API_CALL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
print(curl_error($curl)."\n");

curl_close($curl);

var_dump(json_decode($result));

?>

This will simply output the following :

code:
$ php -f nxGetInfo.php

object(stdClass)#1 (15) {
["uuid"]=>
string(36) "79fcd7ab-xxxx-49bd-ba54-e6706255e01e"
["meta_request"]=>
object(stdClass)#2 (1) {
["method_name"]=>
string(8) "VmCreate"
}
["meta_response"]=>
object(stdClass)#3 (1) {
["error_code"]=>
int(0)
}
["create_time_usecs"]=>
int(1522403890229910)
["start_time_usecs"]=>
int(1522403890260801)
["complete_time_usecs"]=>
int(1522403890493811)
["last_updated_time_usecs"]=>
int(1522403890493811)
["entity_list"]=>
array(1) {
[0]=>
object(stdClass)#4 (3) {
["entity_id"]=>
string(36) "eeee0d10-2574-4faf-899e-d7cb2e802000"
["entity_type"]=>
string(2) "VM"
["entity_name"]=>
NULL
}
}
["operation_type"]=>
string(8) "VmCreate"
["message"]=>
string(0) ""
["percentage_complete"]=>
int(100)
["progress_status"]=>
string(9) "Succeeded"
["subtask_uuid_list"]=>
array(1) {
[0]=>
string(36) "000dbc71-69aa-4b38-b655-2bd365021a6a"
}
["subtask_info_list"]=>
array(1) {
[0]=>
object(stdClass)#5 (14) {
["uuid"]=>
string(36) "000dbc71-69aa-4b38-b655-2bd365021a6a"
["meta_request"]=>
object(stdClass)#6 (1) {
["method_name"]=>
string(8) "VmCreate"
}
["meta_response"]=>
object(stdClass)#7 (1) {
["error_code"]=>
int(0)
}
["create_time_usecs"]=>
int(1522403890296632)
["start_time_usecs"]=>
int(1522403890331961)
["complete_time_usecs"]=>
int(1522403890387195)
["last_updated_time_usecs"]=>
int(1522403890387195)
["entity_list"]=>
array(1) {
[0]=>
object(stdClass)#8 (3) {
["entity_id"]=>
string(36) "eeee0d10-2574-4faf-899e-d7cb2e802000"
["entity_type"]=>
string(2) "VM"
["entity_name"]=>
NULL
}
}
["operation_type"]=>
string(9) "kVmCreate"
["message"]=>
string(0) ""
["percentage_complete"]=>
int(100)
["progress_status"]=>
string(9) "Succeeded"
["parent_task_uuid"]=>
string(36) "79fcd7ab-b6c8-49bd-ba54-e6706255e01e"
["cluster_uuid"]=>
string(36) "000512b5-c10d-f271-0000-000000005f0a"
}
}
["cluster_uuid"]=>
string(36) "000512b5-xxxx-f271-0000-000000005f0a"
}



I really hope this will help others !

Keywords : php nutanix rest api call
Userlevel 3
Badge +18
More details on my blog : https://flhoest.blogspot.be/2018/03/nutanix-rest-api-calls-samples.html

Reply