RESI API filter

  • 14 March 2014
  • 4 replies
  • 5463 views

Userlevel 2
Badge +13
What is the syntax for the filter on the REST API. Specifically I'm trying to filter a host query by a serviceVMExternalIP.

4 replies

Userlevel 4
Badge +20
Hi Josh

I am by no means an REST-API expert, if it wasn't for the REST API Explorer I'd be further down the path of reading a dummies book.

Are you just trying to access the url via the external cluster IP? for example my external cluster IP is 10.3.148.33.

I can get to the Rest API exploer becasue I have the firewall turned off for the cluster(you can just do open the ports you need)
https://10.3.148.33:9440/PrismGateway/services/rest/v1/hosts/ works for me.

Maybe this blog post can help as well.

http://philthevirtualizer.com/2014/03/03/connecting-to-the-nutanix-prismapi-with-vcenter-orchestrator/

Let me know otherwise and I will track down the info you need.
Userlevel 2
Badge +13
I'm actually trying to generate a CSV of all of the cluster info so I can put it in the documentation that I'm writing. I have 130 nodes scattered across 5 datacenters that are all in linked mode so I'm starting by grabbing the IPs of the CVMs out of PowerCLI (and a few other tidbits of info) and throwing that into a hashtable.

for ($i=0; $i -lt $cvms.cvmIP.count; $i++) { $node = New-Object PSObject -Property @{ block = @{} position = @{} model = @{} cvmName = $cvms[$i].Name cvmIP = $cvms[$i].cvmIP hostIP = @{} ipmiIP = @{} vSphereCluster = $cvms[$i].vCluster datacenter = $cvms[$i].vDatacenter datastore = @{} container = @{} storagePool = @{} nutanixCluster = @{} } $nodes += $node}

Then I want to connect to the IP of the CVM and get the host information, like host IP, IPMI IP, etc as you can see above.

The problem is that when I query https://$IP:9440/PrismGateway/services/rest/v1/hosts/ it comes back with all of the hosts in an array. Now I could just build a function to tear through that array 130 times (which is a little complicated since all the nodes are not actually in the same cluster), but using the REST API Exporer it has a parameter called Filter Criteria that looks like it should filter content based on some kind of input which is EXACTLY WHAT I NEED! YAY! Now if I only knew how to use it!

https://192.168.10.71:9440/PrismGateway/services/rest/v1/hosts/?filterCriteria=TEST
Userlevel 2
Badge +13
Nutanix support hooked me up with this gem:

https://[localhost ip]:9440/PrismGateway/services/rest/v1/hosts?filterCriteria=node_name==[CVM host name].corp.nutanix.com
Badge +5
If you want to get the REST data from your nodes for perl you can do the following. Took me a bit of time to get this sorted out. You need to base64 your password. I used some REST templates for nutanix I found and adapted the to make it a bit simpler.


use REST::Client;
use MIME::Base64;
use JSON;
use Data::Dumper;

# (1) quit unless we have the correct number of command-line args
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "Usage: ntnx_check_vms.pl nutanix_cvm_name ";
exit;
}

$cvm_name = $ARGV[0];

# Configurables - password is base64 encrypted.
$endpoint = $cvm_name.":9440";
$userpass = "YOUR-BASE64-PASSWORD";

# Older implementations of LWP check this to disable server verification
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

# Set up the connection
my $client = REST::Client->new( );

# Newer implementations of LWP use this to disable server verification
# Try SSL_verify_mode => SSL_VERIFY_NONE. 0 is more compatible, but may be deprecated
$client->getUseragent()->ssl_opts( SSL_verify_mode => 0 );

#$client->setHost( "https://$endpoint" );
$client->addHeader( "Authorization", "Basic ".$userpass );

# Perform a HTTP GET on this URI
$client->GET( "https://".$endpoint."/PrismGateway/services/rest/v1/vms/" );
die $client->responseContent() if( $client->responseCode() >= 300 );

# Add the node to the list of draining nodes
my $r = decode_json( $client->responseContent() );

foreach $temp ( @{$r->{entities}} ) {
print "$temp->{vmId} : $temp->{vmName} : $temp->{powerState}";
print "";
}

Reply