Created
September 18, 2014 23:47
-
-
Save epequeno/b88af5d668818eebf4e0 to your computer and use it in GitHub Desktop.
practice with python/ raxapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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