Difference between revisions of "Rackspace API"
(→Cloud Files) |
(→Cloud Servers) |
||
Line 65: | Line 65: | ||
==Cloud Servers== | ==Cloud Servers== | ||
− | ''Note: See the [http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_preface.html NextGen API docs] or the [http://docs.rackspace.com/servers/api/v1.0/cs-devguide/content/Overview-d1e70.html FirstGen API docs] for more details and examples.'' | + | ''Note: See the [http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_preface.html NextGen API docs] or the [http://docs.rackspace.com/servers/api/v1.0/cs-devguide/content/Overview-d1e70.html FirstGen API docs] for more details and examples. Take note of the API version numbers (e.g., "v1.0", "v2", etc.), as they are important and some are deprecated.'' |
* List all of the ''NextGen'' servers on your account for a given region: | * List all of the ''NextGen'' servers on your account for a given region: | ||
− | + | REGION=dfw | |
− | curl -XGET <nowiki>https://$ | + | curl -XGET <nowiki>https://${REGION}.servers.api.rackspacecloud.com/v2/${MYRAXACCOUNT}/servers</nowiki> \ |
− | -H "X-Auth-Token: $MYRAXTOKEN" \ | + | -H "X-Auth-Token: ${MYRAXTOKEN}" \ |
-H "Accept: application/json" | python -m json.tool | -H "Accept: application/json" | python -m json.tool | ||
* List all of the ''FirstGen'' servers on your account (regardless of region): | * List all of the ''FirstGen'' servers on your account (regardless of region): | ||
− | + | curl -s <nowiki>https://servers.api.rackspacecloud.com/v1.0/${MYRAXACCOUNT}/servers</nowiki> \ | |
− | curl -s <nowiki>https://servers.api.rackspacecloud.com/v1.0/$MYRAXACCOUNT/servers</nowiki> \ | + | -H "X-Auth-Token: ${MYRAXTOKEN}" | python -m json.tool |
− | -H "X-Auth-Token: $MYRAXTOKEN" | python -m json.tool | + | |
* Create a ''NextGen'' Cloud Server (in your account's default region): | * Create a ''NextGen'' Cloud Server (in your account's default region): | ||
− | curl -s <nowiki>https://servers.api.rackspacecloud.com/v1.0/$MYRAXACCOUNT/servers</nowiki> -X POST \ | + | curl -s <nowiki>https://servers.api.rackspacecloud.com/v1.0/${MYRAXACCOUNT}/servers</nowiki> -X POST \ |
-H "Content-Type: application/json" \ | -H "Content-Type: application/json" \ | ||
− | -H "X-Auth-Token: $MYRAXTOKEN" \ | + | -H "X-Auth-Token: ${MYRAXTOKEN}" \ |
-d "{\"server\":{\"name\":\"testing\",\"imageId\":12345678,\"flavorId\":4,}}" | python -m json.tool | -d "{\"server\":{\"name\":\"testing\",\"imageId\":12345678,\"flavorId\":4,}}" | python -m json.tool | ||
Revision as of 17:08, 6 December 2013
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.
Contents
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. Take note of the API version numbers (e.g., "v1.0", "v2", etc.), as they are important and some are deprecated.
- List all of the NextGen servers on your account for a given region:
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):
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" } }
- Delete a Cloud Server:
$ curl -i -X DELETE -H "X-Auth-Token: ${MYRAXTOKEN}" \ https://${REGION}.servers.api.rackspacecloud.com/v2/${MYRAXACCOUNT}/servers/${SERVER_ID}
- Resize (up/down) a Cloud Server (see: for details):
$ curl -i -vv -X POST \ -H "X-Auth-Token: ${MYRAXTOKEN}" -H "Content-Type: application/json" \ https://${REGION}.servers.api.rackspacecloud.com/v2/${MYRAXACCOUNT}/servers/${SERVER_ID}/action \ -d '{"resize":{"flavorRef":"2"}}'
- Change administrative password (see: for details)
$ curl -v -s -XPUT https://servers.api.rackspacecloud.com/v1.0/${MYRAXACCOUNT}/servers/${SERVER_ID} \ -H "Accept: application/json" -H "Content-Type: application/json" -H "X-Auth-Token: ${MYRAXTOKEN}" \ -d '{"server":{"name":"SERVER_NAME", "adminPass": "NEW_PASSWORD"}}' # Normal Response Code: 204
Cloud Files
Note: See the Cloud Files docs for more details and examples.
- Create container (e.g., region DFW):
REGION=dfw; curl -XPUT -H "X-Auth-Token: $MYRAXTOKEN" \ https://storage101.${REGION}1.clouddrive.com/v1/MossoCloudFS_ffff-ffff-ffff-ffff-ffff/container_name
- List all containers in a given region (note: the "
-D -
" prints out the header response as well. Remove that part if you do not need/want it):
REGION=dfw; curl -X GET -D - -H "X-Auth-Token:${MYRAXTOKEN}" "https://storage101.${REGION}1.clouddrive.com/v1/MossoCloudFS_ffff-ffff-ffff-ffff-ffff/"