Skip to content

Instantly share code, notes, and snippets.

@ConsoleCatzirl
Created December 17, 2013 17:46
Show Gist options
  • Save ConsoleCatzirl/8009393 to your computer and use it in GitHub Desktop.
Save ConsoleCatzirl/8009393 to your computer and use it in GitHub Desktop.
Test hpcloud auth
import restclient
import json
import os
class ident(object):
def __init__(self, region_url, debug=False):
self.debug = debug
self.connect(url=region_url)
def __enter__(self):
return self
def connect(self, url):
self.resource = restclient.rest.Resource(url)
def api_auth(self, access_key, secret_key):
body = '{ "auth":{ "apiAccessKeyCredentials":{ "accessKey":"' +\
access_key + '", "secretKey":"' + secret_key + '" }}}'
head = {'Content-Type': 'application/json',\
'Accept': 'application/json'}
if self.debug:
print(body, head)
response = self.resource.post('/tokens', body, head)
if self.debug:
print(response)
jdata = json.loads(response)
self.token = jdata['access']['token']['id']
head['X-Auth-Token'] = str(self.token) # convert from unicode
if self.debug:
print(head)
response = self.resource.get('/tenants', head)
if self.debug:
print(response)
jdata = json.loads(response)
self.tenant = jdata['tenants'][0]['name']
self.tenantid = jdata['tenants'][0]['id']
body = '{ "auth":{ "tenantName":"' + self.tenant +\
'", "token": {"id": "' + self.token + '"}}}'
if self.debug:
print(body, head)
response = self.resource.post('/tokens', body, head)
if self.debug:
print(response)
jdata = json.loads(response)
self.token = jdata['access']['token']['id']
head['X-Auth-Token'] = str(self.token) # convert from unicode
return self.tenantid, head
def pass_auth(self, username, password):
body = '{"auth": {"passwordCredentials": {"username": "' + username + \
'", "password": "' + password + '"}}}'
head = {'Content-Type': 'application/json',\
'Accept': 'application/json'}
if self.debug:
print(body, head)
response = self.resource.post('/tokens', body, head)
if self.debug:
print(response)
jdata = json.loads(response)
self.token = jdata['access']['token']['id']
head['X-Auth-Token'] = str(self.token) # convert from unicode
if self.debug:
print(head)
response = self.resource.get('/tenants', head)
if self.debug:
print(response)
jdata = json.loads(response)
self.tenant = jdata['tenants'][0]['name']
self.tenantid = jdata['tenants'][0]['id']
body = '{ "auth":{ "tenantName":"' + self.tenant + '", "token": '\
'{"id": "' + self.token + '"}}}'
if self.debug:
print(body, head)
response = self.resource.post('/tokens', body, head)
if self.debug:
print(response)
jdata = json.loads(response)
self.token = jdata['access']['token']['id']
head['X-Auth-Token'] = str(self.token) # convert from unicode
return self.tenantid, head
def revoke(self):
if self.debug:
print('revoking auth token')
head = {'X-Auth-Token': str(self.token)}
try:
self.resource.delete('/HP-IDM/v1.0/tokens/' + self.token, head)
except restclient.errors.Unauthorized:
pass
def __exit__(self, type, value, traceback):
self.revoke()
def test():
with ident(region_url=os.environ['OS_AUTH_URL'], debug=True) as i:
print(i.pass_auth(username=os.environ['OS_USERNAME'],
password=os.environ['OS_PASSWORD']))
with ident(region_url=os.environ['OS_AUTH_URL'], debug=True) as i:
print(i.api_auth(access_key=os.environ['OS_API_KEY'],
secret_key=os.environ['OS_API_SECRET']))
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment