Rackspace API

From Christoph's Personal Wiki
Revision as of 18:00, 15 November 2013 by Christoph (Talk | contribs) (Cloud Files)

Jump to: navigation, search

This article will be a somewhat random assortment of API calls to various Rackspace services. I plan to organize this article and add nearly all possible calls. Most of the API calls will be made using `curl` commands, but other resources will be used as well.

Environment variables

Note: This is probably not the most secure way to store your account username, API Key, account number, or token. However, it makes it simpler to illustrate using the Rackspace API in this article.

You can find all of the following (except for your 24-hour-valid token) in the Rackspace Cloud Control Panel under the "Account Settings" section.

MYRAXUSERNAME=your_account_username
MYRAXAPIKEY=your_unique_api_key  # _never_ give this out to anyone!
MYRAXACCOUNT=integer_value  # e.g., 123456
MYRAXTOKEN=your_24_hour_valid_token  # see below

I will be using the above environment variables for the remainder of this article.

Regions

The following are the Rackspace API endpoint regions (aka the data centres where your servers/data live):

  • DFW - Dallas DC
  • HKG - Hong Kong DC
  • IAD - Norther Virginia DC
  • LON - London DC
  • ORD - Chicago DC
  • SYD - Sydney DC

Authentication

For every API call it is first necessary to obtain an authenticated token. These tokens are valid for 24 hours and you must re-authenticate if it has expired. To authenticate, you only need your Rackspace account username ($MYRAXUSERNAME) and your API Key ($MYRAXAPIKEY).

  • Simple authentication (this should return, among other things, an "X-Auth-Token"):
curl -D - -H "X-Auth-Key: $MYRAXAPIKEY" \
     -H "X-Auth-User: $MYRAXUSERNAME" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     https://identity.api.rackspacecloud.com/v1.0
  • Authenticate the same way as above, but time namelookup, connect, starttransfer, and total API calls:
curl -D - -H "X-Auth-Key: $MYRAXAPIKEY" \
     -H "X-Auth-User: $MYRAXUSERNAME" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -w "time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" \
     https://identity.api.rackspacecloud.com/v1.0
  • Authenticate and receive a full listing of API endpoints (for all of the Rackspace services active on your account):
curl -X POST https://identity.api.rackspacecloud.com/v2.0/tokens \
     -d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'$MYRAXUSERNAME'", "apiKey":"'$MYRAXAPIKEY'" } } }' \
     -H "Content-type: application/json"
  • Authenticate the same way as above, but use compression during the send/receive:
curl -i -X POST \
     -H 'Host: identity.api.rackspacecloud.com' \
     -H 'Accept-Encoding: gzip,deflate' \
     -H 'X-LC-Request-ID: 16491440' \
     -H 'Content-Type: application/json; charset=UTF-8' \
     -H 'Content-Length: 119' \
     -H 'Accept: application/json' \
     -H 'User-Agent: libcloud/0.13.0 (Rackspace Monitoring)' \
     --data-binary '{"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": "$MYRAXUSERNAME", "apiKey": "$MYRAXAPIKEY"}}}' \
     --compress https://identity.api.rackspacecloud.com:443/v2.0/tokens
  • Save the "X-Auth-Token" as an environmental variable (remember, this token will only be valid for 24 hours):
MYRAXTOKEN=`curl -s -XPOST https://identity.api.rackspacecloud.com/v2.0/tokens \
  -d'{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"'$MYRAXUSERNAME'","apiKey":"'$MYRAXAPIKEY'"}}}' \
  -H"Content-type:application/json" | \
  python -c 'import sys,json;data=json.loads(sys.stdin.read());print data["access"]["token"]["id"]'`

I will use this $MYRAXTOKEN environment variable (from the last command) for the remainder of this article.

Cloud Servers

Note: See the NextGen API docs or the FirstGen API docs for more details and examples.

  • List all of the NextGen servers on your account for a given region:
account=$MYRAXACCOUNT; region=dfw
curl -XGET https://$region.servers.api.rackspacecloud.com/v2/$MYRAXACCOUNT/servers \
     -H "X-Auth-Token: $MYRAXTOKEN" \
     -H "Accept: application/json" | python -m json.tool
  • List all of the FirstGen servers on your account (regardless of region):
account=$MYRAXACCOUNT
curl -s https://servers.api.rackspacecloud.com/v1.0/$MYRAXACCOUNT/servers \
     -H "X-Auth-Token: $MYRAXTOKEN" | python -m json.tool
  • Create a NextGen Cloud Server (in your account's default region):
curl -s https://servers.api.rackspacecloud.com/v1.0/$MYRAXACCOUNT/servers -X POST \
    -H "Content-Type: application/json" \
    -H "X-Auth-Token: $MYRAXTOKEN" \
    -d "{\"server\":{\"name\":\"testing\",\"imageId\":12345678,\"flavorId\":4,}}" | python -m json.tool

The above should return something similar to the following (note that "status": "BUILD" will become "Active" once the build process is complete):

{
    "server": {
    "addresses": {
    "private": [
    "10.x.x.x"
    ],
    "public": [
    "184.x.x.x"
    ]
},
"adminPass": "lR2aB3g5Xtesting",
"flavorId": 4,
"hostId": "ffffff2cd2470205bbe2f6b6f7f83d14",
"id": 87654321,
"imageId": 12345678,
"metadata": {},
"name": "testing",
"progress": 0,
"status": "BUILD"
}
}

Cloud Files

Note: See the Cloud Files docs for more details and examples.

  • Create container (e.g., region DFW):
curl -XPUT -H "X-Auth-Token: $MYRAXTOKEN" \
  https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_ffff-ffff-ffff-ffff-ffff/container_name

External links