Skip to content

Instantly share code, notes, and snippets.

@epequeno
Created September 18, 2014 23:47
Show Gist options
  • Save epequeno/b88af5d668818eebf4e0 to your computer and use it in GitHub Desktop.
Save epequeno/b88af5d668818eebf4e0 to your computer and use it in GitHub Desktop.
practice with python/ raxapi
# make a file called auth.json that has the following:
{
"auth": {
"RAX-KSKEY:apiKeyCredentials": {
"username": "",
"apiKey": ""
}
}
}
# then another .py file with the following:
import json
import requests
json_header = {'Content-Type':'application/json'}
id_url = 'https://identity.api.rackspacecloud.com/v2.0/tokens'
data = json.dumps(json.loads(open('auth.json', 'r').read()))
req = requests.post(id_url, data=data, headers=json_header)
token = req.json()['access']['token']['id']
tenantid = req.json()['access']['user']['roles'][1]['tenantId']
dc_urls = [endpoint['publicURL'] for endpoint in req.json()['access']['serviceCatalog'][6]['endpoints']]
auth_header = {'X-Auth-Token': token}
for dc in dc_urls:
servers_url = dc + "/servers"
req = requests.get(servers_url, headers=auth_header)
if len(req.json()['servers']) > 0:
print "%s has %d servers:" % (dc[8:11], len(req.json()['servers']))
for i in req.json()['servers']:
print ' ', i['name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment