Rackspace API

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

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.

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

  • 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/$account/servers \
     -H "X-Auth-Token: $TOKEN" \
     -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/$account/servers \
     -H "X-Auth-Token: $TOKEN" | python -m json.tool
  • Create a NextGen Cloud Server (in your account's default region):
curl -s https://servers.api.rackspacecloud.com/v1.0/$account/servers -X POST \
    -H "Content-Type: application/json" \
    -H "X-Auth-Token: $TOKEN" \
    -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": 21614475,
"imageId": 12345678,
"metadata": {},
"name": "testing",
"progress": 0,
"status": "BUILD"
}
}

Cloud Files

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

External links