Note: Please go to docs.rightscale.com to access the current RightScale documentation set. Also, feel free to Chat with us!
Home > Guides > RightScale API 1.5 > Examples > Servers > List Servers

List Servers

List all Servers owned by the current account.

icon_curl_v1.png    icon_rs_api_client_v1.png  

Table of Contents

Warning!  Some API calls can be "expensive" (resource intensive) depending on the number of cloud assets.  Listing all servers can take time and may not be recommended for accounts with many servers.

Curl

Example Call

Prerequisite:  Example assumes you have previously authenticated, and your valid session cookie is in 'mycookie'.

curl -i -H X_API_VERSION:1.5 -b mycookie -X GET https://my.rightscale.com/api/servers.xml

Tip: If you save XML output of your 'ListServers' script to a file, you can then run a single command to get the number of Servers for that account.  For example:

$ ./ListServers > ListServers.xml          # Output of the example ListServers script saved to ListServers.xml
$ grep "<server>" ListServers.xml | wc -l  # Find each <server> XML block, and then count how many lines there are
482                                        # 482 servers found in this example

Sample Output

Note: Truncated XML output without headers included (to save space).  Shows only first and last server for the account.

<?xml version="1.0" encoding="UTF-8"?>
<servers>
  <server>
    <updated_at>2010/11/10 05:47:08 +0000</updated_at>
    <links>
      <link href="/api/servers/710980" rel="self"/>
      <link href="/api/deployments/28185" rel="deployment"/>
      <link href="/api/clouds/232/instances/2PL2523DPV4ES" rel="next_instance"/>
      <link href="/api/servers/710990/alert_specs" rel="alert_specs"/>
    </links>
    <description></description>
    <created_at>2009/12/01 21:51:31 +0000</created_at>
    <name>PHP FrontEnd (Chef) -2</name>
    <actions>
      <action rel="launch"/>
    </actions>
    <state>inactive</state>
  </server>
. . .
  <server>
    <updated_at>2012/10/10 17:46:12 +0000</updated_at>
    <links>
      <link href="/api/servers/529811001" rel="self"/>
      <link href="/api/deployments/89627" rel="deployment"/>
      <link href="/api/clouds/1859/instances/EQPVQSTLQC6KQ" rel="current_instance"/>
      <link href="/api/clouds/1859/instances/6N7C6A3D98EMO" rel="next_instance"/>
      <link href="/api/servers/529911001/alert_specs" rel="alert_specs"/>
    </links>
    <description></description>
    <created_at>2012/10/10 17:43:42 +0000</created_at>
    <name>Greg Doe Test 1</name>
    <actions>
      <action rel="terminate"/>
    </actions>
    <state>operational</state>
  </server>
</servers>

Supplemental

Show only Servers in a Specific Deployment

The following supplemental example includes a "filter" such that only servers from the specified deployment are listed.

Important!  When using a filter (or view) the computation is done on the server side. This results in a lighter response (less content) returned to the client.  The server essentially fetches all content and returns it to the client. 

#!/bin/sh -e
DEPLOYMENT="316795001"
curl -i -H X_API_VERSION:1.5 -b mycookie -d filter[]="deployment_href==/api/deployments/$DEPLOYMENT" -X GET https://my.rightscale.com/api/servers.xml
Show only Load Balancers in a specific Deployment

The following supplemental example is similar to the previous, but it includes an additional filter.  Not only does it only show servers in a specific deployment (as opposed to all servers in the cloud you authenticated with) but it restricts the response to servers that have "balancer" in their name. 

Important!   When using more than one filter they are logically ANDed.  In our example, this mean servers in the specified deployment AND their name has the string "balancer" in it.  Note that a partial match is allowed with the name filter, hence a complicated regular expression with wildcards is not needed.  (e.g. *balancer*)

#!/bin/sh -e
DEPLOYMENT="306795001"                          # Deployment ID to list servers from
curl -i -H X_API_VERSION:1.5 -b mycookie \      # Invoke curl, specify version and authentication cookie
-d filter[]="deployment_href==/api/deployments/$DEPLOYMENT" \
-d filter[]="name==balancer" \                  # ONLY show servers that have "balancer" in their name
-X GET https://my.rightscale.com/api/servers.xml

In the Create 3 Tier Servers tutorial, the above script and API call would respond with "Bethany_load_balancer1" and "Bethany_load_balancer2".

right_api_client

Example Call

Warning!  Some API calls can be "expensive" (resource intensive) depending on the number of cloud assets.  Listing all servers can take time and may not be recommended for accounts with many servers.

require 'rubygems'
require 'pp'                          # Pretty print Ruby gem
require 'right_api_client'            # RightScale API Client gem
                                      # Create client object. Effectively authenticate to use the RS API.
@client = RightApi::Client.new(:email => 'greg.doe@example.com', :password => 'SecurePasswd', :account_id => '1234')
pp @client.servers.index              # Print/output all Servers in this account

Sample Output

[#<RightApi::ResourceDetail resource_type="server", name="PHP FrontEnd (Chef Alpha) -2">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_load_balancer1">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_load_balancer2">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_app_server1">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_app_server2">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_db_server1">,
 #<RightApi::ResourceDetail resource_type="server", name="Bethany_db_server2">,
 #<RightApi::ResourceDetail resource_type="server", name="Base ServerTemplate for Linux (v13.1)">,
 #<RightApi::ResourceDetail resource_type="server", name="Base ServerTemplate for Linux (RSB)  (v13.1)">,
 #<RightApi::ResourceDetail resource_type="server", name="Azure West US">,
 #<RightApi::ResourceDetail resource_type="server", name="Google">,
 #<RightApi::ResourceDetail resource_type="server", name="Rackspace">,
 #<RightApi::ResourceDetail resource_type="server", name="CloudStack PHP">,
 #<RightApi::ResourceDetail resource_type="server", name="CloudStack PHP v1">,
 #<RightApi::ResourceDetail resource_type="server", name="Resat API 1.5 server">,                                         
 #<RightApi::ResourceDetail resource_type="server", name="Storage Toolbox (v13.1.1)">]

 

PowerShell

Prerequisite:  Example assumes you have previously authenticated and your session cookie for subsequent requests is in 'cookieContainer'.

Example Call

The following will show all servers within the account (not just servers in a deployment).

#get cookie container from authentication $cookieContainer

$webRequest = [System.Net.WebRequest]::Create("https://my.rightscale.com/api/servers.xml")
$webRequest.Method = "GET"
$webRequest.CookieContainer = $cookieContainer
$webRequest.Headers.Add("X_API_VERSION", "1.5");

[System.Net.WebResponse] $webResponse = $webRequest.GetResponse()
$responseStream = $webResponse.GetResponseStream()
$responseStreamReader = New-Object System.IO.StreamReader -argumentList $responseStream
[string]$responseString = $responseStreamReader.ReadToEnd()
$responseString

Sample Output

<?xml version="1.0" encoding="UTF-8"?>
<servers>
  <server>
    <state>inactive</state>
    <updated_at>2010/11/10 05:47:08 +0000</updated_at>
    <created_at>2009/11/17 23:45:16 +0000</created_at>
    <links>
      <link rel="self" href="/api/servers/708440"/>
      <link rel="deployment" href="/api/deployments/31226"/>
      <link rel="next_instance" href="/api/clouds/232/instances/6JV1EEKB421BH"/>
      <link rel="alert_specs" href="/api/servers/708440/alert_specs"/>
      <link rel="alerts" href="/api/servers/708440/alerts"/>
    </links>
    <description></description>
    <actions>
      <action rel="launch"/>
      <action rel="clone"/>
    </actions>
    <name>PHP FrontEnd - Rackspace</name>
  </server>

. . . output truncated . . .

  <server>
    <state>inactive</state>
    <updated_at>2013/10/08 23:05:33 +0000</updated_at>
    <created_at>2013/10/08 22:50:16 +0000</created_at>
    <links>
      <link rel="self" href="/api/servers/930342001"/>
      <link rel="deployment" href="/api/deployments/96308"/>
      <link rel="next_instance" href="/api/clouds/2160/instances/8EL23Q5CAC6SP"/>
      <link rel="alert_specs" href="/api/servers/930342001/alert_specs"/>
      <link rel="alerts" href="/api/servers/930342001/alerts"/>
    </links>
    <description></description>
    <actions>
      <action rel="launch"/>
      <action rel="clone"/>
    </actions>
    <name>Base ServerTemplate for Linux (RSB)  (v13.2.1) v1</name>
  </server>
  <server>
    <state>operational</state>
    <updated_at>2013/10/10 21:52:33 +0000</updated_at>
    <created_at>2013/10/10 21:51:35 +0000</created_at>
    <links>
      <link rel="self" href="/api/servers/932206001"/>
      <link rel="deployment" href="/api/deployments/42060"/>
      <link rel="current_instance" href="/api/clouds/2175/instances/19TO4SE9LC1BS"/>
      <link rel="next_instance" href="/api/clouds/2175/instances/12RRDHIJ68ABF"/>
      <link rel="alert_specs" href="/api/servers/932206001/alert_specs"/>
      <link rel="alerts" href="/api/servers/932206001/alerts"/>
    </links>
    <description></description>
    <actions>
      <action rel="terminate"/>
      <action rel="clone"/>
    </actions>
    <name>Base ServerTemplate for Linux (RSB) (v13.5.0-LTS)</name>
  </server>
</servers>


 

You must to post a comment.
Last modified
09:48, 11 Oct 2013

Tags

Classifications

This page has no classifications.

Announcements

None


© 2006-2014 RightScale, Inc. All rights reserved.
RightScale is a registered trademark of RightScale, Inc. All other products and services may be trademarks or servicemarks of their respective owners.